org.apache.jcs.auxiliary.lateral.socket.tcp
Class LateralTCPListener<K extends Serializable,V extends Serializable>

java.lang.Object
  extended by org.apache.jcs.auxiliary.lateral.socket.tcp.LateralTCPListener<K,V>
All Implemented Interfaces:
Serializable, ILateralCacheListener<K,V>, ICacheListener<K,V>, IShutdownObserver

public class LateralTCPListener<K extends Serializable,V extends Serializable>
extends Object
implements ILateralCacheListener<K,V>, Serializable, IShutdownObserver

Listens for connections from other TCP lateral caches and handles them. The initialization method starts a listening thread, which creates a socket server. When messages are received they are passed to a pooled executor which then calls the appropriate handle method.

See Also:
Serialized Form

Nested Class Summary
 class LateralTCPListener.ConnectionHandler
          A Separate thread that runs when a command comes into the LateralTCPReceiver.
 class LateralTCPListener.ListenerThread
          Processes commands from the server socket.
protected static class LateralTCPListener.MyThreadFactory
          Allows us to set the daemon status on the executor threads
 
Field Summary
protected static HashMap<String,ILateralCacheListener<?,?>> instances
          Map of available instances, keyed by port
protected static org.apache.commons.logging.Log log
          The logger
protected  ExecutorService pooledExecutor
          The processor.
protected  int port
          Listening port
protected  boolean shutdown
          is this shut down?
protected  boolean terminated
          is this terminated?
 
Constructor Summary
protected LateralTCPListener(ITCPLateralCacheAttributes ilca)
          Only need one since it does work for all regions, just reference by multiple region names.
 
Method Summary
 void dispose()
          Dispose this listener
protected  CompositeCache<K,V> getCache(String name)
          Gets the cacheManager attribute of the LateralCacheTCPListener object.
 ICompositeCacheManager getCacheManager()
           
 int getGetCnt()
           
static
<K extends Serializable,V extends Serializable>
LateralTCPListener<K,V>
getInstance(ITCPLateralCacheAttributes ilca, ICompositeCacheManager cacheMgr)
          Gets the instance attribute of the LateralCacheTCPListener class.
 long getListenerId()
          Gets the listenerId attribute of the LateralCacheTCPListener object
 int getPutCnt()
          This is roughly the number of updates the lateral has received.
 int getRemoveCnt()
           
 ITCPLateralCacheAttributes getTcpLateralCacheAttributes()
           
 void handleDispose(String cacheName)
          This marks this instance as terminated.
 ICacheElement<K,V> handleGet(String cacheName, K key)
          Gets the cache that was injected by the lateral factory.
 Set<K> handleGetGroupKeys(String cacheName, String group)
          Gets the cache that was injected by the lateral factory.
 Set<String> handleGetGroupNames(String cacheName)
          Gets the cache that was injected by the lateral factory.
 Map<K,ICacheElement<K,V>> handleGetMatching(String cacheName, String pattern)
          Gets the cache that was injected by the lateral factory.
 void handlePut(ICacheElement<K,V> element)
          Increments the put count.
 void handleRemove(String cacheName, K key)
          Increments the remove count.
 void handleRemoveAll(String cacheName)
          Gets the cache that was injected by the lateral factory.
 void init()
          This starts the ListenerThread on the specified port.
 void setCacheManager(ICompositeCacheManager cacheMgr)
           
 void setListenerId(long id)
          Let the lateral cache set a listener_id.
 void setTcpLateralCacheAttributes(ITCPLateralCacheAttributes tcpLateralCacheAttributes)
           
 void shutdown()
          Shuts down the receiver.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
The logger


instances

protected static final HashMap<String,ILateralCacheListener<?,?>> instances
Map of available instances, keyed by port


port

protected int port
Listening port


pooledExecutor

protected ExecutorService pooledExecutor
The processor. We should probably use an event queue here.


shutdown

protected boolean shutdown
is this shut down?


terminated

protected boolean terminated
is this terminated?

Constructor Detail

LateralTCPListener

protected LateralTCPListener(ITCPLateralCacheAttributes ilca)
Only need one since it does work for all regions, just reference by multiple region names.

Parameters:
ilca -
Method Detail

getInstance

public static <K extends Serializable,V extends Serializable> LateralTCPListener<K,V> getInstance(ITCPLateralCacheAttributes ilca,
                                                                                                  ICompositeCacheManager cacheMgr)
Gets the instance attribute of the LateralCacheTCPListener class.

Parameters:
ilca - ITCPLateralCacheAttributes
cacheMgr -
Returns:
The instance value

init

public void init()
This starts the ListenerThread on the specified port.

Specified by:
init in interface ILateralCacheListener<K extends Serializable,V extends Serializable>

setListenerId

public void setListenerId(long id)
                   throws IOException
Let the lateral cache set a listener_id. Since there is only one listener for all the regions and every region gets registered? the id shouldn't be set if it isn't zero. If it is we assume that it is a reconnect.

By default, the listener id is the vmid.

The service should set this value. This value will never be changed by a server we connect to. It needs to be non static, for unit tests.

The service will use the value it sets in all send requests to the sender.

Specified by:
setListenerId in interface ICacheListener<K extends Serializable,V extends Serializable>
Parameters:
id - The new listenerId value
Throws:
IOException

getListenerId

public long getListenerId()
                   throws IOException
Gets the listenerId attribute of the LateralCacheTCPListener object

Specified by:
getListenerId in interface ICacheListener<K extends Serializable,V extends Serializable>
Returns:
The listenerId value
Throws:
IOException

handlePut

public void handlePut(ICacheElement<K,V> element)
               throws IOException
Increments the put count. Gets the cache that was injected by the lateral factory. Calls put on the cache.

Specified by:
handlePut in interface ICacheListener<K extends Serializable,V extends Serializable>
Throws:
IOException
See Also:
ICacheListener.handlePut(org.apache.jcs.engine.behavior.ICacheElement)

handleRemove

public void handleRemove(String cacheName,
                         K key)
                  throws IOException
Increments the remove count. Gets the cache that was injected by the lateral factory. Calls remove on the cache.

Specified by:
handleRemove in interface ICacheListener<K extends Serializable,V extends Serializable>
Throws:
IOException
See Also:
ICacheListener.handleRemove(java.lang.String, java.io.Serializable)

handleRemoveAll

public void handleRemoveAll(String cacheName)
                     throws IOException
Gets the cache that was injected by the lateral factory. Calls removeAll on the cache.

Specified by:
handleRemoveAll in interface ICacheListener<K extends Serializable,V extends Serializable>
Throws:
IOException
See Also:
ICacheListener.handleRemoveAll(java.lang.String)

handleGet

public ICacheElement<K,V> handleGet(String cacheName,
                                    K key)
                                                                       throws IOException
Gets the cache that was injected by the lateral factory. Calls get on the cache.

Parameters:
cacheName -
key -
Returns:
a ICacheElement
Throws:
IOException

handleGetMatching

public Map<K,ICacheElement<K,V>> handleGetMatching(String cacheName,
                                                   String pattern)
                                                                                                           throws IOException
Gets the cache that was injected by the lateral factory. Calls get on the cache.

Parameters:
cacheName - the name of the cache
pattern - the matching pattern
Returns:
Map
Throws:
IOException

handleGetGroupKeys

public Set<K> handleGetGroupKeys(String cacheName,
                                 String group)
                                               throws IOException
Gets the cache that was injected by the lateral factory. Calls getGroupKeys on the cache.

Parameters:
cacheName - the name of the cache
group - the group name
Returns:
a set of keys
Throws:
IOException

handleGetGroupNames

public Set<String> handleGetGroupNames(String cacheName)
                                throws IOException
Gets the cache that was injected by the lateral factory. Calls getGroupNames on the cache.

Parameters:
cacheName - the name of the cache
Returns:
a set of group names
Throws:
IOException

handleDispose

public void handleDispose(String cacheName)
                   throws IOException
This marks this instance as terminated.

Specified by:
handleDispose in interface ICacheListener<K extends Serializable,V extends Serializable>
Throws:
IOException
See Also:
ICacheListener.handleDispose(java.lang.String)

dispose

public void dispose()
Description copied from interface: ILateralCacheListener
Dispose this listener

Specified by:
dispose in interface ILateralCacheListener<K extends Serializable,V extends Serializable>

getCache

protected CompositeCache<K,V> getCache(String name)
Gets the cacheManager attribute of the LateralCacheTCPListener object.

Normally this is set by the factory. If it wasn't set the listener defaults to the expected singleton behavior of the cache manager.

Parameters:
name -
Returns:
CompositeCache

getPutCnt

public int getPutCnt()
This is roughly the number of updates the lateral has received.

Returns:
Returns the putCnt.

getGetCnt

public int getGetCnt()
Returns:
Returns the getCnt.

getRemoveCnt

public int getRemoveCnt()
Returns:
Returns the removeCnt.

setCacheManager

public void setCacheManager(ICompositeCacheManager cacheMgr)
Specified by:
setCacheManager in interface ILateralCacheListener<K extends Serializable,V extends Serializable>
Parameters:
cacheMgr - The cacheMgr to set.

getCacheManager

public ICompositeCacheManager getCacheManager()
Specified by:
getCacheManager in interface ILateralCacheListener<K extends Serializable,V extends Serializable>
Returns:
Returns the cacheMgr.

setTcpLateralCacheAttributes

public void setTcpLateralCacheAttributes(ITCPLateralCacheAttributes tcpLateralCacheAttributes)
Parameters:
tcpLateralCacheAttributes - The tcpLateralCacheAttributes to set.

getTcpLateralCacheAttributes

public ITCPLateralCacheAttributes getTcpLateralCacheAttributes()
Returns:
Returns the tcpLateralCacheAttributes.

shutdown

public void shutdown()
Shuts down the receiver.

Specified by:
shutdown in interface IShutdownObserver


Copyright © 2002-2013 The Apache Software Foundation. All Rights Reserved.