1 package org.apache.commons.jcs3.auxiliary.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24
25 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes;
26 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener;
27 import org.apache.commons.jcs3.auxiliary.remote.server.behavior.RemoteType;
28 import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
29 import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal;
30 import org.apache.commons.jcs3.engine.stats.StatElement;
31 import org.apache.commons.jcs3.engine.stats.Stats;
32 import org.apache.commons.jcs3.engine.stats.behavior.IStatElement;
33 import org.apache.commons.jcs3.engine.stats.behavior.IStats;
34 import org.apache.commons.jcs3.log.Log;
35 import org.apache.commons.jcs3.log.LogManager;
36
37
38
39
40
41
42
43 public class RemoteCache<K, V>
44 extends AbstractRemoteAuxiliaryCache<K, V>
45 {
46
47 private static final Log log = LogManager.getLog( RemoteCache.class );
48
49
50 private final RemoteCacheMonitor monitor;
51
52
53 private AbstractRemoteCacheNoWaitFacade<K, V> facade;
54
55
56
57
58
59
60
61
62
63
64
65
66 public RemoteCache( final IRemoteCacheAttributes cattr,
67 final ICacheServiceNonLocal<K, V> remote,
68 final IRemoteCacheListener<K, V> listener,
69 final RemoteCacheMonitor monitor )
70 {
71 super( cattr, remote, listener );
72 this.monitor = monitor;
73
74 RemoteUtils.configureGlobalCustomSocketFactory( getRemoteCacheAttributes().getRmiSocketFactoryTimeoutMillis() );
75 }
76
77
78
79
80 @Override
81 public IStats getStatistics()
82 {
83 final IStats stats = new Stats();
84 stats.setTypeName( "Remote Cache" );
85
86 final ArrayList<IStatElement<?>> elems = new ArrayList<>();
87
88 elems.add(new StatElement<>( "Remote Host:Port", getIPAddressForService() ) );
89 elems.add(new StatElement<>( "Remote Type", this.getRemoteCacheAttributes().getRemoteTypeName() ) );
90
91
92
93
94
95
96
97 final IStats sStats = super.getStatistics();
98 elems.addAll(sStats.getStatElements());
99
100 stats.setStatElements( elems );
101
102 return stats;
103 }
104
105
106
107
108
109
110 protected void setFacade(final AbstractRemoteCacheNoWaitFacade<K, V> facade)
111 {
112 this.facade = facade;
113 }
114
115
116
117
118
119
120 protected AbstractRemoteCacheNoWaitFacade<K, V> getFacade()
121 {
122 return facade;
123 }
124
125
126
127
128
129
130
131
132
133
134 @Override
135 protected void handleException( final Exception ex, final String msg, final String eventName )
136 throws IOException
137 {
138 final String message = "Disabling remote cache due to error: " + msg;
139
140 logError( cacheName, "", message );
141 log.error( message, ex );
142
143
144 if ( !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
145 {
146
147 setRemoteCacheService( new ZombieCacheServiceNonLocal<>( getRemoteCacheAttributes().getZombieQueueMaxSize() ) );
148 }
149
150
151
152 monitor.notifyError();
153
154 log.debug( "Initiating failover, rcnwf = {0}", facade );
155
156 if ( facade != null && facade.getAuxiliaryCacheAttributes().getRemoteType() == RemoteType.LOCAL )
157 {
158 log.debug( "Found facade, calling failover" );
159
160
161 facade.failover( facade.getPrimaryServer() );
162 }
163
164 if ( ex instanceof IOException )
165 {
166 throw (IOException) ex;
167 }
168 throw new IOException( ex );
169 }
170
171
172
173
174
175
176 @Override
177 public String toString()
178 {
179 return "RemoteCache: " + cacheName + " attributes = " + getRemoteCacheAttributes();
180 }
181
182
183
184
185
186
187 @Override
188 public String getEventLoggingExtraInfo()
189 {
190 return getIPAddressForService();
191 }
192
193
194
195
196
197
198
199
200 protected String getIPAddressForService()
201 {
202 String ipAddress = "(null)";
203 if (this.getRemoteCacheAttributes().getRemoteLocation() != null)
204 {
205 ipAddress = this.getRemoteCacheAttributes().getRemoteLocation().toString();
206 }
207 return ipAddress;
208 }
209 }