Package org.atmosphere.cpr
Interface BroadcasterCache
-
- All Superinterfaces:
AtmosphereConfigAware
- All Known Implementing Classes:
AbstractBroadcasterCache,DefaultBroadcasterCache,SessionBroadcasterCache,UUIDBroadcasterCache
public interface BroadcasterCache extends AtmosphereConfigAware
A BroadcasterCache is a cache for broadcasted messages. When a Broadcaster is about to execute a broadcast operation (Broadcaster.broadcast(Object), the messages is cached, and the the write operation is executed. If the write operation succeed, the message is removed from the cache. If the write operation fails for anAtmosphereResource, the message stays in the cache so next time the client reconnects, the message can be sent back to the client. BroadcasterCache is useful for applications that require that no messages are lost, e.g all broadcasted message must be delivered to the client. If your application can survive lost messages, your don't need to install a BroadcasterCache. A BroadcasterCache works the following way. The methods are always invoked from the application'sBroadcaster.
Implementations of this interface must be thread-safe. A BroadcasterCache can be configured by invoking1. When the Broadcaster is created, a unique BroadcasterCache is created and assigned to it as well. That means a BroadcasterCache is, by default, associated with a Broadcaster. You can share BroadcasterCache instances among Broadcasters as well. 2. Just after the constructor has been invoked, the#configure(BroadcasterConfig)will get invoked, allowing the instance to configure itself based on aBroadcasterConfig. 3. WhenBroadcasterstarts,start()will be invoked. 4. Every time aBroadcaster.broadcast(Object)invocation occurs, theaddToCache(String, String, org.atmosphere.cache.BroadcastMessage)method will be invoked, allowing the instance to cache the object. 5. If the write operation succeeds, theclearCache(String, String, org.atmosphere.cache.CacheMessage)method will be invoked. If the write operation fail the cache won't be cleared, and the message will be available next time the client reconnects. An application that write a BroadcasterCache must make sure cached message aren't staying in the cache forever to prevent memory leaks. 6. When a client reconnects, theretrieveFromCache(String, String)method will be invoked. If messages are available, aListwill be returned and written back to the client. 7. When messages are added to the cache, an application can always customize the messages by creatingBroadcasterCacheInspectorand add them usinginspector(org.atmosphere.cache.BroadcasterCacheInspector). BroadcasterCacheInspector will be invoked every timeaddToCache(String, String, org.atmosphere.cache.BroadcastMessage)is executed. 8. An application may decide that, at one point in time, stop caching message for a particularAtmosphereResourceby invokingexcludeFromCache(String, AtmosphereResource)BroadcasterConfig.setBroadcasterCache(BroadcasterCache), by defining it in your web/application.xml or by using theBroadcasterCacheServiceannotation.- Author:
- Jeanfrancois Arcand
-
-
Field Summary
Fields Modifier and Type Field Description static BroadcasterCacheDEFAULTstatic java.lang.StringNULL
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description BroadcasterCacheaddBroadcasterCacheListener(BroadcasterCacheListener l)Add aBroadcasterCacheListenerCacheMessageaddToCache(java.lang.String broadcasterId, java.lang.String uuid, BroadcastMessage message)Start tracking messages associated withAtmosphereResourcefrom the cache.BroadcasterCachecacheCandidate(java.lang.String broadcasterId, java.lang.String uuid)Add aAtmosphereResource.uuid()to the list of activeAtmosphereResourceMessage will be cached for the resource associated with the uuid.voidcleanup()Clean resources associated with this instance.BroadcasterCacheclearCache(java.lang.String broadcasterId, java.lang.String uuid, CacheMessage cache)Remove the previously cached message.BroadcasterCacheexcludeFromCache(java.lang.String broadcasterId, AtmosphereResource r)Allow an application to exclude, or block, anAtmosphereResourceto received cached message.BroadcasterCacheinspector(BroadcasterCacheInspector interceptor)Add aBroadcasterCacheInspectorthat will be invoked before a message gets added to the cache.BroadcasterCacheremoveBroadcasterCacheListener(BroadcasterCacheListener l)Remove aBroadcasterCacheListenerjava.util.List<java.lang.Object>retrieveFromCache(java.lang.String id, java.lang.String uuid)Retrieve messages associated withAtmosphereResource.voidstart()This method is invoked when the Broadcaster is started.voidstop()This method is invoked when the Broadcaster is stopped.-
Methods inherited from interface org.atmosphere.inject.AtmosphereConfigAware
configure
-
-
-
-
Field Detail
-
NULL
static final java.lang.String NULL
- See Also:
- Constant Field Values
-
DEFAULT
static final BroadcasterCache DEFAULT
-
-
Method Detail
-
start
void start()
This method is invoked when the Broadcaster is started.
-
stop
void stop()
This method is invoked when the Broadcaster is stopped.
-
cleanup
void cleanup()
Clean resources associated with this instance. This method is useful when ExecutorServices are shared and some future must be cancelled. This method will always be invoked when aBroadcastergets destroyed.
-
addToCache
CacheMessage addToCache(java.lang.String broadcasterId, java.lang.String uuid, BroadcastMessage message)
Start tracking messages associated withAtmosphereResourcefrom the cache.- Parameters:
broadcasterId- The associated {@link Broadcaster#addAtmosphereResource(AtmosphereResource).getID}uuid-AtmosphereResource.uuid()message-BroadcastMessage.- Returns:
- The
CacheMessage
-
retrieveFromCache
java.util.List<java.lang.Object> retrieveFromCache(java.lang.String id, java.lang.String uuid)Retrieve messages associated withAtmosphereResource.- Parameters:
id- The associated {@link org.atmosphere.cpr.Broadcaster#addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).getID}uuid-AtmosphereResource- Returns:
- a
Listof messages (String).
-
clearCache
BroadcasterCache clearCache(java.lang.String broadcasterId, java.lang.String uuid, CacheMessage cache)
Remove the previously cached message.- Parameters:
broadcasterId- TheBroadcaster.getID()uuid- anAtmosphereResource.uuid()cache- theCacheMessage
-
excludeFromCache
BroadcasterCache excludeFromCache(java.lang.String broadcasterId, AtmosphereResource r)
Allow an application to exclude, or block, anAtmosphereResourceto received cached message. No new message will get sent to this client except the ones already cached.- Parameters:
broadcasterId- TheBroadcaster.getID()r- anAtmosphereResource- Returns:
- this
-
cacheCandidate
BroadcasterCache cacheCandidate(java.lang.String broadcasterId, java.lang.String uuid)
Add aAtmosphereResource.uuid()to the list of activeAtmosphereResourceMessage will be cached for the resource associated with the uuid.- Parameters:
broadcasterId- TheBroadcaster.getID()uuid- anAtmosphereResource.uuid()- Returns:
- this
-
inspector
BroadcasterCache inspector(BroadcasterCacheInspector interceptor)
Add aBroadcasterCacheInspectorthat will be invoked before a message gets added to the cache.- Parameters:
interceptor- an instance ofBroadcasterCacheInspector- Returns:
- this
-
addBroadcasterCacheListener
BroadcasterCache addBroadcasterCacheListener(BroadcasterCacheListener l)
Add aBroadcasterCacheListener- Parameters:
l- aBroadcasterCacheListener- Returns:
- this
-
removeBroadcasterCacheListener
BroadcasterCache removeBroadcasterCacheListener(BroadcasterCacheListener l)
Remove aBroadcasterCacheListener- Parameters:
l- aBroadcasterCacheListener- Returns:
- this
-
-