public class DefaultBroadcaster extends java.lang.Object implements Broadcaster
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.| Modifier and Type | Class and Description |
|---|---|
protected static class |
DefaultBroadcaster.AsyncWriteToken |
Broadcaster.POLICY, Broadcaster.SCOPE| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
ASYNC_TOKEN |
protected java.util.concurrent.Future<?>[] |
asyncWriteFuture |
protected BroadcasterConfig |
bc |
protected java.util.concurrent.ConcurrentLinkedQueue<BroadcasterListener> |
broadcasterListeners |
protected java.util.concurrent.ConcurrentLinkedQueue<Entry> |
broadcastOnResume |
static java.lang.String |
CACHED |
protected AtmosphereConfig |
config |
protected java.util.concurrent.ConcurrentLinkedQueue<Entry> |
delayedBroadcast |
protected java.util.concurrent.atomic.AtomicBoolean |
destroyed |
protected java.util.concurrent.atomic.AtomicInteger |
dispatchThread |
protected java.util.concurrent.atomic.AtomicBoolean |
initialized |
protected java.util.concurrent.ConcurrentLinkedQueue<BroadcasterLifeCyclePolicyListener> |
lifeCycleListeners |
protected java.util.concurrent.BlockingQueue<Entry> |
messages |
protected java.lang.String |
name |
protected java.util.concurrent.Future<?>[] |
notifierFuture |
static int |
POLLING_DEFAULT |
protected java.util.concurrent.ConcurrentLinkedQueue<AtmosphereResource> |
resources |
protected Broadcaster.SCOPE |
scope |
protected java.util.concurrent.atomic.AtomicBoolean |
started |
protected org.atmosphere.cpr.DefaultBroadcaster.WriteQueue |
uniqueWriteQueue |
protected java.net.URI |
uri |
protected int |
waitTime |
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,org.atmosphere.cpr.DefaultBroadcaster.WriteQueue> |
writeQueues |
protected int |
writeTimeoutInSecond |
| Constructor and Description |
|---|
DefaultBroadcaster() |
| Modifier and Type | Method and Description |
|---|---|
Broadcaster |
addAtmosphereResource(AtmosphereResource r)
Add a
AtmosphereResource to the list of items to be notified when
the Broadcaster.broadcast(java.lang.Object) is invoked. |
void |
addBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
|
Broadcaster |
addBroadcasterListener(BroadcasterListener b)
Add a
BroadcasterListener. |
java.util.concurrent.Future<java.lang.Object> |
awaitAndBroadcast(java.lang.Object t,
long time,
java.util.concurrent.TimeUnit timeUnit)
Await for available
AtmosphereResource before broadcasting. |
java.util.concurrent.Future<java.lang.Object> |
broadcast(java.lang.Object msg)
Broadcast the
Object to all suspended responses, eg. |
java.util.concurrent.Future<java.lang.Object> |
broadcast(java.lang.Object msg,
AtmosphereResource r)
Broadcast the
Object to all suspended responses, eg. |
java.util.concurrent.Future<java.lang.Object> |
broadcast(java.lang.Object msg,
java.util.Set<AtmosphereResource> subset)
Broadcast the
Object to all suspended response, eg. |
protected void |
broadcastOnResume(AtmosphereResource r) |
java.util.concurrent.Future<java.lang.Object> |
broadcastOnResume(java.lang.Object msg)
Broadcast the
Object when an AtmosphereResource is resumed by a timeout or when using
AtmosphereResource.resume(). |
protected void |
cacheAndSuspend(AtmosphereResource r)
Look in the cache to see if there are messages available, and take the appropriate actions.
|
void |
cacheLostMessage(AtmosphereResource r)
Cache the message because an unexpected exception occurred.
|
void |
cacheLostMessage(AtmosphereResource r,
boolean force)
Cache the message because an unexpected exception occurred.
|
void |
cacheLostMessage(AtmosphereResource r,
DefaultBroadcaster.AsyncWriteToken token)
Cache the message because an unexpected exception occurred.
|
void |
cacheLostMessage(AtmosphereResource r,
DefaultBroadcaster.AsyncWriteToken token,
boolean force)
Cache the message because an unexpected exception occurred.
|
protected boolean |
checkCachedAndPush(AtmosphereResource r,
AtmosphereResourceEvent e) |
protected BroadcasterConfig |
createBroadcasterConfig(AtmosphereConfig config)
Create
BroadcasterConfig. |
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.
|
protected void |
deliverPush(Entry entry,
boolean rec) |
void |
destroy()
Destroy this instance and shutdown it's associated
ExecutorService. |
protected void |
dispatchMessages(Entry e) |
protected void |
entryDone(BroadcasterFuture<?> f) |
protected void |
executeAsyncWrite(DefaultBroadcaster.AsyncWriteToken token) |
protected void |
executeBlockingWrite(AtmosphereResource r,
Entry entry) |
protected java.lang.Object |
filter(java.lang.Object msg)
Invoke the
BroadcastFilter |
protected BroadcasterFuture<java.lang.Object> |
futureDone(java.lang.Object msg) |
protected java.lang.Runnable |
getAsyncWriteHandler(org.atmosphere.cpr.DefaultBroadcaster.WriteQueue writeQueue) |
java.util.Collection<AtmosphereResource> |
getAtmosphereResources()
Return an
List of AtmosphereResource. |
BroadcasterConfig |
getBroadcasterConfig()
Return the current
BroadcasterConfig. |
protected java.lang.Runnable |
getBroadcastHandler() |
java.lang.String |
getID()
Return the ID of this
Broadcaster. |
Broadcaster.SCOPE |
getScope()
Return the
Broadcaster.SCOPE. |
Broadcaster |
initialize(java.lang.String name,
AtmosphereConfig config) |
Broadcaster |
initialize(java.lang.String name,
java.net.URI uri,
AtmosphereConfig config)
Configure a Broadcaster.
|
protected void |
invokeOnStateChange(AtmosphereResource r,
AtmosphereResourceEvent e) |
boolean |
isDestroyed()
Return true if this
Broadcaster has been destroyed. |
protected void |
killReactiveThreads() |
protected void |
notifyAndAdd(AtmosphereResource r) |
protected void |
notifyBroadcastListener() |
protected void |
notifyDestroyListener() |
protected void |
notifyEmptyListener() |
protected void |
notifyIdleListener() |
protected void |
notifyOnAddAtmosphereResourceListener(AtmosphereResource r) |
protected void |
notifyOnRemoveAtmosphereResourceListener(AtmosphereResource r) |
void |
onException(java.lang.Throwable t,
AtmosphereResource ar) |
void |
onException(java.lang.Throwable t,
AtmosphereResource ar,
boolean notifyAndCache) |
protected boolean |
perRequestFilter(AtmosphereResource r,
Entry msg) |
protected void |
prepareInvokeOnStateChange(AtmosphereResource r,
AtmosphereResourceEvent e) |
protected void |
push(Entry entry) |
protected void |
queueWriteIO(AtmosphereResource r,
Entry entry) |
protected int |
reactiveThreadsCount()
Return the default number of reactive threads that will be waiting for work when a broadcast operation
is executed.
|
void |
releaseExternalResources()
Release external resources associated with this Broadcaster.
|
Broadcaster |
removeAtmosphereResource(AtmosphereResource r)
Remove a
AtmosphereResource from the list of s to be notified when
the Broadcaster.broadcast(java.lang.Object) is invoked. |
protected Broadcaster |
removeAtmosphereResource(AtmosphereResource r,
boolean executeDone) |
void |
removeBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
Remove a
BroadcasterLifeCyclePolicyListener. |
Broadcaster |
removeBroadcasterListener(BroadcasterListener b)
Remove a
BroadcasterListener. |
void |
resumeAll()
Resume all suspended responses (
AtmosphereResource) added via
Broadcaster.addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource). |
protected boolean |
retrieveTrackedBroadcast(AtmosphereResource r,
AtmosphereResourceEvent e) |
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.
|
void |
setBroadcasterConfig(BroadcasterConfig bc)
Set the
BroadcasterConfig instance. |
void |
setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy lifeCyclePolicy)
Set the
BroadcasterLifeCyclePolicy. |
void |
setID(java.lang.String id)
Set the ID of this
Broadcaster. |
void |
setScope(Broadcaster.SCOPE scope)
Set the scope.
|
void |
setSuspendPolicy(long maxSuspendResource,
Broadcaster.POLICY policy)
Set the maximum number of suspended
AtmosphereResources. |
protected void |
spawnReactor() |
protected void |
start() |
java.lang.String |
toString() |
public static final int POLLING_DEFAULT
public static final java.lang.String CACHED
public static final java.lang.String ASYNC_TOKEN
protected final java.util.concurrent.ConcurrentLinkedQueue<AtmosphereResource> resources
protected BroadcasterConfig bc
protected final java.util.concurrent.BlockingQueue<Entry> messages
protected final java.util.concurrent.ConcurrentLinkedQueue<BroadcasterListener> broadcasterListeners
protected final java.util.concurrent.atomic.AtomicBoolean started
protected final java.util.concurrent.atomic.AtomicBoolean initialized
protected final java.util.concurrent.atomic.AtomicBoolean destroyed
protected Broadcaster.SCOPE scope
protected java.lang.String name
protected final java.util.concurrent.ConcurrentLinkedQueue<Entry> delayedBroadcast
protected final java.util.concurrent.ConcurrentLinkedQueue<Entry> broadcastOnResume
protected final java.util.concurrent.ConcurrentLinkedQueue<BroadcasterLifeCyclePolicyListener> lifeCycleListeners
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,org.atmosphere.cpr.DefaultBroadcaster.WriteQueue> writeQueues
protected final org.atmosphere.cpr.DefaultBroadcaster.WriteQueue uniqueWriteQueue
protected final java.util.concurrent.atomic.AtomicInteger dispatchThread
protected java.util.concurrent.Future<?>[] notifierFuture
protected java.util.concurrent.Future<?>[] asyncWriteFuture
protected java.net.URI uri
protected AtmosphereConfig config
protected int writeTimeoutInSecond
protected int waitTime
public Broadcaster initialize(java.lang.String name, java.net.URI uri, AtmosphereConfig config)
Broadcasterinitialize in interface Broadcasterconfig - an AtmosphereConfigpublic Broadcaster initialize(java.lang.String name, AtmosphereConfig config)
protected BroadcasterConfig createBroadcasterConfig(AtmosphereConfig config)
BroadcasterConfig.config - the AtmosphereConfigBroadcasterConfigpublic void destroy()
BroadcasterExecutorService.destroy in interface Broadcasterpublic java.util.Collection<AtmosphereResource> getAtmosphereResources()
BroadcasterList of AtmosphereResource.getAtmosphereResources in interface BroadcasterList of AtmosphereResource associated with this Broadcaster.Broadcaster.addAtmosphereResource(AtmosphereResource)public void setScope(Broadcaster.SCOPE scope)
BroadcastersetScope in interface Broadcasterscope - Broadcaster.SCOPE to set.public Broadcaster.SCOPE getScope()
BroadcasterBroadcaster.SCOPE.getScope in interface BroadcasterBroadcaster.SCOPE of Broadcaster.public void setID(java.lang.String id)
BroadcasterBroadcaster.setID in interface Broadcasterid - ID of this Broadcasterpublic java.lang.String getID()
BroadcasterBroadcaster.getID in interface BroadcasterBroadcasterpublic void resumeAll()
BroadcasterAtmosphereResource) added via
Broadcaster.addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).resumeAll in interface Broadcasterpublic void releaseExternalResources()
BroadcasterAtmosphereResource and some resource (like database connections, JMS queue, etc.) needs to be
closed.releaseExternalResources in interface Broadcasterpublic void setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy lifeCyclePolicy)
BroadcasterBroadcasterLifeCyclePolicy. Make sure you are selecting the right policy
to avoid unexpected situations.setBroadcasterLifeCyclePolicy in interface BroadcasterlifeCyclePolicy - a BroadcasterLifeCyclePolicypublic void addBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
BroadcasteraddBroadcasterLifeCyclePolicyListener in interface Broadcasterb - a BroadcasterLifeCyclePolicyListenerpublic void removeBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
BroadcasterBroadcasterLifeCyclePolicyListener.removeBroadcasterLifeCyclePolicyListener in interface Broadcasterb - a BroadcasterLifeCyclePolicyListenerpublic boolean isDestroyed()
BroadcasterBroadcaster has been destroyed.isDestroyed in interface BroadcasterBroadcaster has been destroyedpublic java.util.concurrent.Future<java.lang.Object> awaitAndBroadcast(java.lang.Object t,
long time,
java.util.concurrent.TimeUnit timeUnit)
BroadcasterAtmosphereResource before broadcasting. This method will block until
Broadcaster.addAtmosphereResource(AtmosphereResource) gets invoked.awaitAndBroadcast in interface Broadcasterpublic Broadcaster addBroadcasterListener(BroadcasterListener b)
BroadcasterBroadcasterListener.addBroadcasterListener in interface Broadcasterb - a BroadcasterListenerpublic Broadcaster removeBroadcasterListener(BroadcasterListener b)
BroadcasterBroadcasterListener.removeBroadcasterListener in interface Broadcasterb - a BroadcasterListenerprotected java.lang.Runnable getBroadcastHandler()
protected java.lang.Runnable getAsyncWriteHandler(org.atmosphere.cpr.DefaultBroadcaster.WriteQueue writeQueue)
protected void start()
protected void spawnReactor()
protected void killReactiveThreads()
protected int reactiveThreadsCount()
protected void push(Entry entry)
protected void deliverPush(Entry entry, boolean rec)
protected void queueWriteIO(AtmosphereResource r, Entry entry) throws java.lang.InterruptedException
java.lang.InterruptedExceptionprotected void executeBlockingWrite(AtmosphereResource r, Entry entry) throws java.lang.InterruptedException
java.lang.InterruptedExceptionprotected boolean perRequestFilter(AtmosphereResource r, Entry msg)
protected void executeAsyncWrite(DefaultBroadcaster.AsyncWriteToken token)
protected boolean checkCachedAndPush(AtmosphereResource r, AtmosphereResourceEvent e)
protected boolean retrieveTrackedBroadcast(AtmosphereResource r, AtmosphereResourceEvent e)
protected void invokeOnStateChange(AtmosphereResource r, AtmosphereResourceEvent e)
protected void prepareInvokeOnStateChange(AtmosphereResource r, AtmosphereResourceEvent e)
public void onException(java.lang.Throwable t,
AtmosphereResource ar)
public void onException(java.lang.Throwable t,
AtmosphereResource ar,
boolean notifyAndCache)
public void cacheLostMessage(AtmosphereResource r)
r - AtmosphereResourcepublic void cacheLostMessage(AtmosphereResource r, boolean force)
r - AtmosphereResourcepublic void cacheLostMessage(AtmosphereResource r, DefaultBroadcaster.AsyncWriteToken token)
r - AtmosphereResourcepublic void cacheLostMessage(AtmosphereResource r, DefaultBroadcaster.AsyncWriteToken token, boolean force)
r - AtmosphereResourcepublic void setSuspendPolicy(long maxSuspendResource,
Broadcaster.POLICY policy)
BroadcasterAtmosphereResources. 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.setSuspendPolicy in interface BroadcastermaxSuspendResource - maximum number of suspended AtmosphereResourcepublic java.util.concurrent.Future<java.lang.Object> broadcast(java.lang.Object msg)
BroadcasterObject to all suspended responses, eg. invoke AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent).broadcast in interface Broadcastermsg - the Object to be broadcastedFuture that can be used to synchronize using the Future.get()protected BroadcasterFuture<java.lang.Object> futureDone(java.lang.Object msg)
protected void dispatchMessages(Entry e)
protected java.lang.Object filter(java.lang.Object msg)
BroadcastFiltermsg - public java.util.concurrent.Future<java.lang.Object> broadcast(java.lang.Object msg,
AtmosphereResource r)
BroadcasterObject to all suspended responses, eg. invoke
AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent) with an instance of AtmosphereResource, representing
a single suspended response.broadcast in interface Broadcastermsg - the Object to be broadcastedr - an AtmosphereResourceFuture that can be used to synchronize using the Future.get()public java.util.concurrent.Future<java.lang.Object> broadcastOnResume(java.lang.Object msg)
BroadcasterObject when an AtmosphereResource is resumed by a timeout or when using
AtmosphereResource.resume().broadcastOnResume in interface Broadcastermsg - the Object to be broadcastedFuture that can be used to synchronize using the Future.get()protected void broadcastOnResume(AtmosphereResource r)
public java.util.concurrent.Future<java.lang.Object> broadcast(java.lang.Object msg,
java.util.Set<AtmosphereResource> subset)
BroadcasterObject to all suspended response, eg. invoke
AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent) with a Set of AtmosphereResource,
representing a set of AtmosphereHandler.broadcast in interface Broadcastermsg - the Object to be broadcastedsubset - a Set of AtmosphereResourceFuture that can be used to synchronize using the Future.get()public Broadcaster addAtmosphereResource(AtmosphereResource r)
BroadcasterAtmosphereResource to the list of items to be notified when
the Broadcaster.broadcast(java.lang.Object) is invoked.
It is strongly recommended to suspend the AtmosphereResource before
adding it to a Broadcaster.addAtmosphereResource in interface Broadcasterr - an AtmosphereResourceprotected void cacheAndSuspend(AtmosphereResource r)
r - AtmosphereResourceprotected void notifyAndAdd(AtmosphereResource r)
protected void entryDone(BroadcasterFuture<?> f)
protected void notifyBroadcastListener()
protected void notifyOnAddAtmosphereResourceListener(AtmosphereResource r)
protected void notifyOnRemoveAtmosphereResourceListener(AtmosphereResource r)
public Broadcaster removeAtmosphereResource(AtmosphereResource r)
BroadcasterAtmosphereResource from the list of s to be notified when
the Broadcaster.broadcast(java.lang.Object) is invoked.removeAtmosphereResource in interface Broadcasterr - an AtmosphereResourceprotected Broadcaster removeAtmosphereResource(AtmosphereResource r, boolean executeDone)
protected void notifyIdleListener()
protected void notifyDestroyListener()
protected void notifyEmptyListener()
public void setBroadcasterConfig(BroadcasterConfig bc)
BroadcasterBroadcasterConfig instance.setBroadcasterConfig in interface Broadcasterbc - Configuration to be set.public BroadcasterConfig getBroadcasterConfig()
BroadcasterBroadcasterConfig.getBroadcasterConfig in interface BroadcasterBroadcasterConfigpublic java.util.concurrent.Future<java.lang.Object> delayBroadcast(java.lang.Object o)
BroadcasterObject will be broadcasted when Broadcaster.broadcast(java.lang.Object)
is invoked the first time.delayBroadcast in interface Broadcastero - the Object to be broadcastedFuture that can be used to synchronize using the Future.get()public java.util.concurrent.Future<java.lang.Object> delayBroadcast(java.lang.Object o,
long delay,
java.util.concurrent.TimeUnit t)
BroadcasterObject will be broadcasted once the
specified delay expires or when Broadcaster.broadcast(java.lang.Object) is invoked the first time.delayBroadcast in interface Broadcastero - 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()public java.util.concurrent.Future<java.lang.Object> scheduleFixedBroadcast(java.lang.Object o,
long period,
java.util.concurrent.TimeUnit t)
BroadcasterObject 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 Broadcaster.broadcast(java.lang.Object)
is invoked.scheduleFixedBroadcast in interface Broadcastero - 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()public java.util.concurrent.Future<java.lang.Object> scheduleFixedBroadcast(java.lang.Object o,
long waitFor,
long period,
java.util.concurrent.TimeUnit t)
BroadcasterObject will be broadcasted after every period
specified time frame expires. If the TimeUnit is set null, the
Object will be broadcasted when the first Broadcaster.broadcast(java.lang.Object) is invoked.scheduleFixedBroadcast in interface Broadcastero - 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()public java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2018. All Rights Reserved.