org.apache.jcs.auxiliary.remote.server
Class RemoteCacheServer<K extends Serializable,V extends Serializable>

java.lang.Object
  extended by java.rmi.server.RemoteObject
      extended by java.rmi.server.RemoteServer
          extended by java.rmi.server.UnicastRemoteObject
              extended by org.apache.jcs.auxiliary.remote.server.RemoteCacheServer<K,V>
All Implemented Interfaces:
Serializable, Remote, Unreferenced, IRemoteCacheObserver, IRemoteCacheServiceAdmin, ICacheObserver, ICacheService<K,V>, ICacheServiceAdmin, ICacheServiceNonLocal<K,V>

public class RemoteCacheServer<K extends Serializable,V extends Serializable>
extends UnicastRemoteObject
implements ICacheServiceNonLocal<K,V>, IRemoteCacheObserver, IRemoteCacheServiceAdmin, Unreferenced

This class provides remote cache services. The remote cache server propagates events from local caches to other local caches. It can also store cached data, making it available to new clients.

Remote cache servers can be clustered. If the cache used by this remote cache is configured to use a remote cache of type cluster, the two remote caches will communicate with each other. Remote and put requests can be sent from one remote to another. If they are configured to broadcast such event to their client, then remove an puts can be sent to all locals in the cluster.

Get requests are made between clustered servers if AllowClusterGet is true. You can setup several clients to use one remote server and several to use another. The get local will be distributed between the two servers. Since caches are usually high get and low put, this should allow you to scale.

See Also:
Serialized Form

Field Summary
protected  IRemoteCacheServerAttributes remoteCacheServerAttributes
          Configuration settings.
protected static boolean timing
          timing -- if we should record operation times.
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Constructor Summary
protected RemoteCacheServer(IRemoteCacheServerAttributes rcsa)
          Constructor for the RemoteCacheServer object.
protected RemoteCacheServer(IRemoteCacheServerAttributes rcsa, RMISocketFactory customRMISocketFactory)
          Constructor for the RemoteCacheServer object.
 
Method Summary
<KK extends Serializable,VV extends Serializable>
void
addCacheListener(ICacheListener<KK,VV> listener)
          Subscribes to all remote caches.
<KK extends Serializable,VV extends Serializable>
void
addCacheListener(String cacheName, ICacheListener<KK,VV> listener)
          Subscribes to the specified remote cache.
 void dispose(String cacheName)
          Frees the specified remote cache.
 void dispose(String cacheName, long requesterId)
          Frees the specified remote cache.
 ICacheElement<K,V> get(String cacheName, K key)
          Returns a cache value from the specified remote cache; or null if the cache or key does not exist.
 ICacheElement<K,V> get(String cacheName, K key, long requesterId)
          Returns a cache bean from the specified cache; or null if the key does not exist.
protected  CacheListeners<K,V> getCacheListeners(String cacheName)
          Returns the cache listener for the specified cache.
protected  CacheListeners<K,V> getClusterListeners(String cacheName)
          Gets the clusterListeners attribute of the RemoteCacheServer object.
protected  String getExtraInfoForRequesterId(long requesterId)
          Ip address for the client, if one is stored.
 Set<K> getGroupKeys(String cacheName, String group)
          Gets the set of keys of objects currently in the group.
 Set<String> getGroupNames(String cacheName)
          Gets the set of group names currently in the cache.
 Map<K,ICacheElement<K,V>> getMatching(String cacheName, String pattern)
          Gets all matching items.
 Map<K,ICacheElement<K,V>> getMatching(String cacheName, String pattern, long requesterId)
          Retrieves all matching keys.
 Map<K,ICacheElement<K,V>> getMultiple(String cacheName, Set<K> keys)
          Gets multiple items from the cache based on the given set of keys.
 Map<K,ICacheElement<K,V>> getMultiple(String cacheName, Set<K> keys, long requesterId)
          Gets multiple items from the cache based on the given set of keys.
protected  int getPutCount()
          How many put events have we received.
 String getStats()
          Gets the stats attribute of the RemoteCacheServer object.
protected  void logApplicationEvent(String source, String eventName, String optionalDetails)
          Logs an event if an event logger is configured.
protected
<T extends Serializable>
void
logICacheEvent(ICacheEvent<T> cacheEvent)
          Logs an event if an event logger is configured.
protected  Set<K> processGetGroupKeys(String cacheName, String group)
          Gets the set of keys of objects currently in the group.
protected  Set<String> processGetGroupNames(String cacheName)
          Gets the set of group names currently in the cache.
protected  Map<K,ICacheElement<K,V>> processGetMatching(String cacheName, String pattern, long requesterId)
          Retrieves all matching keys.
 void put(ICacheElement<K,V> item)
          Puts a cache bean to the remote cache and notifies all listeners which
have a different listener id than the originating host; are currently subscribed to the related cache.
 void release()
          Frees all remote caches.
 void remove(String cacheName, K key)
          Removes the given key from the specified remote cache.
 void remove(String cacheName, K key, long requesterId)
          Remove the key from the cache region and don't tell the source listener about it.
 void removeAll(String cacheName)
          Remove all keys from the specified remote cache.
 void removeAll(String cacheName, long requesterId)
          Remove all keys from the specified remote cache.
<KK extends Serializable,VV extends Serializable>
void
removeCacheListener(ICacheListener<KK,VV> listener)
          Unsubscribes from all remote caches.
<KK extends Serializable,VV extends Serializable>
void
removeCacheListener(String cacheName, ICacheListener<KK,VV> listener)
          Unsubscribe this listener from this region.
 void removeCacheListener(String cacheName, long listenerId)
          Unsubscribe this listener from this region.
 void setCacheEventLogger(ICacheEventLogger cacheEventLogger)
          Allows it to be injected.
 void shutdown()
          Shuts down the remote server.
 void shutdown(String host, int port)
          Shuts down a server at a particular host and port.
 void unreferenced()
          Called by the RMI runtime sometime after the runtime determines that the reference list, the list of clients referencing the remote object, becomes empty.
 void update(ICacheElement<K,V> item)
          Puts a cache item to the cache.
 void update(ICacheElement<K,V> item, long requesterId)
          The internal processing is wrapped in event logging calls.
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

timing

protected static final boolean timing
timing -- if we should record operation times.

See Also:
Constant Field Values

remoteCacheServerAttributes

protected IRemoteCacheServerAttributes remoteCacheServerAttributes
Configuration settings.

Constructor Detail

RemoteCacheServer

protected RemoteCacheServer(IRemoteCacheServerAttributes rcsa)
                     throws RemoteException
Constructor for the RemoteCacheServer object. This initializes the server with the values from the config file.

Parameters:
rcsa -
Throws:
RemoteException

RemoteCacheServer

protected RemoteCacheServer(IRemoteCacheServerAttributes rcsa,
                            RMISocketFactory customRMISocketFactory)
                     throws RemoteException
Constructor for the RemoteCacheServer object. This initializes the server with the values from the config file.

Parameters:
rcsa -
customRMISocketFactory -
Throws:
RemoteException
Method Detail

put

public void put(ICacheElement<K,V> item)
         throws IOException
Puts a cache bean to the remote cache and notifies all listeners which
  1. have a different listener id than the originating host;
  2. are currently subscribed to the related cache.

Parameters:
item -
Throws:
IOException

update

public void update(ICacheElement<K,V> item)
            throws IOException
Description copied from interface: ICacheService
Puts a cache item to the cache.

Specified by:
update in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
item -
Throws:
IOException

update

public void update(ICacheElement<K,V> item,
                   long requesterId)
            throws IOException
The internal processing is wrapped in event logging calls.

Specified by:
update in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
item -
requesterId -
Throws:
IOException

get

public ICacheElement<K,V> get(String cacheName,
                              K key)
                                                                 throws IOException
Returns a cache value from the specified remote cache; or null if the cache or key does not exist.

Specified by:
get in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
key -
Returns:
ICacheElement
Throws:
IOException

get

public ICacheElement<K,V> get(String cacheName,
                              K key,
                              long requesterId)
                                                                 throws IOException
Returns a cache bean from the specified cache; or null if the key does not exist.

Adding the requestor id, allows the cache to determine the source of the get.

The internal processing is wrapped in event logging calls.

Specified by:
get in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
key -
requesterId -
Returns:
ICacheElement
Throws:
IOException

getMatching

public Map<K,ICacheElement<K,V>> getMatching(String cacheName,
                                             String pattern)
                                                                                                     throws IOException
Gets all matching items.

Specified by:
getMatching in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
pattern -
Returns:
Map of keys and wrapped objects
Throws:
IOException

getMatching

public Map<K,ICacheElement<K,V>> getMatching(String cacheName,
                                             String pattern,
                                             long requesterId)
                                                                                                     throws IOException
Retrieves all matching keys.

Specified by:
getMatching in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
pattern -
requesterId -
Returns:
Map of keys and wrapped objects
Throws:
IOException

processGetMatching

protected Map<K,ICacheElement<K,V>> processGetMatching(String cacheName,
                                                       String pattern,
                                                       long requesterId)
Retrieves all matching keys.

Parameters:
cacheName -
pattern -
requesterId -
Returns:
Map of keys and wrapped objects

getMultiple

public Map<K,ICacheElement<K,V>> getMultiple(String cacheName,
                                             Set<K> keys)
                                                                                                     throws IOException
Gets multiple items from the cache based on the given set of keys.

Specified by:
getMultiple in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
keys -
Returns:
a map of K key to ICacheElement element, or an empty map if there is no data in cache for any of these keys
Throws:
IOException

getMultiple

public Map<K,ICacheElement<K,V>> getMultiple(String cacheName,
                                             Set<K> keys,
                                             long requesterId)
                                                                                                     throws IOException
Gets multiple items from the cache based on the given set of keys.

The internal processing is wrapped in event logging calls.

Specified by:
getMultiple in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
keys -
requesterId -
Returns:
a map of K key to ICacheElement element, or an empty map if there is no data in cache for any of these keys
Throws:
IOException

getGroupKeys

public Set<K> getGroupKeys(String cacheName,
                           String group)
Gets the set of keys of objects currently in the group.

Specified by:
getGroupKeys in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
group -
Returns:
A Set of group keys

processGetGroupKeys

protected Set<K> processGetGroupKeys(String cacheName,
                                     String group)
Gets the set of keys of objects currently in the group.

Parameters:
cacheName -
group -
Returns:
Set

getGroupNames

public Set<String> getGroupNames(String cacheName)
Gets the set of group names currently in the cache.

Specified by:
getGroupNames in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName - the name of the region
Returns:
A Set of group names

processGetGroupNames

protected Set<String> processGetGroupNames(String cacheName)
Gets the set of group names currently in the cache.

Parameters:
cacheName - the name of the region
Returns:
A Set of group names

remove

public void remove(String cacheName,
                   K key)
            throws IOException
Removes the given key from the specified remote cache. Defaults the listener id to 0.

Specified by:
remove in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
key -
Throws:
IOException

remove

public void remove(String cacheName,
                   K key,
                   long requesterId)
            throws IOException
Remove the key from the cache region and don't tell the source listener about it.

The internal processing is wrapped in event logging calls.

Specified by:
remove in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
key -
requesterId -
Throws:
IOException

removeAll

public void removeAll(String cacheName)
               throws IOException
Remove all keys from the specified remote cache.

Specified by:
removeAll in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
Throws:
IOException

removeAll

public void removeAll(String cacheName,
                      long requesterId)
               throws IOException
Remove all keys from the specified remote cache.

The internal processing is wrapped in event logging calls.

Specified by:
removeAll in interface ICacheServiceNonLocal<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
requesterId -
Throws:
IOException

getPutCount

protected int getPutCount()
How many put events have we received.

Returns:
puts

dispose

public void dispose(String cacheName)
             throws IOException
Frees the specified remote cache.

Specified by:
dispose in interface ICacheService<K extends Serializable,V extends Serializable>
Parameters:
cacheName -
Throws:
IOException

dispose

public void dispose(String cacheName,
                    long requesterId)
             throws IOException
Frees the specified remote cache.

Parameters:
cacheName -
requesterId -
Throws:
IOException

release

public void release()
             throws IOException
Frees all remote caches.

Specified by:
release in interface ICacheService<K extends Serializable,V extends Serializable>
Throws:
IOException

getCacheListeners

protected CacheListeners<K,V> getCacheListeners(String cacheName)
Returns the cache listener for the specified cache. Creates the cache and the cache descriptor if they do not already exist.

Parameters:
cacheName -
Returns:
The cacheListeners value

getClusterListeners

protected CacheListeners<K,V> getClusterListeners(String cacheName)
Gets the clusterListeners attribute of the RemoteCacheServer object.

TODO may be able to remove this

Parameters:
cacheName -
Returns:
The clusterListeners value

addCacheListener

public <KK extends Serializable,VV extends Serializable> void addCacheListener(String cacheName,
                                                                               ICacheListener<KK,VV> listener)
                      throws IOException
Subscribes to the specified remote cache.

If the client id is 0, then the remote cache server will increment it's local count and assign an id to the client.

Specified by:
addCacheListener in interface ICacheObserver
Parameters:
cacheName - the specified remote cache.
listener - object to notify for cache changes. must be synchronized since there are remote calls involved.
Throws:
IOException

addCacheListener

public <KK extends Serializable,VV extends Serializable> void addCacheListener(ICacheListener<KK,VV> listener)
                      throws IOException
Subscribes to all remote caches.

Specified by:
addCacheListener in interface ICacheObserver
Parameters:
listener - The feature to be added to the CacheListener attribute
Throws:
IOException

removeCacheListener

public <KK extends Serializable,VV extends Serializable> void removeCacheListener(String cacheName,
                                                                                  ICacheListener<KK,VV> listener)
                         throws IOException
Unsubscribe this listener from this region. If the listener is registered, it will be removed from the event queue map list.

Specified by:
removeCacheListener in interface ICacheObserver
Parameters:
cacheName -
listener -
Throws:
IOException

removeCacheListener

public void removeCacheListener(String cacheName,
                                long listenerId)
Unsubscribe this listener from this region. If the listener is registered, it will be removed from the event queue map list.

Parameters:
cacheName -
listenerId -

removeCacheListener

public <KK extends Serializable,VV extends Serializable> void removeCacheListener(ICacheListener<KK,VV> listener)
                         throws IOException
Unsubscribes from all remote caches.

Specified by:
removeCacheListener in interface ICacheObserver
Parameters:
listener -
Throws:
IOException

shutdown

public void shutdown()
              throws IOException
Shuts down the remote server.

Specified by:
shutdown in interface ICacheServiceAdmin
Throws:
IOException

shutdown

public void shutdown(String host,
                     int port)
              throws IOException
Shuts down a server at a particular host and port. Then it calls shutdown on the cache itself.

Specified by:
shutdown in interface ICacheServiceAdmin
Parameters:
host -
port -
Throws:
IOException

unreferenced

public void unreferenced()
Called by the RMI runtime sometime after the runtime determines that the reference list, the list of clients referencing the remote object, becomes empty.

Specified by:
unreferenced in interface Unreferenced

getStats

public String getStats()
                throws IOException
Gets the stats attribute of the RemoteCacheServer object.

Specified by:
getStats in interface ICacheServiceAdmin
Returns:
The stats value
Throws:
IOException

logApplicationEvent

protected void logApplicationEvent(String source,
                                   String eventName,
                                   String optionalDetails)
Logs an event if an event logger is configured.

Parameters:
source -
eventName -
optionalDetails -

logICacheEvent

protected <T extends Serializable> void logICacheEvent(ICacheEvent<T> cacheEvent)
Logs an event if an event logger is configured.

Parameters:
cacheEvent -

getExtraInfoForRequesterId

protected String getExtraInfoForRequesterId(long requesterId)
Ip address for the client, if one is stored.

Protected for testing.

Parameters:
requesterId -
Returns:
String

setCacheEventLogger

public void setCacheEventLogger(ICacheEventLogger cacheEventLogger)
Allows it to be injected.

Parameters:
cacheEventLogger -


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