|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| package net.sf.ehcache.distribution; |
|
18 |
| |
|
19 |
| import net.sf.ehcache.CacheManager; |
|
20 |
| import net.sf.ehcache.CacheException; |
|
21 |
| import net.sf.ehcache.Cache; |
|
22 |
| |
|
23 |
| import java.io.IOException; |
|
24 |
| import java.net.InetAddress; |
|
25 |
| import java.util.Date; |
|
26 |
| import java.util.List; |
|
27 |
| import java.util.ArrayList; |
|
28 |
| import java.util.Iterator; |
|
29 |
| import java.rmi.NotBoundException; |
|
30 |
| |
|
31 |
| import org.apache.commons.logging.Log; |
|
32 |
| import org.apache.commons.logging.LogFactory; |
|
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 |
| public final class MulticastRMICacheManagerPeerProvider extends RMICacheManagerPeerProvider implements CacheManagerPeerProvider { |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| public static final int STALE_PEER_TIME_MS = 11000; |
|
65 |
| |
|
66 |
| private static final Log LOG = LogFactory.getLog(MulticastRMICacheManagerPeerProvider.class.getName()); |
|
67 |
| |
|
68 |
| |
|
69 |
| private final MulticastKeepaliveHeartbeatReceiver heartBeatReceiver; |
|
70 |
| private final MulticastKeepaliveHeartbeatSender heartBeatSender; |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
126
| public MulticastRMICacheManagerPeerProvider(CacheManager cacheManager, InetAddress groupMulticastAddress,
|
|
79 |
| Integer groupMulticastPort) { |
|
80 |
126
| super(cacheManager);
|
|
81 |
126
| heartBeatReceiver = new MulticastKeepaliveHeartbeatReceiver(this, groupMulticastAddress, groupMulticastPort);
|
|
82 |
126
| heartBeatSender = new MulticastKeepaliveHeartbeatSender(cacheManager, groupMulticastAddress, groupMulticastPort);
|
|
83 |
| } |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
125
| public final void init() throws CacheException {
|
|
89 |
125
| try {
|
|
90 |
125
| heartBeatReceiver.init();
|
|
91 |
125
| heartBeatSender.init();
|
|
92 |
| } catch (IOException exception) { |
|
93 |
| LOG.error("Error starting heartbeat. Error was: " + exception.getMessage(), exception); |
|
94 |
| throw new CacheException(exception.getMessage()); |
|
95 |
| } |
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
53789
| public final synchronized void registerPeer(String rmiUrl) {
|
|
103 |
| |
|
104 |
53789
| try {
|
|
105 |
53789
| CachePeer cachePeer = lookupRemoteCachePeer(rmiUrl);
|
|
106 |
53787
| CachePeerEntry cachePeerEntry = new CachePeerEntry(cachePeer, new Date());
|
|
107 |
53788
| peerUrls.put(rmiUrl, cachePeerEntry);
|
|
108 |
| } catch (IOException e) { |
|
109 |
| if (LOG.isDebugEnabled()) { |
|
110 |
| LOG.debug("Unable to lookup remote cache peer for " + rmiUrl + ". Removing from peer list. Cause was: " |
|
111 |
| + e.getMessage()); |
|
112 |
| } |
|
113 |
| peerUrls.remove(rmiUrl); |
|
114 |
| } catch (NotBoundException e) { |
|
115 |
| peerUrls.remove(rmiUrl); |
|
116 |
| if (LOG.isDebugEnabled()) { |
|
117 |
| LOG.debug("Unable to lookup remote cache peer for " + rmiUrl + ". Removing from peer list. Cause was: " |
|
118 |
| + e.getMessage()); |
|
119 |
| } |
|
120 |
| } catch (Throwable t) { |
|
121 |
| LOG.error("Unable to lookup remote cache peer for " + rmiUrl |
|
122 |
| + ". Cause was not due to an IOException or NotBoundException which will occur in normal operation:" + |
|
123 |
| " " + t.getMessage()); |
|
124 |
| } |
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
2094
| public final synchronized List listRemoteCachePeers(Cache cache) throws CacheException {
|
|
131 |
2094
| List remoteCachePeers = new ArrayList();
|
|
132 |
2094
| List staleList = new ArrayList();
|
|
133 |
2094
| synchronized (peerUrls) {
|
|
134 |
2094
| for (Iterator iterator = peerUrls.keySet().iterator(); iterator.hasNext();) {
|
|
135 |
460405
| String rmiUrl = (String) iterator.next();
|
|
136 |
460405
| String rmiUrlCacheName = extractCacheName(rmiUrl);
|
|
137 |
460405
| try {
|
|
138 |
460405
| if (!rmiUrlCacheName.equals(cache.getName())) {
|
|
139 |
452034
| continue;
|
|
140 |
| } |
|
141 |
8371
| CachePeerEntry cachePeerEntry = (CachePeerEntry) peerUrls.get(rmiUrl);
|
|
142 |
8371
| Date date = cachePeerEntry.date;
|
|
143 |
8371
| if (!stale(date)) {
|
|
144 |
8370
| CachePeer cachePeer = cachePeerEntry.cachePeer;
|
|
145 |
8370
| remoteCachePeers.add(cachePeer);
|
|
146 |
| } else { |
|
147 |
1
| if (LOG.isDebugEnabled()) {
|
|
148 |
0
| LOG.debug("rmiUrl " + rmiUrl + " is stale. Either the remote peer is shutdown or the " +
|
|
149 |
| "network connectivity has been interrupted. Will be removed from list of remote cache peers"); |
|
150 |
| } |
|
151 |
1
| staleList.add(rmiUrl);
|
|
152 |
| } |
|
153 |
| } catch (Exception exception) { |
|
154 |
| LOG.error(exception.getMessage(), exception); |
|
155 |
| throw new CacheException("Unable to list remote cache peers. Error was " + exception.getMessage()); |
|
156 |
| } |
|
157 |
| } |
|
158 |
| |
|
159 |
2094
| for (int i = 0; i < staleList.size(); i++) {
|
|
160 |
1
| String rmiUrl = (String) staleList.get(i);
|
|
161 |
1
| peerUrls.remove(rmiUrl);
|
|
162 |
| } |
|
163 |
| } |
|
164 |
2094
| return remoteCachePeers;
|
|
165 |
| } |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
125
| public final void dispose() {
|
|
172 |
125
| heartBeatSender.dispose();
|
|
173 |
125
| heartBeatReceiver.dispose();
|
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
8371
| protected final boolean stale(Date date) {
|
|
185 |
8371
| long now = System.currentTimeMillis();
|
|
186 |
8371
| return date.getTime() < (now - STALE_PEER_TIME_MS);
|
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| protected static final class CachePeerEntry { |
|
194 |
| |
|
195 |
| private final CachePeer cachePeer; |
|
196 |
| private final Date date; |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
53788
| public CachePeerEntry(CachePeer cachePeer, Date date) {
|
|
205 |
53790
| this.cachePeer = cachePeer;
|
|
206 |
53788
| this.date = date;
|
|
207 |
| } |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
0
| public final CachePeer getCachePeer() {
|
|
213 |
0
| return cachePeer;
|
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
0
| public final Date getDate() {
|
|
221 |
0
| return date;
|
|
222 |
| } |
|
223 |
| |
|
224 |
| } |
|
225 |
| |
|
226 |
| } |