| 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 org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheFactory; |
| 23 |
import org.apache.commons.jcs3.auxiliary.AuxiliaryCache; |
| 24 |
import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; |
| 25 |
import org.apache.commons.jcs3.auxiliary.remote.RemoteCacheNoWait; |
| 26 |
import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheClient; |
| 27 |
import org.apache.commons.jcs3.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient; |
| 28 |
import org.apache.commons.jcs3.auxiliary.remote.server.behavior.RemoteType; |
| 29 |
import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager; |
| 30 |
import org.apache.commons.jcs3.engine.behavior.IElementSerializer; |
| 31 |
import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger; |
| 32 |
import org.apache.commons.jcs3.log.Log; |
| 33 |
import org.apache.commons.jcs3.log.LogManager; |
| 34 |
import org.apache.commons.jcs3.utils.config.OptionConverter; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class RemoteHttpCacheFactory |
| 43 |
extends AbstractAuxiliaryCacheFactory |
| 44 |
{ |
| 45 |
|
| 46 |
private static final Log log = LogManager.getLog( RemoteHttpCacheFactory.class ); |
| 47 |
|
| 48 |
|
| 49 |
private RemoteHttpCacheMonitor monitor; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
public <K, V> AuxiliaryCache<K, V> createCache( final AuxiliaryCacheAttributes iaca, final ICompositeCacheManager cacheMgr, |
| 66 |
final ICacheEventLogger cacheEventLogger, final IElementSerializer elementSerializer ) |
| 67 |
{ |
| 68 |
final RemoteHttpCacheAttributes rca = (RemoteHttpCacheAttributes) iaca; |
| 69 |
|
| 70 |
|
| 71 |
rca.setRemoteType( RemoteType.LOCAL ); |
| 72 |
|
| 73 |
final RemoteHttpClientListener<K, V> listener = new RemoteHttpClientListener<>( rca, cacheMgr, elementSerializer ); |
| 74 |
|
| 75 |
final IRemoteHttpCacheClient<K, V> remoteService = createRemoteHttpCacheClientForAttributes(rca); |
| 76 |
|
| 77 |
final IRemoteCacheClient<K, V> remoteCacheClient = |
| 78 |
new RemoteHttpCache<>( rca, remoteService, listener, monitor ); |
| 79 |
remoteCacheClient.setCacheEventLogger( cacheEventLogger ); |
| 80 |
remoteCacheClient.setElementSerializer( elementSerializer ); |
| 81 |
|
| 82 |
final RemoteCacheNoWait<K, V> remoteCacheNoWait = new RemoteCacheNoWait<>( remoteCacheClient ); |
| 83 |
remoteCacheNoWait.setCacheEventLogger( cacheEventLogger ); |
| 84 |
remoteCacheNoWait.setElementSerializer( elementSerializer ); |
| 85 |
|
| 86 |
return remoteCacheNoWait; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
protected <V, K> IRemoteHttpCacheClient<K, V> createRemoteHttpCacheClientForAttributes(final RemoteHttpCacheAttributes cattr) |
| 97 |
{ |
| 98 |
IRemoteHttpCacheClient<K, V> remoteService = OptionConverter.instantiateByClassName( cattr |
| 99 |
.getRemoteHttpClientClassName(), null ); |
| 100 |
|
| 101 |
if ( remoteService == null ) |
| 102 |
{ |
| 103 |
log.info( "Creating the default client for {0}", |
| 104 |
cattr::getCacheName); |
| 105 |
remoteService = new RemoteHttpCacheClient<>(); |
| 106 |
} |
| 107 |
|
| 108 |
remoteService.initialize( cattr ); |
| 109 |
return remoteService; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@Override |
| 116 |
public void initialize() |
| 117 |
{ |
| 118 |
super.initialize(); |
| 119 |
monitor = new RemoteHttpCacheMonitor(this); |
| 120 |
monitor.setDaemon(true); |
| 121 |
monitor.start(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@Override |
| 128 |
public void dispose() |
| 129 |
{ |
| 130 |
if (monitor != null) |
| 131 |
{ |
| 132 |
monitor.notifyShutdown(); |
| 133 |
try |
| 134 |
{ |
| 135 |
monitor.join(5000); |
| 136 |
} |
| 137 |
catch (final InterruptedException e) |
| 138 |
{ |
| 139 |
|
| 140 |
} |
| 141 |
monitor = null; |
| 142 |
} |
| 143 |
|
| 144 |
super.dispose(); |
| 145 |
} |
| 146 |
} |