/** * Fixes up all the caches managed by this cache manager. */ public void fixCaches() { if ( !canFix ) { return; } if ( log.isInfoEnabled() ) { log.info( "Fixing caches. ICacheServiceNonLocal " + remoteService + " | IRemoteCacheObserver " + remoteWatch ); } for (RemoteCacheNoWait<?, ?> c : caches.values()) { if (c.getStatus() == CacheStatus.ERROR) { c.fixCache( remoteService ); } } if ( log.isInfoEnabled() ) { String msg = "Remote connection to " + registry + " resumed."; if ( cacheEventLogger != null ) { cacheEventLogger.logApplicationEvent( "RemoteCacheManager", "fix", msg ); } log.info( msg ); } }
/** * Synchronously reads from the remote cache. * <p> * @param key * @return Either an ICacheElement<K, V> or null if it is not found. */ @Override public ICacheElement<K, V> get( K key ) { for (RemoteCacheNoWait<K, V> nw : noWaits) { try { ICacheElement<K, V> obj = nw.get( key ); if ( obj != null ) { return obj; } } catch ( IOException ex ) { log.debug( "Failed to get." ); return null; } } return null; }
/** * Gets multiple items from the cache based on the given set of keys. * <p> * @param keys * @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no * data in cache for any of these keys */ @Override public Map<K, ICacheElement<K, V>> getMultiple( Set<K> keys ) { if ( keys != null && !keys.isEmpty() ) { for ( int i = 0; i < noWaits.length; i++ ) { try { return noWaits[i].getMultiple( keys ); } catch ( IOException ex ) { log.debug( "Failed to get." ); } } } return Collections.emptyMap(); }
private void removeListenerFromCache(RemoteCacheNoWait<?, ?> cache) throws IOException { IRemoteCacheClient<?, ?> rc = cache.getRemoteCache(); if ( log.isDebugEnabled() ) { log.debug( "Found cache for [" + cache.getCacheName() + "], deregistering listener." ); } // could also store the listener for a server in the manager. IRemoteCacheListener<?, ?> listener = rc.getListener(); remoteWatch.removeCacheListener( cache.getCacheName(), listener ); }
/** Shutdown all. */ public void release() { cacheLock.lock(); try { for (RemoteCacheNoWait<?, ?> c : caches.values()) { try { if ( log.isInfoEnabled() ) { log.info( "freeCache [" + c.getCacheName() + "]" ); } removeListenerFromCache(c); c.dispose(); } catch ( IOException ex ) { log.error( "Problem releasing " + c.getCacheName(), ex ); } } caches.clear(); } finally { cacheLock.unlock(); } }
RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); noWait.update( element ); SleepUtil.sleepAtLeast( 10 ); ICacheEventQueue<String, String> originalQueue = noWait.getCacheEventQueue(); noWait.fixCache( service ); noWait.update( element ); SleepUtil.sleepAtLeast( 10 ); ICacheEventQueue<String, String> newQueue = noWait.getCacheEventQueue();
remoteCacheClient.setElementSerializer( elementSerializer ); remoteCacheNoWait = new RemoteCacheNoWait<K, V>( remoteCacheClient ); remoteCacheNoWait.setCacheEventLogger( cacheEventLogger ); remoteCacheNoWait.setElementSerializer( elementSerializer );
/** * Gets the status attribute of the RemoteCacheNoWaitFacade object * <p> * Return ALIVE if any are alive. * <p> * @return The status value */ @Override public CacheStatus getStatus() { for (RemoteCacheNoWait<K, V> nw : noWaits) { if ( nw.getStatus() == CacheStatus.ALIVE ) { return CacheStatus.ALIVE; } } return CacheStatus.DISPOSED; }
/** * Simply verify that the client status is returned in the stats. * <p> * @throws Exception */ public void testGetStats() throws Exception { // SETUP MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>(); client.status = CacheStatus.ALIVE; RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); // DO WORK String result = noWait.getStats(); // VERIFY assertTrue( "Status should contain 'ALIVE'", result.indexOf( "ALIVE" ) != -1 ); }
/** * Simply verify that we get a status of error if the cache is in error.. * <p> * @throws Exception */ public void testGetStatus_error() throws Exception { // SETUP MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>(); client.status = CacheStatus.ERROR; RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); // DO WORK CacheStatus result = noWait.getStatus(); // VERIFY assertEquals( "Wrong status", CacheStatus.ERROR, result ); }
/** * Simply verify that the client get is called from the no wait. * <p> * @throws Exception */ public void testGet() throws Exception { // SETUP MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>(); RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); ICacheElement<String, String> input = new CacheElement<String, String>( "testUpdate", "key", "value" ); client.getSetupMap.put( "key", input ); // DO WORK ICacheElement<String, String> result = noWait.get( "key" ); // VERIFY assertEquals( "Wrong element", input, result ); }
/** * Simply verify that the client gets updated via the no wait. * <p> * @throws Exception */ public void testUpdate() throws Exception { // SETUP MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>(); RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); ICacheElement<String, String> element = new CacheElement<String, String>( "testUpdate", "key", "value" ); // DO WORK noWait.update( element ); // VERIFY SleepUtil.sleepAtLeast( 10 ); assertEquals( "Wrong number updated.", 1, client.updateList.size() ); assertEquals( "Wrong element", element, client.updateList.get( 0 ) ); }
/** * Simply verify that the client gets updated via the no wait. * <p> * @throws Exception */ public void testRemove() throws Exception { // SETUP MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>(); RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); String input = "MyKey"; // DO WORK noWait.remove( input ); SleepUtil.sleepAtLeast( 10 ); // VERIFY assertEquals( "Wrong number updated.", 1, client.removeList.size() ); assertEquals( "Wrong key", input, client.removeList.get( 0 ) ); }
/** * Simply verify that the client getMultiple is called from the no wait. * <p> * @throws Exception */ public void testGetMultiple() throws Exception { // SETUP MockRemoteCacheClient<String, String> client = new MockRemoteCacheClient<String, String>(); RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); ICacheElement<String, String> inputElement = new CacheElement<String, String>( "testUpdate", "key", "value" ); Map<String, ICacheElement<String, String>> inputMap = new HashMap<String, ICacheElement<String,String>>(); inputMap.put( "key", inputElement ); Set<String> keys = new HashSet<String>(); keys.add( "key" ); client.getMultipleSetupMap.put( keys, inputMap ); // DO WORK Map<String, ICacheElement<String, String>> result = noWait.getMultiple( keys ); // VERIFY assertEquals( "elements map", inputMap, result ); }
/** * Verify that we can add an item. */ public void testAddNoWait_InList() { // SETUP List<RemoteCacheNoWait<String, String>> noWaits = new ArrayList<RemoteCacheNoWait<String,String>>(); IRemoteCacheAttributes cattr = new RemoteCacheAttributes(); cattr.setCacheName( "testCache1" ); RemoteCache<String, String> client = new RemoteCache<String, String>(cattr, null, null, null); RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<String, String>( client ); noWaits.add( noWait ); RemoteCacheNoWaitFacade<String, String> facade = new RemoteCacheNoWaitFacade<String, String>(noWaits, cattr, null, null, null ); // VERIFY assertEquals( "Should have one entry.", 1, facade.noWaits.size() ); assertTrue( "Should be in the list.", facade.noWaits.contains( noWait ) ); assertSame( "Should have same facade.", facade, ((RemoteCache<String, String>)facade.noWaits.get(0).getRemoteCache()).getFacade() ); } }
/** * Stops a listener. This is used to deregister a failover after primary reconnection. * <p> * @param cacheName * @throws IOException */ public void removeRemoteCacheListener( String cacheName ) throws IOException { synchronized ( caches ) { RemoteCacheNoWait<?, ?> cache = caches.get( cacheName ); if ( cache != null ) { IRemoteCacheClient<?, ?> rc = cache.getRemoteCache(); if ( log.isDebugEnabled() ) { log.debug( "Found cache for [" + cacheName + "], deregistering listener." ); } // could also store the listener for a server in the manager. IRemoteCacheListener<?, ?> listener = rc.getListener(); remoteWatch.removeCacheListener( cacheName, listener ); } } }
/** * Returns the stats and the cache.toString(). * <p> * (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return getStats() + "\n" + remoteCacheClient.toString(); }
/** * Adds a remove request to the remote cache. * <p> * @param key * @return whether or not it was removed, right now it return false. */ @Override public boolean remove( K key ) { try { for ( int i = 0; i < noWaits.length; i++ ) { noWaits[i].remove( key ); } } catch ( Exception ex ) { log.error( ex ); } return false; }
/** * Fixes up all the caches managed by this cache manager. * <p> * @param remoteService * @param remoteWatch */ public void fixCaches( ICacheServiceNonLocal<?, ?> remoteService, ICacheObserver remoteWatch ) { if ( log.isInfoEnabled() ) { log.info( "Fixing caches. ICacheServiceNonLocal " + remoteService + " | IRemoteCacheObserver " + remoteWatch ); } synchronized ( caches ) { this.remoteService = remoteService; this.remoteWatch.setCacheWatch( remoteWatch ); for (RemoteCacheNoWait<?, ?> c : caches.values()) { c.fixCache( remoteService ); } } }