listener = LateralTCPListener.getInstance( attr, cacheMgr );
/** * Increments the put count. Gets the cache that was injected by the lateral factory. Calls put * on the cache. * <p> * @see org.apache.commons.jcs.engine.behavior.ICacheListener#handlePut(org.apache.commons.jcs.engine.behavior.ICacheElement) */ @Override public void handlePut( ICacheElement<K, V> element ) throws IOException { putCnt++; if ( log.isInfoEnabled() ) { if ( getPutCnt() % 100 == 0 ) { log.info( "Put Count (port " + getTcpLateralCacheAttributes().getTcpListenerPort() + ") = " + getPutCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handlePut> cacheName=" + element.getCacheName() + ", key=" + element.getKey() ); } getCache( element.getCacheName() ).localUpdate( element ); }
/** * Gets the instance attribute of the LateralCacheTCPListener class. * <p> * @param ilca ITCPLateralCacheAttributes * @param cacheMgr * @return The instance value */ public synchronized static <K, V> LateralTCPListener<K, V> getInstance( ITCPLateralCacheAttributes ilca, ICompositeCacheManager cacheMgr ) { @SuppressWarnings("unchecked") // Need to cast because of common map for all instances LateralTCPListener<K, V> ins = (LateralTCPListener<K, V>) instances.get( String.valueOf( ilca.getTcpListenerPort() ) ); if ( ins == null ) { ins = new LateralTCPListener<K, V>( ilca ); ins.init(); ins.setCacheManager( cacheMgr ); instances.put( String.valueOf( ilca.getTcpListenerPort() ), ins ); if ( log.isInfoEnabled() ) { log.info( "Created new listener " + ilca.getTcpListenerPort() ); } } return ins; }
/** * Increments the remove count. Gets the cache that was injected by the lateral factory. Calls * remove on the cache. * <p> * @see org.apache.commons.jcs.engine.behavior.ICacheListener#handleRemove(java.lang.String, * Object) */ @Override public void handleRemove( String cacheName, K key ) throws IOException { removeCnt++; if ( log.isInfoEnabled() ) { if ( getRemoveCnt() % 100 == 0 ) { log.info( "Remove Count = " + getRemoveCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handleRemove> cacheName=" + cacheName + ", key=" + key ); } getCache( cacheName ).localRemove( key ); }
/** * Gets the cache that was injected by the lateral factory. Calls get on the cache. * <p> * @param cacheName * @param key * @return a ICacheElement * @throws IOException */ public ICacheElement<K, V> handleGet( String cacheName, K key ) throws IOException { getCnt++; if ( log.isInfoEnabled() ) { if ( getGetCnt() % 100 == 0 ) { log.info( "Get Count (port " + getTcpLateralCacheAttributes().getTcpListenerPort() + ") = " + getGetCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handleGet> cacheName=" + cacheName + ", key = " + key ); } return getCache( cacheName ).localGet( key ); }
/** * Gets the cacheManager attribute of the LateralCacheTCPListener object. * <p> * Normally this is set by the factory. If it wasn't set the listener defaults to the expected * singleton behavior of the cache manager. * <p> * @param name * @return CompositeCache */ protected CompositeCache<K, V> getCache( String name ) { if ( getCacheManager() == null ) { // revert to singleton on failure try { setCacheManager( CompositeCacheManager.getInstance() ); } catch (CacheException e) { throw new RuntimeException("Could not retrieve cache manager instance", e); } if ( log.isDebugEnabled() ) { log.debug( "cacheMgr = " + getCacheManager() ); } } return getCacheManager().getCache( name ); }
LateralTCPListener<String, String> listener = LateralTCPListener.getInstance( lac, cacheMgr ); assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );
/** * Gets the cache that was injected by the lateral factory. Calls getKeySet on the cache. * <p> * @param cacheName the name of the cache * @return a set of keys * @throws IOException */ public Set<K> handleGetKeySet( String cacheName ) throws IOException { return getCache( cacheName ).getKeySet(true); }
/** * This starts the ListenerThread on the specified port. */ @Override public synchronized void init() { try { this.port = getTcpLateralCacheAttributes().getTcpListenerPort(); pooledExecutor = Executors.newCachedThreadPool( new DaemonThreadFactory("JCS-LateralTCPListener-")); terminated = new AtomicBoolean(false); shutdown = new AtomicBoolean(false); log.info( "Listening on port " + port ); ServerSocket serverSocket = new ServerSocket( port ); serverSocket.setSoTimeout( acceptTimeOut ); receiver = new ListenerThread(serverSocket); receiver.setDaemon( true ); receiver.start(); } catch ( IOException ex ) { throw new IllegalStateException( ex ); } }
/** * Gets the cache that was injected by the lateral factory. Calls get on the cache. * <p> * @param cacheName the name of the cache * @param pattern the matching pattern * @return Map * @throws IOException */ public Map<K, ICacheElement<K, V>> handleGetMatching( String cacheName, String pattern ) throws IOException { getCnt++; if ( log.isInfoEnabled() ) { if ( getGetCnt() % 100 == 0 ) { log.info( "GetMatching Count (port " + getTcpLateralCacheAttributes().getTcpListenerPort() + ") = " + getGetCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handleGetMatching> cacheName=" + cacheName + ", pattern = " + pattern ); } return getCache( cacheName ).localGetMatching( pattern ); }
LateralTCPListener<String, String> listener = LateralTCPListener.getInstance( lac, cacheMgr ); assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );
/** * Increments the remove count. Gets the cache that was injected by the lateral factory. Calls * remove on the cache. * <p> * @see org.apache.commons.jcs.engine.behavior.ICacheListener#handleRemove(java.lang.String, * Object) */ @Override public void handleRemove( String cacheName, K key ) throws IOException { removeCnt++; if ( log.isInfoEnabled() ) { if ( getRemoveCnt() % 100 == 0 ) { log.info( "Remove Count = " + getRemoveCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handleRemove> cacheName=" + cacheName + ", key=" + key ); } getCache( cacheName ).localRemove( key ); }
/** * Gets the cacheManager attribute of the LateralCacheTCPListener object. * <p> * Normally this is set by the factory. If it wasn't set the listener defaults to the expected * singleton behavior of the cache manager. * <p> * @param name * @return CompositeCache */ protected CompositeCache<K, V> getCache( String name ) { if ( getCacheManager() == null ) { // revert to singleton on failure try { setCacheManager( CompositeCacheManager.getInstance() ); } catch (CacheException e) { throw new RuntimeException("Could not retrieve cache manager instance", e); } if ( log.isDebugEnabled() ) { log.debug( "cacheMgr = " + getCacheManager() ); } } return getCacheManager().getCache( name ); }
/** * Gets the cache that was injected by the lateral factory. Calls getKeySet on the cache. * <p> * @param cacheName the name of the cache * @return a set of keys * @throws IOException */ public Set<K> handleGetKeySet( String cacheName ) throws IOException { return getCache( cacheName ).getKeySet(true); }
/** * This starts the ListenerThread on the specified port. */ @Override public synchronized void init() { try { this.port = getTcpLateralCacheAttributes().getTcpListenerPort(); pooledExecutor = Executors.newCachedThreadPool( new DaemonThreadFactory("JCS-LateralTCPListener-")); terminated = false; shutdown = false; log.info( "Listening on port " + port ); ServerSocket serverSocket = new ServerSocket( port ); serverSocket.setSoTimeout( acceptTimeOut ); receiver = new ListenerThread(serverSocket); receiver.setDaemon( true ); receiver.start(); } catch ( Exception ex ) { log.error( ex ); throw new IllegalStateException( ex.getMessage() ); } }
/** * Add listener for receivers * <p> * @param iaca cache configuration attributes * @param cacheMgr the composite cache manager */ private void addListenerIfNeeded( ITCPLateralCacheAttributes iaca, ICompositeCacheManager cacheMgr ) { // don't create a listener if we are not receiving. if ( iaca.isReceive() ) { try { addLateralCacheListener( iaca.getCacheName(), LateralTCPListener.getInstance( iaca, cacheMgr ) ); } catch ( IOException ioe ) { log.error( "Problem creating lateral listener", ioe ); } } else { if ( log.isDebugEnabled() ) { log.debug( "Not creating a listener since we are not receiving." ); } } }
/** * Gets the cache that was injected by the lateral factory. Calls get on the cache. * <p> * @param cacheName * @param key * @return a ICacheElement * @throws IOException */ public ICacheElement<K, V> handleGet( String cacheName, K key ) throws IOException { getCnt++; if ( log.isInfoEnabled() ) { if ( getGetCnt() % 100 == 0 ) { log.info( "Get Count (port " + getTcpLateralCacheAttributes().getTcpListenerPort() + ") = " + getGetCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handleGet> cacheName=" + cacheName + ", key = " + key ); } return getCache( cacheName ).localGet( key ); }
/** * Increments the put count. Gets the cache that was injected by the lateral factory. Calls put * on the cache. * <p> * @see org.apache.commons.jcs.engine.behavior.ICacheListener#handlePut(org.apache.commons.jcs.engine.behavior.ICacheElement) */ @Override public void handlePut( ICacheElement<K, V> element ) throws IOException { putCnt++; if ( log.isInfoEnabled() ) { if ( getPutCnt() % 100 == 0 ) { log.info( "Put Count (port " + getTcpLateralCacheAttributes().getTcpListenerPort() + ") = " + getPutCnt() ); } } if ( log.isDebugEnabled() ) { log.debug( "handlePut> cacheName=" + element.getCacheName() + ", key=" + element.getKey() ); } getCache( element.getCacheName() ).localUpdate( element ); }
/** * Gets the instance attribute of the LateralCacheTCPListener class. * <p> * @param ilca ITCPLateralCacheAttributes * @param cacheMgr * @return The instance value */ public synchronized static <K, V> LateralTCPListener<K, V> getInstance( ITCPLateralCacheAttributes ilca, ICompositeCacheManager cacheMgr ) { @SuppressWarnings("unchecked") // Need to cast because of common map for all instances LateralTCPListener<K, V> ins = (LateralTCPListener<K, V>) instances.get( String.valueOf( ilca.getTcpListenerPort() ) ); if ( ins == null ) { ins = new LateralTCPListener<K, V>( ilca ); ins.init(); ins.setCacheManager( cacheMgr ); instances.put( String.valueOf( ilca.getTcpListenerPort() ), ins ); if ( log.isInfoEnabled() ) { log.info( "Created new listener " + ilca.getTcpListenerPort() ); } } return ins; }
/** * Gets the cache that was injected by the lateral factory. Calls removeAll on the cache. * <p> * @see org.apache.commons.jcs.engine.behavior.ICacheListener#handleRemoveAll(java.lang.String) */ @Override public void handleRemoveAll( String cacheName ) throws IOException { if ( log.isDebugEnabled() ) { log.debug( "handleRemoveAll> cacheName=" + cacheName ); } getCache( cacheName ).localRemoveAll(); }