public class RMICacheManagerPeerListener extends java.lang.Object implements CacheManagerPeerListener
if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); }Doing so would require the addition of
grant
statements in the java.policy
file.
If no security manager is specified, then no class loading, either by RMI clients or servers, is allowed, aside from
what can be found in the local CLASSPATH. The classpath of each instance of this class should have
all required classes to enable distribution, so no remote classloading is required or desirable. Accordingly,
no security manager is set and there are no special JVM configuration requirements.
This class opens a ServerSocket. The dispose method should be called for orderly closure of that socket. This class
has a shutdown hook which calls dispose() as a convenience feature for developers.Modifier and Type | Field and Description |
---|---|
private CacheManager |
cacheManager |
protected java.util.Map |
cachePeers
The cache peers.
|
private java.lang.String |
hostName |
private static org.slf4j.Logger |
LOG |
private static int |
MINIMUM_SENSIBLE_TIMEOUT |
private static int |
NAMING_UNBIND_MAX_RETRIES |
private static int |
NAMING_UNBIND_RETRY_INTERVAL |
protected java.lang.Integer |
port
The RMI listener port
|
private java.rmi.registry.Registry |
registry |
private boolean |
registryCreated |
private java.lang.Integer |
remoteObjectPort |
private java.lang.Integer |
socketTimeoutMillis |
protected Status |
status
status.
|
Constructor and Description |
---|
RMICacheManagerPeerListener(java.lang.String hostName,
java.lang.Integer port,
java.lang.Integer remoteObjectPort,
CacheManager cacheManager,
java.lang.Integer socketTimeoutMillis)
Constructor with full arguments.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addCachePeer(java.lang.String name,
RMICachePeer peer)
Package local method for testing
|
protected void |
assignFreePort(boolean forced)
Assigns a free port to be the listener port.
|
void |
attemptResolutionOfUniqueResourceConflict()
If a conflict is detected in unique resource use, this method signals the listener to attempt
automatic resolution of the resource conflict.
|
protected void |
bind(java.lang.String peerName,
RMICachePeer rmiCachePeer)
Bind a cache peer
|
protected java.lang.String |
calculateHostAddress()
Calculates the host address as the default NICs IP address
|
void |
dispose()
Stop the listener.
|
protected void |
disposeRMICachePeer(RMICachePeer rmiCachePeer)
A template method to dispose an individual RMICachePeer.
|
java.util.List |
getBoundCachePeers()
All of the caches which are listening for remote changes.
|
protected int |
getFreePort()
Gets a free server socket port.
|
java.lang.String |
getScheme()
The replication scheme this listener interacts with.
|
Status |
getStatus()
Returns the listener status.
|
java.lang.String |
getUniqueResourceIdentifier()
A listener will normally have a resource that only one instance can use at the same time,
such as a port.
|
void |
init()
Call to start the listeners and do any other required initialisation.
|
protected boolean |
isDistributed(Ehcache cache)
Determine if the given cache is distributed.
|
protected java.lang.String[] |
listBoundRMICachePeers()
Returns a list of bound objects.
|
protected java.rmi.Remote |
lookupPeer(java.lang.String name)
Returns a reference to the remote object.
|
void |
notifyCacheAdded(java.lang.String cacheName)
Called immediately after a cache has been added and activated.
|
void |
notifyCacheRemoved(java.lang.String cacheName)
Called immediately after a cache has been disposed and removed.
|
protected void |
populateListOfRemoteCachePeers()
Should be called on init because this is one of the last things that should happen on CacheManager startup.
|
protected void |
startRegistry()
Start the rmiregistry.
|
protected void |
stopRegistry()
Stop the rmiregistry if it was started by this class.
|
protected void |
unbind(RMICachePeer rmiCachePeer)
Unbinds an RMICachePeer and unexports it.
|
private static final org.slf4j.Logger LOG
private static final int MINIMUM_SENSIBLE_TIMEOUT
private static final int NAMING_UNBIND_RETRY_INTERVAL
private static final int NAMING_UNBIND_MAX_RETRIES
protected final java.util.Map cachePeers
protected Status status
protected java.lang.Integer port
private java.rmi.registry.Registry registry
private boolean registryCreated
private final java.lang.String hostName
private CacheManager cacheManager
private java.lang.Integer socketTimeoutMillis
private java.lang.Integer remoteObjectPort
public RMICacheManagerPeerListener(java.lang.String hostName, java.lang.Integer port, java.lang.Integer remoteObjectPort, CacheManager cacheManager, java.lang.Integer socketTimeoutMillis) throws java.net.UnknownHostException
hostName
- may be null, in which case the hostName will be looked up. Machines with multiple
interfaces should specify this if they do not want it to be the default NIC.port
- a port in the range 1025 - 65536remoteObjectPort
- the port number on which the remote objects bound in the registry receive calls.
This defaults to a free port if not specified.cacheManager
- the CacheManager this listener belongs tosocketTimeoutMillis
- TCP/IP Socket timeout when waiting on responsejava.net.UnknownHostException
protected void assignFreePort(boolean forced) throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the statis of the listener is not Status.STATUS_UNINITIALISED
protected java.lang.String calculateHostAddress() throws java.net.UnknownHostException
java.net.UnknownHostException
protected int getFreePort() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public void init() throws CacheException
init
in interface CacheManagerEventListener
CacheException
- - all exceptions are wrapped in CacheExceptionprotected void bind(java.lang.String peerName, RMICachePeer rmiCachePeer) throws java.lang.Exception
rmiCachePeer
- java.lang.Exception
protected java.lang.String[] listBoundRMICachePeers() throws CacheException
RMICachePeer
objectsCacheException
protected java.rmi.Remote lookupPeer(java.lang.String name) throws CacheException
name
- the name of the cache e.g. sampleCache1
CacheException
protected void populateListOfRemoteCachePeers() throws java.rmi.RemoteException
java.rmi.RemoteException
protected boolean isDistributed(Ehcache cache)
cache
- the cache to checkCacheReplicator
is found in the listenersprotected void startRegistry() throws java.rmi.RemoteException
rmiregistry
binary, in which case:
java.rmi.RemoteException
protected void stopRegistry() throws java.rmi.RemoteException
java.rmi.RemoteException
public void dispose() throws CacheException
dispose
in interface CacheManagerEventListener
CacheException
- - all exceptions are wrapped in CacheExceptionprotected void disposeRMICachePeer(RMICachePeer rmiCachePeer) throws java.lang.Exception
rmiCachePeer
- the cache peer to dispose ofjava.lang.Exception
- thrown if something goes wrongprotected void unbind(RMICachePeer rmiCachePeer) throws java.lang.Exception
rmiCachePeer
- the bound and exported cache peerjava.lang.Exception
public java.util.List getBoundCachePeers()
getBoundCachePeers
in interface CacheManagerPeerListener
RMICachePeer
objects. The list if not livepublic Status getStatus()
getStatus
in interface CacheManagerEventListener
public java.lang.String getUniqueResourceIdentifier()
getUniqueResourceIdentifier
in interface CacheManagerPeerListener
public void attemptResolutionOfUniqueResourceConflict() throws java.lang.IllegalStateException, CacheException
attemptResolutionOfUniqueResourceConflict
in interface CacheManagerPeerListener
java.lang.IllegalStateException
- if the statis of the listener is not Status.STATUS_UNINITIALISED
CacheException
public java.lang.String getScheme()
getScheme
in interface CacheManagerPeerListener
public void notifyCacheAdded(java.lang.String cacheName) throws CacheException
Status.STATUS_UNINITIALISED
to Status.STATUS_ALIVE
. Care should be
taken on processing that notification because:
CacheManager.getCache(String)
will cause a deadlock.
notifyCacheAdded
in interface CacheManagerEventListener
cacheName
- the name of the Cache
the operation relates toCacheException
CacheEventListener
public void notifyCacheRemoved(java.lang.String cacheName)
CacheEventListener
status changed will also be triggered. Any attempt from that notification
to access CacheManager will also result in a deadlock.notifyCacheRemoved
in interface CacheManagerEventListener
cacheName
- the name of the Cache
the operation relates tovoid addCachePeer(java.lang.String name, RMICachePeer peer)