| 1 |
package org.apache.commons.jcs3.auxiliary.lateral; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.util.concurrent.ConcurrentHashMap; |
| 23 |
|
| 24 |
import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheMonitor; |
| 25 |
import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory; |
| 26 |
import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes; |
| 27 |
import org.apache.commons.jcs3.engine.CacheStatus; |
| 28 |
import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal; |
| 29 |
import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class LateralCacheMonitor extends AbstractAuxiliaryCacheMonitor |
| 39 |
{ |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private final ConcurrentHashMap<String, LateralCacheNoWait<Object, Object>> caches; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private final LateralTCPCacheFactory factory; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@Deprecated |
| 58 |
protected static void forceShortIdlePeriod( final long idlePeriod ) |
| 59 |
{ |
| 60 |
LateralCacheMonitor.setIdlePeriod(idlePeriod); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public LateralCacheMonitor(final LateralTCPCacheFactory factory) |
| 71 |
{ |
| 72 |
super("JCS-LateralCacheMonitor"); |
| 73 |
this.factory = factory; |
| 74 |
this.caches = new ConcurrentHashMap<>(); |
| 75 |
setIdlePeriod(20000L); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@SuppressWarnings("unchecked") |
| 84 |
public void addCache(final LateralCacheNoWait<?, ?> cache) |
| 85 |
{ |
| 86 |
this.caches.put(cache.getCacheName(), (LateralCacheNoWait<Object, Object>)cache); |
| 87 |
|
| 88 |
|
| 89 |
if (this.getState() == Thread.State.NEW) |
| 90 |
{ |
| 91 |
this.start(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
public void dispose() |
| 100 |
{ |
| 101 |
this.caches.clear(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Override |
| 108 |
public void doWork() |
| 109 |
{ |
| 110 |
|
| 111 |
log.info( "Number of caches to monitor = " + caches.size() ); |
| 112 |
|
| 113 |
caches.forEach((cacheName, cache) -> { |
| 114 |
|
| 115 |
if (cache.getStatus() == CacheStatus.ERROR) |
| 116 |
{ |
| 117 |
log.info( "Found LateralCacheNoWait in error, " + cacheName ); |
| 118 |
|
| 119 |
final ITCPLateralCacheAttributes lca = |
| 120 |
(ITCPLateralCacheAttributes) cache.getAuxiliaryCacheAttributes(); |
| 121 |
|
| 122 |
|
| 123 |
final ICacheServiceNonLocal<Object, Object> cacheService = |
| 124 |
factory.getCSNLInstance(lca, cache.getElementSerializer()); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if (!(cacheService instanceof ZombieCacheServiceNonLocal)) |
| 129 |
{ |
| 130 |
cache.fixCache(cacheService); |
| 131 |
} |
| 132 |
} |
| 133 |
}); |
| 134 |
} |
| 135 |
} |