public interface Broadcaster
AtmosphereResource
s, 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)
):Modifier and Type | Interface and Description |
---|---|
static class |
Broadcaster.POLICY |
static class |
Broadcaster.SCOPE |
Modifier and Type | Field and Description |
---|---|
static String |
ROOT_MASTER |
static final String ROOT_MASTER
Broadcaster initialize(String name, URI uri, AtmosphereConfig config)
name
- uri
- config
- an AtmosphereConfig
void setSuspendPolicy(long maxSuspended, Broadcaster.POLICY policy)
AtmosphereResource
s. If the maximum is reached, Atmosphere will either
resume AtmosphereResource
using Broadcaster.POLICY.FIFO
(first in first out)
or Broadcaster.POLICY.REJECT
the AtmosphereResource
.
By default the number is unlimited.maxSuspended
- maximum number of suspended AtmosphereResourceFuture<Object> broadcast(Object o)
Object
to all suspended responses, eg. invoke AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)
.o
- the Object
to be broadcastedFuture
that can be used to synchronize using the Future.get()
Future<Object> delayBroadcast(Object o)
Object
will be broadcasted when broadcast(java.lang.Object)
is invoked the first time.o
- the Object
to be broadcastedFuture
that can be used to synchronize using the Future.get()
Future<Object> delayBroadcast(Object o, long delay, TimeUnit t)
Object
will be broadcasted once the
specified delay expires or when broadcast(java.lang.Object)
is invoked the first time.o
- the Object
to be broadcasteddelay
- Amount of time to delayt
- the TimeUnit
of the delayFuture
that can be used to synchronize using the Future.get()
Future<Object> scheduleFixedBroadcast(Object o, long period, TimeUnit t)
Object
will be broadcasted after every period
specified time frame expires. If the TimeUnit
is set to null, the
Object
will be broadcasted when the first broadcast(java.lang.Object)
is invoked.o
- the Object
to be broadcastedperiod
- Every so often broadcast.t
- a TimeUnit
of period.Future
that can be used to synchronize using the Future.get()
Future<Object> scheduleFixedBroadcast(Object o, long waitFor, long period, TimeUnit t)
Object
will be broadcasted after every period
specified time frame expires. If the TimeUnit
is set null, the
Object
will be broadcasted when the first broadcast(java.lang.Object)
is invoked.o
- the Object
to be broadcastedwaitFor
- Wait for that long before first broadcastperiod
- The period inbetween broadcast.t
- a TimeUnit
of waitFor and period.Future
that can be used to synchronize using the Future.get()
Future<Object> broadcast(Object o, AtmosphereResource resource)
Object
to all suspended responses, eg. invoke
AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)
with an instance of AtmosphereResource
, representing
a single suspended response.o
- the Object
to be broadcastedresource
- an AtmosphereResource
Future
that can be used to synchronize using the Future.get()
Future<Object> broadcastOnResume(Object o)
Object
when an AtmosphereResource
is resumed by a timeout or when using
AtmosphereResource.resume()
.o
- the Object
to be broadcastedFuture
that can be used to synchronize using the Future.get()
Future<Object> broadcast(Object o, Set<AtmosphereResource> subset)
Object
to all suspended response, eg. invoke
AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent)
with a Set
of AtmosphereResource
,
representing a set of AtmosphereHandler
.o
- the Object
to be broadcastedsubset
- a Set of AtmosphereResource
Future
that can be used to synchronize using the Future.get()
Broadcaster addAtmosphereResource(AtmosphereResource resource)
AtmosphereResource
to the list of items to be notified when
the broadcast(java.lang.Object)
is invoked.
It is strongly recommended to suspend the AtmosphereResource
before
adding it to a Broadcaster
.resource
- an AtmosphereResource
Broadcaster removeAtmosphereResource(AtmosphereResource resource)
AtmosphereResource
from the list of s to be notified when
the broadcast(java.lang.Object)
is invoked.resource
- an AtmosphereResource
void setBroadcasterConfig(BroadcasterConfig bc)
BroadcasterConfig
instance.bc
- Configuration to be set.BroadcasterConfig getBroadcasterConfig()
BroadcasterConfig
.BroadcasterConfig
void destroy()
ExecutorService
.Collection<AtmosphereResource> getAtmosphereResources()
List
of AtmosphereResource
.List
of AtmosphereResource
associated with this Broadcaster
.addAtmosphereResource(AtmosphereResource)
void setScope(Broadcaster.SCOPE scope)
scope
- Broadcaster.SCOPE
to set.Broadcaster.SCOPE getScope()
Broadcaster.SCOPE
.Broadcaster.SCOPE
of Broadcaster
.void setID(String name)
Broadcaster
.name
- ID of this Broadcaster
String getID()
Broadcaster
.Broadcaster
void resumeAll()
AtmosphereResource
) added via
addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource)
.void releaseExternalResources()
AtmosphereResource
and some resource (like database connections, JMS queue, etc.) needs to be
closed.void setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy policy)
BroadcasterLifeCyclePolicy
. Make sure you are selecting the right policy
to avoid unexpected situations.policy
- a BroadcasterLifeCyclePolicy
BroadcasterLifeCyclePolicy getBroadcasterLifeCyclePolicy()
BroadcasterLifeCyclePolicy
.BroadcasterLifeCyclePolicy
void addBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
b
- a BroadcasterLifeCyclePolicyListener
void removeBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
BroadcasterLifeCyclePolicyListener
.b
- a BroadcasterLifeCyclePolicyListener
boolean isDestroyed()
Broadcaster
has been destroyed.Broadcaster
has been destroyedFuture<Object> awaitAndBroadcast(Object t, long time, TimeUnit timeUnit)
AtmosphereResource
before broadcasting. This method will block until
addAtmosphereResource(AtmosphereResource)
gets invoked.Broadcaster addBroadcasterListener(BroadcasterListener b)
BroadcasterListener
.b
- a BroadcasterListener
Broadcaster removeBroadcasterListener(BroadcasterListener b)
BroadcasterListener
.b
- a BroadcasterListener
Copyright © 2015. All Rights Reserved.