-
Notifications
You must be signed in to change notification settings - Fork 1
/
HazelcastStore.java
319 lines (279 loc) · 10.5 KB
/
HazelcastStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright 2015-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.tomcat.session.hazelcast;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.TimeUnit;
import org.apache.catalina.Session;
import org.apache.catalina.Store;
import org.apache.catalina.session.StandardSession;
import org.apache.catalina.session.StoreBase;
import org.apache.juli.logging.Log;
import org.appng.tomcat.session.SessionData;
import org.appng.tomcat.session.Utils;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.ClasspathXmlConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.ManagementCenterConfig;
import com.hazelcast.config.MulticastConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.hazelcast.map.listener.EntryEvictedListener;
/**
* A {@link Store} using Hazelcast's {@link IMap} to store sessions.
* <p/>
* Configuration parameters (defaults in brackets):
* <ul>
* <li>mapName (tomcat.sessions)<br/>
* The name of the map used to store the sessions.</li>
* <li>mode (multicast)<br/>
* The mode to use, one of multicast, tcp, client, standalone.</li>
* <li>group (dev)<br/>
* The Hazelcast group to use.</li>
* <li>addresses (localhost:5701)<br/>
* A comma separated list of server addresses to use, only applies to 'client' mode.</li>
* <li>port (5701)<br/>
* The Hazelcast port to use.</li>
* <li>multicastGroup (224.2.2.3)<br/>
* The multicast group, only applies to 'multicast' mode.</li>
* <li>multicastPort (54327)<br/>
* The multicast port, only applies to 'multicast' mode.</li>
* <li>multicastTimeoutSeconds (2)<br/>
* The multicast timeout, only applies to 'multicast' mode.</li>
* <li>multicastTimeToLive (32)<br/>
* The multicast ttl, only applies to 'multicast' mode.</li>
* <li>tcpMembers (<i>null</i>)<br/>
* A comma separated list of members to connect to, only applies to 'tcp' mode.</li>
* <li>clusterName (appNG)<br/>
* The Hazelcast cluster name.</li>
* <li>instanceName (dev)<br/>
* The Hazelcast instance name.</li>
* </ul>
*
* @author Matthias Müller
*/
public class HazelcastStore extends StoreBase implements EntryEvictedListener<String, SessionData> {
private final Log log = Utils.getLog(HazelcastStore.class);
private HazelcastInstance instance;
private String mapName = "tomcat.sessions";
private Mode mode = Mode.MULTICAST;
private String addresses = "localhost:5701";
private int port = NetworkConfig.DEFAULT_PORT;
private String multicastGroup = MulticastConfig.DEFAULT_MULTICAST_GROUP;
private int multicastPort = MulticastConfig.DEFAULT_MULTICAST_PORT;
private int multicastTimeoutSeconds = MulticastConfig.DEFAULT_MULTICAST_TIMEOUT_SECONDS;
private int multicastTimeToLive = MulticastConfig.DEFAULT_MULTICAST_TTL;
private String tcpMembers;
private boolean managementCenterEnabled;
private String clusterName = "appNG";
private String instanceName = "dev";
private String configFile = "hazelcast.xml";
public enum Mode {
MULTICAST, TCP, CLIENT, STANDALONE, CLASSPATH;
}
@Override
protected void initInternal() {
super.initInternal();
Config config = getConfig();
switch (mode) {
case CLIENT:
instance = HazelcastClient.getHazelcastClientByName(instanceName);
if (null == instance) {
ClientConfig clientConfig = new ClientConfig();
clientConfig.setInstanceName(instanceName);
String[] addressArr = addresses.split(",");
for (String address : addressArr) {
clientConfig.getNetworkConfig().addAddress(address.trim());
}
instance = HazelcastClient.newHazelcastClient(clientConfig);
log.info(String.format("Using new client %s, connecting to %s", instanceName,
clientConfig.getNetworkConfig().getAddresses()));
} else {
log.info(String.format("Using existing client %s", instanceName));
}
return;
case TCP:
TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
tcpIpConfig.setEnabled(true);
if (null != tcpMembers) {
tcpIpConfig.addMember(tcpMembers);
log.info("Using TCP mode with members: " + tcpMembers);
} else {
log.warn("TCP mode is used, but tcpMembers is not set!");
}
instance = Hazelcast.getOrCreateHazelcastInstance(config);
break;
case MULTICAST:
MulticastConfig multicastConfig = config.getNetworkConfig().getJoin().getMulticastConfig();
multicastConfig.setEnabled(true);
multicastConfig.setMulticastGroup(multicastGroup);
multicastConfig.setMulticastPort(multicastPort);
multicastConfig.setMulticastTimeoutSeconds(multicastTimeoutSeconds);
multicastConfig.setMulticastTimeToLive(multicastTimeToLive);
log.info("Using MULTICAST on " + multicastGroup + ":" + multicastPort);
instance = Hazelcast.getOrCreateHazelcastInstance(config);
break;
case CLASSPATH:
config = new ClasspathXmlConfig(configFile);
instance = Hazelcast.getOrCreateHazelcastInstance(config);
log.info("Using classpath config:" + getClass().getClassLoader().getResource(configFile));
break;
default:
instance = Hazelcast.getOrCreateHazelcastInstance(config);
log.info("Using STANDALONE config");
break;
}
log.info(String.format("Using instance %s", instance));
getSessions().addEntryListener(this, true);
}
public void entryEvicted(EntryEvent<String, SessionData> event) {
SessionData oldValue = event.getOldValue();
String siteName = oldValue.getSite();
try (ByteArrayInputStream in = new ByteArrayInputStream(oldValue.getData());
ObjectInputStream ois = Utils.getObjectInputStream(in, siteName, getManager().getContext())) {
StandardSession session = new StandardSession(getManager());
session.readObjectData(ois);
session.expire(true);
} catch (IOException | ClassNotFoundException e) {
log.error("Error expiring session " + event.getKey(), e);
}
}
private Config getConfig() {
Config config = new Config();
config.setClusterName(clusterName);
config.setInstanceName(instanceName);
config.getNetworkConfig().setPort(port);
if (managementCenterEnabled) {
config.setManagementCenterConfig(new ManagementCenterConfig());
}
config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
return config;
}
@Override
protected void destroyInternal() {
log.info(String.format("Shutting down instance %s", instance));
instance.shutdown();
}
public void save(Session session) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
((StandardSession) session).writeObjectData(oos);
String site = Utils.getSiteName(session.getSession());
SessionData sessionData = new SessionData(session.getId(), site, bos.toByteArray());
getSessions().put(session.getId(), sessionData, session.getMaxInactiveInterval(), TimeUnit.SECONDS);
log.debug("saved: " + sessionData);
}
}
@Override
public HazelcastPersistentManager getManager() {
return (HazelcastPersistentManager) super.getManager();
}
public StandardSession load(String id) throws ClassNotFoundException, IOException {
StandardSession session = null;
try {
// pessimistic lock block to prevent concurrency problems whilst finding sessions
getSessions().lock(id);
SessionData sessionData = getSessions().get(id);
if (null == sessionData) {
log.debug(String.format("Session %s not found in map %s", id, getMapNameInternal()));
} else {
try (ByteArrayInputStream is = new ByteArrayInputStream(sessionData.getData());
ObjectInputStream ois = Utils.getObjectInputStream(is, sessionData.getSite(),
manager.getContext())) {
session = (StandardSession) this.manager.createEmptySession();
session.readObjectData(ois);
log.debug("loaded: " + sessionData);
}
}
} finally {
getSessions().unlock(id);
}
return session;
}
public void remove(String id) throws IOException {
SessionData removed = getSessions().remove(id);
log.debug("removed: " + removed);
}
public String[] keys() throws IOException {
return getSessions().keySet().toArray(new String[0]);
}
public int getSize() throws IOException {
return getSessions().size();
}
public void clear() throws IOException {
getSessions().clear();
}
private IMap<String, SessionData> getSessions() {
return instance.getMap(getMapNameInternal());
}
private String getMapNameInternal() {
return getManager().getName() + mapName;
}
// getters and setters
public void setMapName(String mapName) {
this.mapName = mapName;
}
public void setMode(String mode) {
if (null != mode) {
this.mode = Mode.valueOf(mode.trim().toUpperCase());
}
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public void setAddresses(String addresses) {
this.addresses = addresses;
}
public void setPort(int port) {
this.port = port;
}
public void setMulticastGroup(String multicastGroup) {
this.multicastGroup = multicastGroup;
}
public void setMulticastPort(int multicastPort) {
this.multicastPort = multicastPort;
}
public void setMulticastTimeoutSeconds(int multicastTimeoutSeconds) {
this.multicastTimeoutSeconds = multicastTimeoutSeconds;
}
public void setMulticastTimeToLive(int multicastTimeToLive) {
this.multicastTimeToLive = multicastTimeToLive;
}
public void setTcpMembers(String tcpMembers) {
this.tcpMembers = tcpMembers;
}
public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}
public void setManagementCenterEnabled(boolean managementCenterEnabled) {
this.managementCenterEnabled = managementCenterEnabled;
}
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
}