Package org.atmosphere.cpr
Class DefaultBroadcaster
java.lang.Object
org.atmosphere.cpr.DefaultBroadcaster
- All Implemented Interfaces:
Broadcaster
- Direct Known Subclasses:
AbstractBroadcasterProxy,ExcludeSessionBroadcaster,SimpleBroadcaster
The default
Broadcaster implementation.
Broadcast messages to suspended responses using the caller's Thread.
This basic Broadcaster use an ExecutorService
to broadcast messages, hence the broadcast operation is asynchronous. Make sure
you block on broadcast(Object).get()} if you need synchronous operations.- Author:
- Jeanfrancois Arcand
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classstatic final classNested classes/interfaces inherited from interface org.atmosphere.cpr.Broadcaster
Broadcaster.POLICY, Broadcaster.SCOPE -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Future<?>[]protected BroadcasterConfigprotected Collection<BroadcasterListener> protected final ConcurrentLinkedQueue<Deliver> static final Stringprotected booleanprotected AtmosphereConfigprotected final ConcurrentLinkedQueue<Deliver> protected final AtomicBooleanprotected final AtomicIntegerprotected final AtomicBooleanprotected final ConcurrentLinkedQueue<BroadcasterLifeCyclePolicyListener> protected final BlockingQueue<Deliver> protected Stringprotected Future<?>[]static final intprotected final ConcurrentLinkedQueue<AtmosphereResource> protected Broadcaster.SCOPEprotected booleanprotected final AtomicBooleanprotected final DefaultBroadcaster.WriteQueueprotected URIprotected final Stringprotected intprotected final ConcurrentHashMap<String, DefaultBroadcaster.WriteQueue> protected intFields inherited from interface org.atmosphere.cpr.Broadcaster
ROOT_MASTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd aAtmosphereResourceto the list of items to be notified when theBroadcaster.broadcast(java.lang.Object)is invoked.voidAdd aBroadcasterListener.awaitAndBroadcast(Object t, long time, TimeUnit timeUnit) Await for availableAtmosphereResourcebefore broadcasting.Broadcast theObjectto all suspended responses, eg.broadcast(Object msg, Set<AtmosphereResource> subset) Broadcast theObjectto all suspended response, eg.broadcast(Object msg, AtmosphereResource r) Broadcast theObjectto all suspended responses, eg.broadcastOnResume(Object msg) Broadcast theObjectwhen anAtmosphereResourceis resumed by a timeout or when usingAtmosphereResource.resume().protected voidprotected voidLook in the cache to see if there are messages available, and take the appropriate actions.voidcacheLostMessage(AtmosphereResource r, boolean force) Cache the message because an unexpected exception occurred.voidCache the message because an unexpected exception occurred.voidcacheLostMessage(AtmosphereResource r, DefaultBroadcaster.AsyncWriteToken token, boolean force) Cache the message because an unexpected exception occurred.protected booleanprotected booleanprotected BroadcasterConfigCreateBroadcasterConfig.Future<?> currentLifecycleTask(Future<?> currentLifecycleTask) Delay the broadcast operation.delayBroadcast(Object o, long delay, TimeUnit t) Delay the broadcast operation.protected voiddeliverPush(Deliver deliver, boolean rec) voiddestroy()Destroy this instance and shutdown it's associatedExecutorService.protected voidprotected booleanendBroadcast(Deliver deliver, AtmosphereResource r, CacheMessage cacheMsg, boolean deliverMessage) protected voidentryDone(BroadcasterFuture<?> f) protected voidprotected voidexecuteBlockingWrite(AtmosphereResource r, Deliver deliver, AtomicInteger count) protected ObjectInvoke theBroadcastFilterprotected BroadcasterFuture<Object> futureDone(Object msg) protected RunnablegetAsyncWriteHandler(DefaultBroadcaster.WriteQueue writeQueue) Return anListofAtmosphereResource.Return the currentBroadcasterConfig.Get theBroadcasterLifeCyclePolicy.protected RunnablegetID()Return the ID of thisBroadcaster.getScope()Return theBroadcaster.SCOPE.initialize(String name, URI uri, AtmosphereConfig config) Configure a Broadcaster.initialize(String name, AtmosphereConfig config) protected voidbooleanReturn true if thisBroadcasterhas been destroyed.protected voidlifecycleHandler(LifecycleHandler lifecycleHandler) messages()protected voidprotected voidprotected voidprotected voidnotifyOnMessage(Deliver deliver) protected voidvoidvoidonException(Throwable t, AtmosphereResource ar, boolean notifyAndCache) booleanprotected booleanpolicy()protected voidprotected voidprotected voidqueueWriteIO(AtmosphereResource r, Deliver deliver, AtomicInteger count) protected intReturn the default number of reactive threads that will be waiting for work when a broadcast operation is executed.voidRelease external resources associated with this Broadcaster.Remove aAtmosphereResourcefrom the list of s to be notified when theBroadcaster.broadcast(java.lang.Object)is invoked.protected BroadcasterremoveAtmosphereResource(AtmosphereResource r, boolean executeDone) voidRemove aBroadcasterLifeCyclePolicyListener.Remove aBroadcasterListener.Rename this Broadcaster without invoking it's associatedBroadcasterFactory.voidResume all suspended responses (AtmosphereResource) added viaBroadcaster.addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).protected booleanscheduleFixedBroadcast(Object o, long waitFor, long period, TimeUnit t) Broadcast periodically.scheduleFixedBroadcast(Object o, long period, TimeUnit t) Broadcast periodically.voidSet theBroadcasterConfiginstance.voidsetBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy lifeCyclePolicy) Set theBroadcasterLifeCyclePolicy.voidSet the ID of thisBroadcaster.voidsetScope(Broadcaster.SCOPE scope) Set the scope.voidsetSuspendPolicy(long maxSuspendResource, Broadcaster.POLICY policy) Set the maximum number of suspendedAtmosphereResources.protected voidprotected voidstart()toString()
-
Field Details
-
POLLING_DEFAULT
public static final int POLLING_DEFAULT- See Also:
-
CACHED
-
resources
-
bc
-
messages
-
broadcasterListeners
-
started
-
initialized
-
destroyed
-
scope
-
name
-
delayedBroadcast
-
broadcastOnResume
-
lifeCycleListeners
-
writeQueues
-
uniqueWriteQueue
-
dispatchThread
-
notifierFuture
-
asyncWriteFuture
-
uri
-
config
-
writeTimeoutInSecond
protected int writeTimeoutInSecond -
waitTime
protected int waitTime -
candidateForPoolable
protected boolean candidateForPoolable -
usingTokenIdForAttribute
-
-
Constructor Details
-
DefaultBroadcaster
public DefaultBroadcaster()
-
-
Method Details
-
initialize
Description copied from interface:BroadcasterConfigure a Broadcaster.- Specified by:
initializein interfaceBroadcaster- Parameters:
name-uri-config- anAtmosphereConfig- Returns:
- a usable Broadcaster.
-
initialize
-
createBroadcasterConfig
CreateBroadcasterConfig.- Parameters:
config- theAtmosphereConfig- Returns:
- an instance of
BroadcasterConfig
-
destroy
public void destroy()Description copied from interface:BroadcasterDestroy this instance and shutdown it's associatedExecutorService.- Specified by:
destroyin interfaceBroadcaster
-
getAtmosphereResources
Description copied from interface:BroadcasterReturn anListofAtmosphereResource.- Specified by:
getAtmosphereResourcesin interfaceBroadcaster- Returns:
ListofAtmosphereResourceassociated with thisBroadcaster.- See Also:
-
setScope
Description copied from interface:BroadcasterSet the scope.- Specified by:
setScopein interfaceBroadcaster- Parameters:
scope-Broadcaster.SCOPEto set.
-
getScope
Description copied from interface:BroadcasterReturn theBroadcaster.SCOPE.- Specified by:
getScopein interfaceBroadcaster- Returns:
Broadcaster.SCOPEofBroadcaster.
-
setID
Description copied from interface:BroadcasterSet the ID of thisBroadcaster.- Specified by:
setIDin interfaceBroadcaster- Parameters:
id- ID of thisBroadcaster
-
rename
Rename this Broadcaster without invoking it's associatedBroadcasterFactory. This method must be carefully used as it could easily create memory leak as the Broadcaster won't be removed from itsBroadcasterFactory.- Parameters:
id- the new name- Returns:
- this;
-
getID
Description copied from interface:BroadcasterReturn the ID of thisBroadcaster.- Specified by:
getIDin interfaceBroadcaster- Returns:
- the ID of this
Broadcaster
-
resumeAll
public void resumeAll()Description copied from interface:BroadcasterResume all suspended responses (AtmosphereResource) added viaBroadcaster.addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).- Specified by:
resumeAllin interfaceBroadcaster
-
releaseExternalResources
public void releaseExternalResources()Description copied from interface:BroadcasterRelease external resources associated with this Broadcaster. This is useful when a Broadcaster no longer have suspendedAtmosphereResourceand some resource (like database connections, JMS queue, etc.) needs to be closed.- Specified by:
releaseExternalResourcesin interfaceBroadcaster
-
setBroadcasterLifeCyclePolicy
Description copied from interface:BroadcasterSet theBroadcasterLifeCyclePolicy. Make sure you are selecting the right policy to avoid unexpected situations.- Specified by:
setBroadcasterLifeCyclePolicyin interfaceBroadcaster- Parameters:
lifeCyclePolicy- aBroadcasterLifeCyclePolicy
-
getBroadcasterLifeCyclePolicy
Description copied from interface:BroadcasterGet theBroadcasterLifeCyclePolicy.- Specified by:
getBroadcasterLifeCyclePolicyin interfaceBroadcaster- Returns:
BroadcasterLifeCyclePolicy
-
addBroadcasterLifeCyclePolicyListener
Description copied from interface:Broadcaster- Specified by:
addBroadcasterLifeCyclePolicyListenerin interfaceBroadcaster- Parameters:
b- aBroadcasterLifeCyclePolicyListener
-
removeBroadcasterLifeCyclePolicyListener
Description copied from interface:BroadcasterRemove aBroadcasterLifeCyclePolicyListener.- Specified by:
removeBroadcasterLifeCyclePolicyListenerin interfaceBroadcaster- Parameters:
b- aBroadcasterLifeCyclePolicyListener
-
isDestroyed
public boolean isDestroyed()Description copied from interface:BroadcasterReturn true if thisBroadcasterhas been destroyed.- Specified by:
isDestroyedin interfaceBroadcaster- Returns:
- true if this
Broadcasterhas been destroyed
-
awaitAndBroadcast
Description copied from interface:BroadcasterAwait for availableAtmosphereResourcebefore broadcasting. This method will block untilBroadcaster.addAtmosphereResource(AtmosphereResource)gets invoked.- Specified by:
awaitAndBroadcastin interfaceBroadcaster
-
addBroadcasterListener
Description copied from interface:BroadcasterAdd aBroadcasterListener.- Specified by:
addBroadcasterListenerin interfaceBroadcaster- Parameters:
b- aBroadcasterListener- Returns:
- this
-
removeBroadcasterListener
Description copied from interface:BroadcasterRemove aBroadcasterListener.- Specified by:
removeBroadcasterListenerin interfaceBroadcaster- Parameters:
b- aBroadcasterListener- Returns:
- this
-
getBroadcastHandler
-
getAsyncWriteHandler
-
start
protected void start() -
spawnReactor
protected void spawnReactor() -
killReactiveThreads
protected void killReactiveThreads() -
reactiveThreadsCount
protected int reactiveThreadsCount()Return the default number of reactive threads that will be waiting for work when a broadcast operation is executed.- Returns:
- the default number of reactive threads
-
push
-
deliverPush
-
endBroadcast
protected boolean endBroadcast(Deliver deliver, AtmosphereResource r, CacheMessage cacheMsg, boolean deliverMessage) -
queueWriteIO
protected void queueWriteIO(AtmosphereResource r, Deliver deliver, AtomicInteger count) throws InterruptedException - Throws:
InterruptedException
-
executeBlockingWrite
protected void executeBlockingWrite(AtmosphereResource r, Deliver deliver, AtomicInteger count) throws InterruptedException - Throws:
InterruptedException
-
perRequestFilter
-
executeAsyncWrite
-
cacheMessageOnIOException
-
checkCachedAndPush
-
retrieveTrackedBroadcast
-
invokeOnStateChange
-
prepareInvokeOnStateChange
-
onException
-
onException
-
cacheLostMessage
Cache the message because an unexpected exception occurred.- Parameters:
r-AtmosphereResource
-
cacheLostMessage
Cache the message because an unexpected exception occurred.- Parameters:
r-AtmosphereResource
-
cacheLostMessage
public void cacheLostMessage(AtmosphereResource r, DefaultBroadcaster.AsyncWriteToken token, boolean force) Cache the message because an unexpected exception occurred.- Parameters:
r-AtmosphereResource
-
setSuspendPolicy
Description copied from interface:BroadcasterSet the maximum number of suspendedAtmosphereResources. If the maximum is reached, Atmosphere will either resumeAtmosphereResourceusingBroadcaster.POLICY.FIFO(first in first out) orBroadcaster.POLICY.REJECTtheAtmosphereResource. By default the number is unlimited.- Specified by:
setSuspendPolicyin interfaceBroadcaster- Parameters:
maxSuspendResource- maximum number of suspended AtmosphereResource
-
broadcast
Description copied from interface:BroadcasterBroadcast theObjectto all suspended responses, eg. invokeAtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent).- Specified by:
broadcastin interfaceBroadcaster- Parameters:
msg- theObjectto be broadcasted- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
futureDone
-
dispatchMessages
-
filter
Invoke theBroadcastFilter- Parameters:
msg-- Returns:
-
broadcast
Description copied from interface:BroadcasterBroadcast theObjectto all suspended responses, eg. invokeAtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)with an instance ofAtmosphereResource, representing a single suspended response.- Specified by:
broadcastin interfaceBroadcaster- Parameters:
msg- theObjectto be broadcastedr- anAtmosphereResource- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
broadcastOnResume
Description copied from interface:BroadcasterBroadcast theObjectwhen anAtmosphereResourceis resumed by a timeout or when usingAtmosphereResource.resume().- Specified by:
broadcastOnResumein interfaceBroadcaster- Parameters:
msg- theObjectto be broadcasted- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
broadcastOnResume
-
broadcast
Description copied from interface:BroadcasterBroadcast theObjectto all suspended response, eg. invokeAtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)with aSetofAtmosphereResource, representing a set ofAtmosphereHandler.- Specified by:
broadcastin interfaceBroadcaster- Parameters:
msg- theObjectto be broadcastedsubset- a Set ofAtmosphereResource- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
addAtmosphereResource
Description copied from interface:BroadcasterAdd aAtmosphereResourceto the list of items to be notified when theBroadcaster.broadcast(java.lang.Object)is invoked. It is strongly recommended to suspend theAtmosphereResourcebefore adding it to aBroadcaster.- Specified by:
addAtmosphereResourcein interfaceBroadcaster- Parameters:
r- anAtmosphereResource- Returns:
- this
-
cacheAndSuspend
Look in the cache to see if there are messages available, and take the appropriate actions.- Parameters:
r- AtmosphereResource
-
notifyAndAdd
-
entryDone
-
notifyBroadcastListener
protected void notifyBroadcastListener() -
notifyOnAddAtmosphereResourceListener
-
notifyOnRemoveAtmosphereResourceListener
-
notifyOnMessage
-
removeAtmosphereResource
Description copied from interface:BroadcasterRemove aAtmosphereResourcefrom the list of s to be notified when theBroadcaster.broadcast(java.lang.Object)is invoked.- Specified by:
removeAtmosphereResourcein interfaceBroadcaster- Parameters:
r- anAtmosphereResource- Returns:
- this
-
removeAtmosphereResource
-
setBroadcasterConfig
Description copied from interface:BroadcasterSet theBroadcasterConfiginstance.- Specified by:
setBroadcasterConfigin interfaceBroadcaster- Parameters:
bc- Configuration to be set.
-
getBroadcasterConfig
Description copied from interface:BroadcasterReturn the currentBroadcasterConfig.- Specified by:
getBroadcasterConfigin interfaceBroadcaster- Returns:
- the current
BroadcasterConfig
-
delayBroadcast
Description copied from interface:BroadcasterDelay the broadcast operation. TheObjectwill be broadcasted whenBroadcaster.broadcast(java.lang.Object)is invoked the first time.- Specified by:
delayBroadcastin interfaceBroadcaster- Parameters:
o- theObjectto be broadcasted- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
delayBroadcast
Description copied from interface:BroadcasterDelay the broadcast operation. TheObjectwill be broadcasted once the specified delay expires or whenBroadcaster.broadcast(java.lang.Object)is invoked the first time.- Specified by:
delayBroadcastin interfaceBroadcaster- Parameters:
o- theObjectto be broadcasteddelay- Amount of time to delayt- theTimeUnitof the delay- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
scheduleFixedBroadcast
Description copied from interface:BroadcasterBroadcast periodically. TheObjectwill be broadcasted after every period specified time frame expires. If theTimeUnitis set to null, theObjectwill be broadcasted when the firstBroadcaster.broadcast(java.lang.Object)is invoked.- Specified by:
scheduleFixedBroadcastin interfaceBroadcaster- Parameters:
o- theObjectto be broadcastedperiod- Every so often broadcast.t- aTimeUnitof period.- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
scheduleFixedBroadcast
Description copied from interface:BroadcasterBroadcast periodically. TheObjectwill be broadcasted after every period specified time frame expires. If theTimeUnitis set null, theObjectwill be broadcasted when the firstBroadcaster.broadcast(java.lang.Object)is invoked.- Specified by:
scheduleFixedBroadcastin interfaceBroadcaster- Parameters:
o- theObjectto be broadcastedwaitFor- Wait for that long before first broadcastperiod- The period inbetween broadcast.t- aTimeUnitof waitFor and period.- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
toString
-
broadcasterListeners
-
lifeCyclePolicy
-
lifeCycleListeners
-
messages
-
writeQueues
-
policy
-
outOfOrderBroadcastSupported
public boolean outOfOrderBroadcastSupported() -
recentActivity
-
lifecycleHandler
-
lifecycleHandler
-
currentLifecycleTask
-
currentLifecycleTask
-