/
JMSCacheManagerPeerProvider.java
executable file
·272 lines (226 loc) · 7.63 KB
/
JMSCacheManagerPeerProvider.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
/**
* Copyright 2003-2008 Luck Consulting Pty Ltd
*
* 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 net.sf.ehcache.distribution.jms;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.distribution.CacheManagerPeerProvider;
import net.sf.ehcache.distribution.CachePeer;
import static net.sf.ehcache.distribution.jms.JMSUtil.CACHE_MANAGER_UID;
import static net.sf.ehcache.distribution.jms.JMSUtil.localCacheManagerUid;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Creates a single instance of JMSCachePeer which does not publishing and
* subscribing to a single topic for the CacheManager
*
* @author benoit.perroud@elca.ch
* @author Greg Luck
*/
public class JMSCacheManagerPeerProvider implements CacheManagerPeerProvider {
private static final Logger LOG = Logger
.getLogger(JMSCacheManagerPeerProvider.class.getName());
/***/
protected CacheManager cacheManager;
/***/
protected List<CachePeer> remoteCachePeers = new ArrayList<CachePeer>();
/***/
protected TopicConnection replicationTopicConnection;
/***/
protected Topic replicationTopic;
/***/
protected QueueConnection getQueueConnection;
/***/
protected Queue getQueue;
/***/
protected AcknowledgementMode acknowledgementMode;
/***/
protected QueueReceiver getQueueRequestReceiver;
/***/
protected TopicSession topicPublisherSession;
/***/
protected TopicPublisher topicPublisher;
/***/
protected TopicSubscriber topicSubscriber;
/***/
protected QueueSession getQueueSession;
/***/
protected JMSCachePeer cachePeer;
/***/
protected boolean listenToTopic;
/**
* Constructor
*
* @param cacheManager
* @param replicationTopicConnection
* @param replicationTopic
* @param getQueueConnection
* @param getQueue
* @param acknowledgementMode
* @param listenToTopic
* whether this provider should listen to events made to the JMS
* topic
*/
public JMSCacheManagerPeerProvider(CacheManager cacheManager,
TopicConnection replicationTopicConnection, Topic replicationTopic,
QueueConnection getQueueConnection, Queue getQueue,
AcknowledgementMode acknowledgementMode, boolean listenToTopic) {
this.cacheManager = cacheManager;
this.replicationTopicConnection = replicationTopicConnection;
this.replicationTopic = replicationTopic;
this.getQueueConnection = getQueueConnection;
this.getQueue = getQueue;
this.acknowledgementMode = acknowledgementMode;
this.listenToTopic = listenToTopic;
}
/**
* Time for a cluster to form. This varies considerably, depending on the
* implementation.
*
* @return the time in ms, for a cluster to form
*/
public long getTimeForClusterToForm() {
if (LOG.isLoggable(Level.FINEST)) {
LOG.finest("getTimeForClusterToForm ( ) called ");
}
return 0;
}
/**
* The replication scheme. Each peer provider has a scheme name, which can
* be used by caches to specify for replication and bootstrap purposes.
*
* @return the well-known scheme name, which is determined by the
* replication provider author.
*/
public String getScheme() {
return "JMS";
}
/**
* Notifies providers to initialise themselves.
* <p/>
*
* @throws CacheException
*/
public void init() {
try {
topicPublisherSession = replicationTopicConnection
.createTopicSession(false, acknowledgementMode.toInt());
replicationTopicConnection
.setExceptionListener(new ExceptionListener() {
public void onException(JMSException e) {
LOG.log(Level.SEVERE,
"Exception on replication Connection: "
+ e.getMessage(), e);
}
});
topicPublisher = topicPublisherSession
.createPublisher(replicationTopic);
if (listenToTopic) {
LOG.fine("Listening for message on topic "
+ replicationTopic.getTopicName());
// ignore messages we have sent. The third parameter is noLocal,
// which means do not deliver back to the sender
// on the same connection
TopicSession topicSubscriberSession = replicationTopicConnection
.createTopicSession(false, acknowledgementMode.toInt());
topicSubscriber = topicSubscriberSession.createSubscriber(
replicationTopic, null, true);
replicationTopicConnection.start();
}
// noLocal is only supported in the JMS spec for topics. We need to
// use a message selector
// on the queue to achieve the same effect.
getQueueSession = getQueueConnection.createQueueSession(false,
acknowledgementMode.toInt());
String messageSelector = CACHE_MANAGER_UID + " <> "
+ localCacheManagerUid(cacheManager);
getQueueRequestReceiver = getQueueSession.createReceiver(getQueue,
messageSelector);
getQueueConnection.start();
} catch (JMSException e) {
throw new CacheException(
"Exception while creating JMS connections: "
+ e.getMessage(), e);
}
cachePeer = new JMSCachePeer(cacheManager, topicPublisher,
topicPublisherSession, getQueueSession);
remoteCachePeers.add(cachePeer);
try {
if (listenToTopic) {
topicSubscriber.setMessageListener(cachePeer);
}
getQueueRequestReceiver.setMessageListener(cachePeer);
} catch (JMSException e) {
LOG.log(Level.SEVERE, "Cannot register " + cachePeer
+ " as messageListener", e);
}
}
/**
* Providers may be doing all sorts of exotic things and need to be able to
* clean up on dispose.
*
* @throws CacheException
*/
public void dispose() throws CacheException {
LOG.fine("JMSCacheManagerPeerProvider for CacheManager "
+ cacheManager.getName() + " being disposed.");
cachePeer.dispose();
JMSUtil.closeMessageProducer(topicPublisher);
JMSUtil.closeMessageConsumer(topicSubscriber);
JMSUtil.closeSession(topicPublisherSession);
JMSUtil.closeConnection(replicationTopicConnection, true);
JMSUtil.closeMessageConsumer(getQueueRequestReceiver);
JMSUtil.closeSession(getQueueSession);
JMSUtil.closeConnection(getQueueConnection);
}
/**
* @return a list of {@link CachePeer} peers for the given cache, excluding
* the local peer.
*/
public List<CachePeer> listRemoteCachePeers(Ehcache cache)
throws CacheException {
return remoteCachePeers;
}
/**
* Register a new peer.
*
* @param rmiUrl
*/
public void registerPeer(String rmiUrl) {
throw new CacheException("Not implemented for JMS");
}
/**
* Unregisters a peer.
*
* @param rmiUrl
*/
public void unregisterPeer(String rmiUrl) {
throw new CacheException("Not implemented for JMS");
}
}