Package org.atmosphere.cpr
Interface Broadcaster
-
- All Known Implementing Classes:
AbstractBroadcasterProxy,DefaultBroadcaster,ExcludeSessionBroadcaster,SimpleBroadcaster
public interface BroadcasterA Broadcaster is responsible for delivering messages to its subscribedAtmosphereResources, which are representing suspended responses.AtmosphereResourcecan be added usingaddAtmosphereResource(org.atmosphere.cpr.AtmosphereResource), so whenbroadcast(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 usingAtmosphereResourceEvent.getMessage(). ABroadcasterwill by default use anExecutorService, and the number of Threads will be computed based on the number of cores/CPUs of the OS under which the application runs. Thus invokingbroadcast(Object)will be executed asynchronously so this is important to wait for theFuture.get()to awake/unblock to be guaranteed that the operation has completed. One final word on Broadcaster: by default, a Broadcaster will broadcast using allAtmosphereResourceon which the response has been suspended, e.g. {AtmosphereResource#suspend()} has been invoked. This behavior is configurable and you can configure it by invoking thesetScope(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 Classes Modifier and Type Interface Description static classBroadcaster.POLICYstatic classBroadcaster.SCOPE
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringROOT_MASTER
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description BroadcasteraddAtmosphereResource(AtmosphereResource resource)Add aAtmosphereResourceto the list of items to be notified when thebroadcast(java.lang.Object)is invoked.voidaddBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)BroadcasteraddBroadcasterListener(BroadcasterListener b)Add aBroadcasterListener.java.util.concurrent.Future<java.lang.Object>awaitAndBroadcast(java.lang.Object t, long time, java.util.concurrent.TimeUnit timeUnit)Await for availableAtmosphereResourcebefore broadcasting.java.util.concurrent.Future<java.lang.Object>broadcast(java.lang.Object o)Broadcast theObjectto all suspended responses, eg.java.util.concurrent.Future<java.lang.Object>broadcast(java.lang.Object o, java.util.Set<AtmosphereResource> subset)Broadcast theObjectto all suspended response, eg.java.util.concurrent.Future<java.lang.Object>broadcast(java.lang.Object o, AtmosphereResource resource)Broadcast theObjectto all suspended responses, eg.java.util.concurrent.Future<java.lang.Object>broadcastOnResume(java.lang.Object o)Broadcast theObjectwhen anAtmosphereResourceis resumed by a timeout or when usingAtmosphereResource.resume().java.util.concurrent.Future<java.lang.Object>delayBroadcast(java.lang.Object o)Delay the broadcast operation.java.util.concurrent.Future<java.lang.Object>delayBroadcast(java.lang.Object o, long delay, java.util.concurrent.TimeUnit t)Delay the broadcast operation.voiddestroy()Destroy this instance and shutdown it's associatedExecutorService.java.util.Collection<AtmosphereResource>getAtmosphereResources()Return anListofAtmosphereResource.BroadcasterConfiggetBroadcasterConfig()Return the currentBroadcasterConfig.BroadcasterLifeCyclePolicygetBroadcasterLifeCyclePolicy()Get theBroadcasterLifeCyclePolicy.java.lang.StringgetID()Return the ID of thisBroadcaster.Broadcaster.SCOPEgetScope()Return theBroadcaster.SCOPE.Broadcasterinitialize(java.lang.String name, java.net.URI uri, AtmosphereConfig config)Configure a Broadcaster.booleanisDestroyed()Return true if thisBroadcasterhas been destroyed.voidreleaseExternalResources()Release external resources associated with this Broadcaster.BroadcasterremoveAtmosphereResource(AtmosphereResource resource)Remove aAtmosphereResourcefrom the list of s to be notified when thebroadcast(java.lang.Object)is invoked.voidremoveBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)Remove aBroadcasterLifeCyclePolicyListener.BroadcasterremoveBroadcasterListener(BroadcasterListener b)Remove aBroadcasterListener.voidresumeAll()Resume all suspended responses (AtmosphereResource) added viaaddAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).java.util.concurrent.Future<java.lang.Object>scheduleFixedBroadcast(java.lang.Object o, long waitFor, long period, java.util.concurrent.TimeUnit t)Broadcast periodically.java.util.concurrent.Future<java.lang.Object>scheduleFixedBroadcast(java.lang.Object o, long period, java.util.concurrent.TimeUnit t)Broadcast periodically.voidsetBroadcasterConfig(BroadcasterConfig bc)Set theBroadcasterConfiginstance.voidsetBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy policy)Set theBroadcasterLifeCyclePolicy.voidsetID(java.lang.String name)Set the ID of thisBroadcaster.voidsetScope(Broadcaster.SCOPE scope)Set the scope.voidsetSuspendPolicy(long maxSuspended, Broadcaster.POLICY policy)Set the maximum number of suspendedAtmosphereResources.
-
-
-
Field Detail
-
ROOT_MASTER
static final java.lang.String ROOT_MASTER
- See Also:
- Constant Field Values
-
-
Method Detail
-
initialize
Broadcaster initialize(java.lang.String name, java.net.URI uri, AtmosphereConfig config)
Configure a Broadcaster.- Parameters:
name-uri-config- anAtmosphereConfig- Returns:
- a usable Broadcaster.
-
setSuspendPolicy
void setSuspendPolicy(long maxSuspended, Broadcaster.POLICY policy)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
java.util.concurrent.Future<java.lang.Object> broadcast(java.lang.Object o)
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
java.util.concurrent.Future<java.lang.Object> delayBroadcast(java.lang.Object o)
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
java.util.concurrent.Future<java.lang.Object> delayBroadcast(java.lang.Object o, long delay, java.util.concurrent.TimeUnit t)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
java.util.concurrent.Future<java.lang.Object> scheduleFixedBroadcast(java.lang.Object o, long period, java.util.concurrent.TimeUnit t)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
java.util.concurrent.Future<java.lang.Object> scheduleFixedBroadcast(java.lang.Object o, long waitFor, long period, java.util.concurrent.TimeUnit t)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
java.util.concurrent.Future<java.lang.Object> broadcast(java.lang.Object o, AtmosphereResource resource)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
java.util.concurrent.Future<java.lang.Object> broadcastOnResume(java.lang.Object o)
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
java.util.concurrent.Future<java.lang.Object> broadcast(java.lang.Object o, java.util.Set<AtmosphereResource> subset)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
Broadcaster addAtmosphereResource(AtmosphereResource resource)
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
Broadcaster removeAtmosphereResource(AtmosphereResource resource)
Remove aAtmosphereResourcefrom the list of s to be notified when thebroadcast(java.lang.Object)is invoked.- Parameters:
resource- anAtmosphereResource- Returns:
- this
-
setBroadcasterConfig
void setBroadcasterConfig(BroadcasterConfig bc)
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
java.util.Collection<AtmosphereResource> getAtmosphereResources()
Return anListofAtmosphereResource.- Returns:
ListofAtmosphereResourceassociated with thisBroadcaster.- See Also:
addAtmosphereResource(AtmosphereResource)
-
setScope
void setScope(Broadcaster.SCOPE scope)
Set the scope.- Parameters:
scope-Broadcaster.SCOPEto set.
-
getScope
Broadcaster.SCOPE getScope()
Return theBroadcaster.SCOPE.- Returns:
Broadcaster.SCOPEofBroadcaster.
-
setID
void setID(java.lang.String name)
Set the ID of thisBroadcaster.- Parameters:
name- ID of thisBroadcaster
-
getID
java.lang.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
void setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy policy)
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
void addBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
- Parameters:
b- aBroadcasterLifeCyclePolicyListener
-
removeBroadcasterLifeCyclePolicyListener
void removeBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
Remove aBroadcasterLifeCyclePolicyListener.- Parameters:
b- aBroadcasterLifeCyclePolicyListener
-
isDestroyed
boolean isDestroyed()
Return true if thisBroadcasterhas been destroyed.- Returns:
- true if this
Broadcasterhas been destroyed
-
awaitAndBroadcast
java.util.concurrent.Future<java.lang.Object> awaitAndBroadcast(java.lang.Object t, long time, java.util.concurrent.TimeUnit timeUnit)Await for availableAtmosphereResourcebefore broadcasting. This method will block untiladdAtmosphereResource(AtmosphereResource)gets invoked.
-
addBroadcasterListener
Broadcaster addBroadcasterListener(BroadcasterListener b)
Add aBroadcasterListener.- Parameters:
b- aBroadcasterListener- Returns:
- this
-
removeBroadcasterListener
Broadcaster removeBroadcasterListener(BroadcasterListener b)
Remove aBroadcasterListener.- Parameters:
b- aBroadcasterListener- Returns:
- this
-
-