Package org.atmosphere.cpr
Interface Broadcaster
- All Known Implementing Classes:
AbstractBroadcasterProxy,DefaultBroadcaster,ExcludeSessionBroadcaster,SimpleBroadcaster
public interface Broadcaster
A Broadcaster is responsible for delivering messages to its subscribed
AtmosphereResources, which are
representing suspended responses. AtmosphereResource can be added using addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource),
so when broadcast(java.lang.Object) is executed, AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)
will be invoked and the suspended connection will have a chance to write the message available using AtmosphereResourceEvent.getMessage().
A Broadcaster will by default use an ExecutorService, and the number of Threads will be computed
based on the number of cores/CPUs of the OS under which the application runs. Thus invoking
broadcast(Object) will be executed asynchronously so this is
important to wait for the Future.get() to awake/unblock to be guaranteed that the operation has completed.
One final word on Broadcaster: by default, a Broadcaster will broadcast using
all AtmosphereResource on which the response has been suspended, e.g. {AtmosphereResource#suspend()}
has been invoked. This behavior is configurable and you can configure it by invoking the
setScope(org.atmosphere.cpr.Broadcaster.SCOPE) ):- REQUEST: broadcast events only to the AtmosphereResourceEvent associated with the current request.
- APPLICATION: broadcast events to all AtmosphereResourceEvent created for the current web application.
- VM: broadcast events to all AtmosphereResourceEvent created inside the current virtual machine.
- Author:
- Jeanfrancois Arcand
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumstatic enum -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionaddAtmosphereResource(AtmosphereResource resource) Add aAtmosphereResourceto the list of items to be notified when thebroadcast(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 o, Set<AtmosphereResource> subset) Broadcast theObjectto all suspended response, eg.broadcast(Object o, AtmosphereResource resource) Broadcast theObjectto all suspended responses, eg.Broadcast theObjectwhen anAtmosphereResourceis resumed by a timeout or when usingAtmosphereResource.resume().Delay the broadcast operation.delayBroadcast(Object o, long delay, TimeUnit t) Delay the broadcast operation.voiddestroy()Destroy this instance and shutdown it's associatedExecutorService.Return anListofAtmosphereResource.Return the currentBroadcasterConfig.Get theBroadcasterLifeCyclePolicy.getID()Return the ID of thisBroadcaster.getScope()Return theBroadcaster.SCOPE.initialize(String name, URI uri, AtmosphereConfig config) Configure a Broadcaster.booleanReturn true if thisBroadcasterhas been destroyed.voidRelease external resources associated with this Broadcaster.removeAtmosphereResource(AtmosphereResource resource) Remove aAtmosphereResourcefrom the list of s to be notified when thebroadcast(java.lang.Object)is invoked.voidRemove aBroadcasterLifeCyclePolicyListener.Remove aBroadcasterListener.voidResume all suspended responses (AtmosphereResource) added viaaddAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).scheduleFixedBroadcast(Object o, long waitFor, long period, TimeUnit t) Broadcast periodically.scheduleFixedBroadcast(Object o, long period, TimeUnit t) Broadcast periodically.voidSet theBroadcasterConfiginstance.voidSet theBroadcasterLifeCyclePolicy.voidSet the ID of thisBroadcaster.voidsetScope(Broadcaster.SCOPE scope) Set the scope.voidsetSuspendPolicy(long maxSuspended, Broadcaster.POLICY policy) Set the maximum number of suspendedAtmosphereResources.
-
Field Details
-
ROOT_MASTER
- See Also:
-
-
Method Details
-
initialize
Configure a Broadcaster.- Parameters:
name-uri-config- anAtmosphereConfig- Returns:
- a usable Broadcaster.
-
setSuspendPolicy
Set 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.- Parameters:
maxSuspended- maximum number of suspended AtmosphereResource
-
broadcast
Broadcast theObjectto all suspended responses, eg. invokeAtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent).- Parameters:
o- theObjectto be broadcasted- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
delayBroadcast
Delay the broadcast operation. TheObjectwill be broadcasted whenbroadcast(java.lang.Object)is invoked the first time.- Parameters:
o- theObjectto be broadcasted- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
delayBroadcast
Delay the broadcast operation. TheObjectwill be broadcasted once the specified delay expires or whenbroadcast(java.lang.Object)is invoked the first time.- 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
Broadcast periodically. TheObjectwill be broadcasted after every period specified time frame expires. If theTimeUnitis set to null, theObjectwill be broadcasted when the firstbroadcast(java.lang.Object)is invoked.- Parameters:
o- theObjectto be broadcastedperiod- Every so often broadcast.t- aTimeUnitof period.- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
scheduleFixedBroadcast
Broadcast periodically. TheObjectwill be broadcasted after every period specified time frame expires. If theTimeUnitis set null, theObjectwill be broadcasted when the firstbroadcast(java.lang.Object)is invoked.- 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()
-
broadcast
Broadcast theObjectto all suspended responses, eg. invokeAtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)with an instance ofAtmosphereResource, representing a single suspended response.- Parameters:
o- theObjectto be broadcastedresource- anAtmosphereResource- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
broadcastOnResume
Broadcast theObjectwhen anAtmosphereResourceis resumed by a timeout or when usingAtmosphereResource.resume().- Parameters:
o- theObjectto be broadcasted- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
broadcast
Broadcast theObjectto all suspended response, eg. invokeAtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)with aSetofAtmosphereResource, representing a set ofAtmosphereHandler.- Parameters:
o- theObjectto be broadcastedsubset- a Set ofAtmosphereResource- Returns:
- a
Futurethat can be used to synchronize using theFuture.get()
-
addAtmosphereResource
Add aAtmosphereResourceto the list of items to be notified when thebroadcast(java.lang.Object)is invoked. It is strongly recommended to suspend theAtmosphereResourcebefore adding it to aBroadcaster.- Parameters:
resource- anAtmosphereResource- Returns:
- this
-
removeAtmosphereResource
Remove aAtmosphereResourcefrom the list of s to be notified when thebroadcast(java.lang.Object)is invoked.- Parameters:
resource- anAtmosphereResource- Returns:
- this
-
setBroadcasterConfig
Set theBroadcasterConfiginstance.- Parameters:
bc- Configuration to be set.
-
getBroadcasterConfig
BroadcasterConfig getBroadcasterConfig()Return the currentBroadcasterConfig.- Returns:
- the current
BroadcasterConfig
-
destroy
void destroy()Destroy this instance and shutdown it's associatedExecutorService. -
getAtmosphereResources
Collection<AtmosphereResource> getAtmosphereResources()Return anListofAtmosphereResource.- Returns:
ListofAtmosphereResourceassociated with thisBroadcaster.- See Also:
-
setScope
Set the scope.- Parameters:
scope-Broadcaster.SCOPEto set.
-
getScope
Broadcaster.SCOPE getScope()Return theBroadcaster.SCOPE.- Returns:
Broadcaster.SCOPEofBroadcaster.
-
setID
Set the ID of thisBroadcaster.- Parameters:
name- ID of thisBroadcaster
-
getID
String getID()Return the ID of thisBroadcaster.- Returns:
- the ID of this
Broadcaster
-
resumeAll
void resumeAll()Resume all suspended responses (AtmosphereResource) added viaaddAtmosphereResource(org.atmosphere.cpr.AtmosphereResource). -
releaseExternalResources
void releaseExternalResources()Release 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. -
setBroadcasterLifeCyclePolicy
Set theBroadcasterLifeCyclePolicy. Make sure you are selecting the right policy to avoid unexpected situations.- Parameters:
policy- aBroadcasterLifeCyclePolicy
-
getBroadcasterLifeCyclePolicy
BroadcasterLifeCyclePolicy getBroadcasterLifeCyclePolicy()Get theBroadcasterLifeCyclePolicy.- Returns:
BroadcasterLifeCyclePolicy
-
addBroadcasterLifeCyclePolicyListener
- Parameters:
b- aBroadcasterLifeCyclePolicyListener
-
removeBroadcasterLifeCyclePolicyListener
Remove aBroadcasterLifeCyclePolicyListener.- Parameters:
b- aBroadcasterLifeCyclePolicyListener
-
isDestroyed
boolean isDestroyed()Return true if thisBroadcasterhas been destroyed.- Returns:
- true if this
Broadcasterhas been destroyed
-
awaitAndBroadcast
Await for availableAtmosphereResourcebefore broadcasting. This method will block untiladdAtmosphereResource(AtmosphereResource)gets invoked. -
addBroadcasterListener
Add aBroadcasterListener.- Parameters:
b- aBroadcasterListener- Returns:
- this
-
removeBroadcasterListener
Remove aBroadcasterListener.- Parameters:
b- aBroadcasterListener- Returns:
- this
-