|
| 1 | +/* |
| 2 | + * Copyright 2014-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.hazelcast; |
| 18 | + |
| 19 | +import java.io.EOFException; |
| 20 | +import java.io.IOException; |
| 21 | +import java.time.Duration; |
| 22 | +import java.time.Instant; |
| 23 | + |
| 24 | +import com.hazelcast.nio.ObjectDataInput; |
| 25 | +import com.hazelcast.nio.ObjectDataOutput; |
| 26 | +import com.hazelcast.nio.serialization.StreamSerializer; |
| 27 | + |
| 28 | +import org.springframework.session.MapSession; |
| 29 | + |
| 30 | +/** |
| 31 | + * A {@link com.hazelcast.nio.serialization.Serializer} implementation that handles the |
| 32 | + * (de)serialization of {@link MapSession} stored on {@link com.hazelcast.core.IMap}. |
| 33 | + * |
| 34 | + * <p> |
| 35 | + * The use of this serializer is optional and provides faster serialization of sessions. |
| 36 | + * If not configured to be used, Hazelcast will serialize sessions via |
| 37 | + * {@link java.io.Serializable} by default. |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * If multiple instances of a Spring application is run, then all of them need to use the |
| 41 | + * same serialization method. If this serializer is registered on one instance and not |
| 42 | + * another one, then it will end up with HazelcastSerializationException. The same applies |
| 43 | + * when clients are configured to use this serializer but not the members, and vice versa. |
| 44 | + * Also note that, if a new instance is created with this serialization but the existing |
| 45 | + * Hazelcast cluster contains the values not serialized by this but instead the default |
| 46 | + * one, this will result in incompatibility again. |
| 47 | + * |
| 48 | + * <p> |
| 49 | + * An example of how to register the serializer on embedded instance can be seen below: |
| 50 | + * |
| 51 | + * <pre class="code"> |
| 52 | + * Config config = new Config(); |
| 53 | + * |
| 54 | + * // ... other configurations for Hazelcast ... |
| 55 | + * |
| 56 | + * SerializerConfig serializerConfig = new SerializerConfig(); |
| 57 | + * serializerConfig.setImplementation(new HazelcastSessionSerializer()).setTypeClass(MapSession.class); |
| 58 | + * config.getSerializationConfig().addSerializerConfig(serializerConfig); |
| 59 | + * |
| 60 | + * HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); |
| 61 | + * </pre> |
| 62 | + * |
| 63 | + * Below is the example of how to register the serializer on client instance. Note that, |
| 64 | + * to use the serializer in client/server mode, the serializer - and hence |
| 65 | + * {@link MapSession}, must exist on the server's classpath and must be registered via |
| 66 | + * {@link com.hazelcast.config.SerializerConfig} with the configuration above for each |
| 67 | + * server. |
| 68 | + * |
| 69 | + * <pre class="code"> |
| 70 | + * ClientConfig clientConfig = new ClientConfig(); |
| 71 | + * |
| 72 | + * // ... other configurations for Hazelcast Client ... |
| 73 | + * |
| 74 | + * SerializerConfig serializerConfig = new SerializerConfig(); |
| 75 | + * serializerConfig.setImplementation(new HazelcastSessionSerializer()).setTypeClass(MapSession.class); |
| 76 | + * clientConfig.getSerializationConfig().addSerializerConfig(serializerConfig); |
| 77 | + * |
| 78 | + * HazelcastInstance hazelcastClient = HazelcastClient.newHazelcastClient(clientConfig); |
| 79 | + * </pre> |
| 80 | + * |
| 81 | + * @author Enes Ozcan |
| 82 | + * @since 2.4.0 |
| 83 | + */ |
| 84 | +public class HazelcastSessionSerializer implements StreamSerializer<MapSession> { |
| 85 | + |
| 86 | + private static final int SERIALIZER_TYPE_ID = 1453; |
| 87 | + |
| 88 | + @Override |
| 89 | + public void write(ObjectDataOutput out, MapSession session) throws IOException { |
| 90 | + out.writeUTF(session.getOriginalId()); |
| 91 | + out.writeUTF(session.getId()); |
| 92 | + writeInstant(out, session.getCreationTime()); |
| 93 | + writeInstant(out, session.getLastAccessedTime()); |
| 94 | + writeDuration(out, session.getMaxInactiveInterval()); |
| 95 | + for (String attrName : session.getAttributeNames()) { |
| 96 | + Object attrValue = session.getAttribute(attrName); |
| 97 | + if (attrValue != null) { |
| 98 | + out.writeUTF(attrName); |
| 99 | + out.writeObject(attrValue); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void writeInstant(ObjectDataOutput out, Instant instant) throws IOException { |
| 105 | + out.writeLong(instant.getEpochSecond()); |
| 106 | + out.writeInt(instant.getNano()); |
| 107 | + } |
| 108 | + |
| 109 | + private void writeDuration(ObjectDataOutput out, Duration duration) throws IOException { |
| 110 | + out.writeLong(duration.getSeconds()); |
| 111 | + out.writeInt(duration.getNano()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public MapSession read(ObjectDataInput in) throws IOException { |
| 116 | + String originalId = in.readUTF(); |
| 117 | + MapSession cached = new MapSession(originalId); |
| 118 | + cached.setId(in.readUTF()); |
| 119 | + cached.setCreationTime(readInstant(in)); |
| 120 | + cached.setLastAccessedTime(readInstant(in)); |
| 121 | + cached.setMaxInactiveInterval(readDuration(in)); |
| 122 | + try { |
| 123 | + while (true) { |
| 124 | + // During write, it's not possible to write |
| 125 | + // number of non-null attributes without an extra |
| 126 | + // iteration. Hence the attributes are read until |
| 127 | + // EOF here. |
| 128 | + String attrName = in.readUTF(); |
| 129 | + Object attrValue = in.readObject(); |
| 130 | + cached.setAttribute(attrName, attrValue); |
| 131 | + } |
| 132 | + } |
| 133 | + catch (EOFException ignored) { |
| 134 | + } |
| 135 | + return cached; |
| 136 | + } |
| 137 | + |
| 138 | + private Instant readInstant(ObjectDataInput in) throws IOException { |
| 139 | + long seconds = in.readLong(); |
| 140 | + int nanos = in.readInt(); |
| 141 | + return Instant.ofEpochSecond(seconds, nanos); |
| 142 | + } |
| 143 | + |
| 144 | + private Duration readDuration(ObjectDataInput in) throws IOException { |
| 145 | + long seconds = in.readLong(); |
| 146 | + int nanos = in.readInt(); |
| 147 | + return Duration.ofSeconds(seconds, nanos); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int getTypeId() { |
| 152 | + return SERIALIZER_TYPE_ID; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void destroy() { |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments