public interface BroadcasterCache extends AtmosphereConfigAware
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 an
AtmosphereResource
, 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's Broadcaster
.
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. WhenBroadcaster
starts,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, aList
will be returned and written back to the client. 7. When messages are added to the cache, an application can always customize the messages by creatingBroadcasterCacheInspector
and 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 particularAtmosphereResource
by invokingexcludeFromCache(String, AtmosphereResource)
BroadcasterConfig.setBroadcasterCache(BroadcasterCache)
, by
defining it in your web/application.xml or by using the BroadcasterCacheService
annotation.Modifier and Type | Field and Description |
---|---|
static BroadcasterCache |
DEFAULT |
static String |
NULL |
Modifier and Type | Method and Description |
---|---|
BroadcasterCache |
addBroadcasterCacheListener(BroadcasterCacheListener l)
Add a
BroadcasterCacheListener |
CacheMessage |
addToCache(String broadcasterId,
String uuid,
BroadcastMessage message)
Start tracking messages associated with
AtmosphereResource from the cache. |
BroadcasterCache |
cacheCandidate(String broadcasterId,
String uuid)
Add a
AtmosphereResource.uuid() to the list of active AtmosphereResource
Message will be cached for the resource associated with the uuid. |
void |
cleanup()
Clean resources associated with this instance.
|
BroadcasterCache |
clearCache(String broadcasterId,
String uuid,
CacheMessage cache)
Remove the previously cached message.
|
BroadcasterCache |
excludeFromCache(String broadcasterId,
AtmosphereResource r)
Allow an application to exclude, or block, an
AtmosphereResource to received cached message. |
BroadcasterCache |
inspector(BroadcasterCacheInspector interceptor)
Add a
BroadcasterCacheInspector that will be invoked before a message gets added to the cache. |
BroadcasterCache |
removeBroadcasterCacheListener(BroadcasterCacheListener l)
Remove a
BroadcasterCacheListener |
List<Object> |
retrieveFromCache(String id,
String uuid)
Retrieve messages associated with
AtmosphereResource . |
void |
start()
This method is invoked when the Broadcaster is started.
|
void |
stop()
This method is invoked when the Broadcaster is stopped.
|
configure
static final String NULL
static final BroadcasterCache DEFAULT
void start()
void stop()
void cleanup()
Broadcaster
gets destroyed.CacheMessage addToCache(String broadcasterId, String uuid, BroadcastMessage message)
AtmosphereResource
from the cache.broadcasterId
- The associated Broadcaster#addAtmosphereResource(AtmosphereResource).getID
uuid
- AtmosphereResource.uuid()
message
- BroadcastMessage
.CacheMessage
List<Object> retrieveFromCache(String id, String uuid)
AtmosphereResource
.id
- The associated org.atmosphere.cpr.Broadcaster#addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).getID
uuid
- AtmosphereResource
List
of messages (String).BroadcasterCache clearCache(String broadcasterId, String uuid, CacheMessage cache)
broadcasterId
- The Broadcaster.getID()
uuid
- an AtmosphereResource.uuid()
cache
- the CacheMessage
BroadcasterCache excludeFromCache(String broadcasterId, AtmosphereResource r)
AtmosphereResource
to received cached message.
No new message will get sent to this client except the ones already cached.broadcasterId
- The Broadcaster.getID()
r
- an AtmosphereResource
BroadcasterCache cacheCandidate(String broadcasterId, String uuid)
AtmosphereResource.uuid()
to the list of active AtmosphereResource
Message will be cached for the resource associated with the uuid.broadcasterId
- The Broadcaster.getID()
uuid
- an AtmosphereResource.uuid()
BroadcasterCache inspector(BroadcasterCacheInspector interceptor)
BroadcasterCacheInspector
that will be invoked before a message gets added to the cache.interceptor
- an instance of BroadcasterCacheInspector
BroadcasterCache addBroadcasterCacheListener(BroadcasterCacheListener l)
BroadcasterCacheListener
l
- a BroadcasterCacheListener
BroadcasterCache removeBroadcasterCacheListener(BroadcasterCacheListener l)
BroadcasterCacheListener
l
- a BroadcasterCacheListener
Copyright © 2016. All rights reserved.