| 1 |
package org.apache.commons.jcs3.auxiliary.remote.http.client; |
| 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.io.Serializable; |
| 24 |
import java.util.concurrent.ConcurrentHashMap; |
| 25 |
|
| 26 |
import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheMonitor; |
| 27 |
import org.apache.commons.jcs3.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient; |
| 28 |
import org.apache.commons.jcs3.engine.CacheStatus; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class RemoteHttpCacheMonitor extends AbstractAuxiliaryCacheMonitor |
| 36 |
{ |
| 37 |
|
| 38 |
private final ConcurrentHashMap<RemoteHttpCache<?, ?>, RemoteHttpCache<?, ?>> remoteHttpCaches; |
| 39 |
|
| 40 |
|
| 41 |
private final RemoteHttpCacheFactory factory; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public RemoteHttpCacheMonitor(final RemoteHttpCacheFactory factory) |
| 49 |
{ |
| 50 |
super("JCS-RemoteHttpCacheMonitor"); |
| 51 |
this.factory = factory; |
| 52 |
this.remoteHttpCaches = new ConcurrentHashMap<>(); |
| 53 |
setIdlePeriod(3000L); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public void notifyError( final RemoteHttpCache<?, ?> remoteCache ) |
| 62 |
{ |
| 63 |
if ( log.isInfoEnabled() ) |
| 64 |
{ |
| 65 |
log.info( "Notified of an error. " + remoteCache ); |
| 66 |
} |
| 67 |
|
| 68 |
remoteHttpCaches.put( remoteCache, remoteCache ); |
| 69 |
notifyError(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Override |
| 76 |
protected void dispose() |
| 77 |
{ |
| 78 |
this.remoteHttpCaches.clear(); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@Override |
| 87 |
protected void doWork() |
| 88 |
{ |
| 89 |
|
| 90 |
if (factory == null) |
| 91 |
{ |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
for (final RemoteHttpCache<?, ?> remoteCache : this.remoteHttpCaches.values()) |
| 99 |
{ |
| 100 |
try |
| 101 |
{ |
| 102 |
if ( remoteCache.getStatus() == CacheStatus.ERROR ) |
| 103 |
{ |
| 104 |
final RemoteHttpCacheAttributes attributes = remoteCache.getRemoteHttpCacheAttributes(); |
| 105 |
|
| 106 |
final IRemoteHttpCacheClient<Serializable, Serializable> remoteService = |
| 107 |
factory.createRemoteHttpCacheClientForAttributes( attributes ); |
| 108 |
|
| 109 |
if ( log.isInfoEnabled() ) |
| 110 |
{ |
| 111 |
log.info( "Performing Alive check on service " + remoteService ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if ( remoteService.isAlive() ) |
| 116 |
{ |
| 117 |
remoteCache.fixCache( remoteService ); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
allright.set(false); |
| 122 |
} |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
catch ( final IOException ex ) |
| 127 |
{ |
| 128 |
allright.set(false); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
log.error( ex ); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |