org.atmosphere.cpr
Class DefaultBroadcaster

java.lang.Object
  extended by org.atmosphere.cpr.DefaultBroadcaster
All Implemented Interfaces:
Broadcaster
Direct Known Subclasses:
AbstractBroadcasterProxy, ExcludeSessionBroadcaster, SimpleBroadcaster

public class DefaultBroadcaster
extends Object
implements Broadcaster

Broadcaster implementation.

Broadcast messages to suspended response using the caller's Thread. This basic Broadcaster use an ExecutorService to broadcast message, 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
protected static class DefaultBroadcaster.AsyncWriteToken
           
static class DefaultBroadcaster.Entry
           
 
Nested classes/interfaces inherited from interface org.atmosphere.cpr.Broadcaster
Broadcaster.POLICY, Broadcaster.SCOPE
 
Field Summary
static String ASYNC_TOKEN
           
protected  Future<?> asyncWriteFuture
           
protected  BlockingQueue<DefaultBroadcaster.AsyncWriteToken> asyncWriteQueue
           
protected  BroadcasterConfig bc
           
 BroadcasterCache broadcasterCache
           
protected  CopyOnWriteArrayList<BroadcasterListener> broadcasterListeners
           
protected  ConcurrentLinkedQueue<DefaultBroadcaster.Entry> broadcastOnResume
           
static String CACHED
           
protected  BroadcasterCache.STRATEGY cacheStrategy
           
protected  AtmosphereConfig config
           
protected  ConcurrentLinkedQueue<DefaultBroadcaster.Entry> delayedBroadcast
           
protected  AtomicBoolean destroyed
           
protected  ConcurrentLinkedQueue<BroadcasterLifeCyclePolicyListener> lifeCycleListeners
           
protected  BlockingQueue<DefaultBroadcaster.Entry> messages
           
protected  String name
           
protected  Future<?> notifierFuture
           
protected  ConcurrentLinkedQueue<AtmosphereResource> resources
           
protected  Broadcaster.SCOPE scope
           
protected  AtomicBoolean started
           
protected  URI uri
           
 
Constructor Summary
DefaultBroadcaster(String name, AtmosphereConfig config)
           
DefaultBroadcaster(String name, URI uri, AtmosphereConfig config)
           
 
Method Summary
 Broadcaster addAtmosphereResource(AtmosphereResource r)
          Add a AtmosphereResource to the list of item to be notified when the Broadcaster.broadcast(T) is invoked.
 void addBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
          Add a BroadcasterLifeCyclePolicyListener
 Broadcaster addBroadcasterListener(BroadcasterListener b)
          Add a BroadcasterListener
<T> Future<T>
awaitAndBroadcast(T t, long time, TimeUnit timeUnit)
          Await for available AtmosphereResource before broadcasting.
protected  void broadcast(AtmosphereResource r, AtmosphereResourceEvent e)
           
<T> Future<T>
broadcast(T msg)
          Broadcast the Object to all suspended response, e.g.
<T> Future<T>
broadcast(T msg, AtmosphereResource r)
          Broadcast the Object to all suspended response, e.g.
<T> Future<T>
broadcast(T msg, Set<AtmosphereResource> subset)
          Broadcast the Object to all suspended response, e.g.
protected  void broadcastOnResume(AtmosphereResource r)
           
<T> Future<T>
broadcastOnResume(T msg)
          Broadcast the Object when an AtmosphereResource is resumed by a timeout or when using AtmosphereResource.resume()
 void cacheLostMessage(AtmosphereResource r)
          Cache the message because an unexpected exception occurred.
 void cacheLostMessage(AtmosphereResource r, DefaultBroadcaster.AsyncWriteToken token)
          Cache the message because an unexpected exception occurred.
protected  void checkCachedAndPush(AtmosphereResource r, AtmosphereResourceEvent e)
           
protected  BroadcasterConfig createBroadcasterConfig(AtmosphereConfig config)
          Create BroadcasterConfig
<T> Future<T>
delayBroadcast(T o)
          Delay the broadcast operation.
<T> Future<T>
delayBroadcast(T o, long delay, TimeUnit t)
          Delay the broadcast operation.
 void destroy()
          Destroy this instance and shutdown it's associated ExecutorService
protected  void executeAsyncWrite(DefaultBroadcaster.AsyncWriteToken token)
           
protected  Object filter(Object msg)
          Invoke the BroadcastFilter
protected  Runnable getAsyncWriteHandler()
           
 Collection<AtmosphereResource> getAtmosphereResources()
          Return an List of AtmosphereResource.
 BroadcasterConfig getBroadcasterConfig()
          Return the current BroadcasterConfig
protected  Runnable getBroadcastHandler()
           
 String getID()
          Return the id of this Broadcaster
 Broadcaster.SCOPE getScope()
          Return the Broadcaster.SCOPE
 boolean isDestroyed()
          Return true if that Broadcaster has been destroyed
 void notifyOnPostCreate()
           
 void onException(Throwable t, AtmosphereResource ar)
           
protected  Object perRequestFilter(AtmosphereResource r, DefaultBroadcaster.Entry msg)
           
protected  void push(DefaultBroadcaster.Entry entry)
           
protected  void push(DefaultBroadcaster.Entry entry, boolean rec)
           
protected  void queueWriteIO(AtmosphereResource r, Object finalMsg, DefaultBroadcaster.Entry entry)
           
 void releaseExternalResources()
          Release external resources associated with this Broadcaster.
 Broadcaster removeAtmosphereResource(AtmosphereResource r)
          Remove a AtmosphereResource from the list of item to be notified when the Broadcaster.broadcast(T) 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)
           
 Future<?> scheduleFixedBroadcast(Object o, long waitFor, long period, TimeUnit t)
          Broadcast periodically.
 Future<?> scheduleFixedBroadcast(Object o, long period, TimeUnit t)
          Broadcast periodically.
 void setBroadcasterConfig(BroadcasterConfig bc)
          Set the BroadcasterConfig instance.
 void setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy lifeCyclePolicy)
          Set the BroadcasterLifeCyclePolicy.
 void setID(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 AtmosphereResource.
protected  void start()
           
 String toString()
           
protected  void trackBroadcastMessage(AtmosphereResource r, Object msg)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CACHED

public static final String CACHED

ASYNC_TOKEN

public static final String ASYNC_TOKEN

resources

protected final ConcurrentLinkedQueue<AtmosphereResource> resources

bc

protected BroadcasterConfig bc

messages

protected final BlockingQueue<DefaultBroadcaster.Entry> messages

asyncWriteQueue

protected final BlockingQueue<DefaultBroadcaster.AsyncWriteToken> asyncWriteQueue

broadcasterListeners

protected final CopyOnWriteArrayList<BroadcasterListener> broadcasterListeners

started

protected final AtomicBoolean started

destroyed

protected final AtomicBoolean destroyed

scope

protected Broadcaster.SCOPE scope

name

protected String name

delayedBroadcast

protected final ConcurrentLinkedQueue<DefaultBroadcaster.Entry> delayedBroadcast

broadcastOnResume

protected final ConcurrentLinkedQueue<DefaultBroadcaster.Entry> broadcastOnResume

lifeCycleListeners

protected final ConcurrentLinkedQueue<BroadcasterLifeCyclePolicyListener> lifeCycleListeners

notifierFuture

protected Future<?> notifierFuture

asyncWriteFuture

protected Future<?> asyncWriteFuture

broadcasterCache

public BroadcasterCache broadcasterCache

uri

protected URI uri

config

protected AtmosphereConfig config

cacheStrategy

protected BroadcasterCache.STRATEGY cacheStrategy
Constructor Detail

DefaultBroadcaster

public DefaultBroadcaster(String name,
                          URI uri,
                          AtmosphereConfig config)

DefaultBroadcaster

public DefaultBroadcaster(String name,
                          AtmosphereConfig config)
Method Detail

createBroadcasterConfig

protected BroadcasterConfig createBroadcasterConfig(AtmosphereConfig config)
Create BroadcasterConfig

Parameters:
config - the AtmosphereConfig
Returns:
an instance of BroadcasterConfig

destroy

public void destroy()
Destroy this instance and shutdown it's associated ExecutorService

Specified by:
destroy in interface Broadcaster

getAtmosphereResources

public Collection<AtmosphereResource> getAtmosphereResources()
Return an List of AtmosphereResource.

Specified by:
getAtmosphereResources in interface Broadcaster
Returns:
List of AtmosphereResource associated with this Broadcaster.
See Also:
Broadcaster.addAtmosphereResource(AtmosphereResource)

setScope

public void setScope(Broadcaster.SCOPE scope)
Set the scope.

Specified by:
setScope in interface Broadcaster
Parameters:
scope - Broadcaster.SCOPE to set.

getScope

public Broadcaster.SCOPE getScope()
Return the Broadcaster.SCOPE

Specified by:
getScope in interface Broadcaster
Returns:
Broadcaster.SCOPE of Broadcaster.

setID

public void setID(String id)
Set the id of this Broadcaster

Specified by:
setID in interface Broadcaster
Parameters:
id - ID of this Broadcaster

getID

public String getID()
Return the id of this Broadcaster

Specified by:
getID in interface Broadcaster
Returns:
the id of this Broadcaster

resumeAll

public void resumeAll()
Resume all suspended responses (AtmosphereResource) added via Broadcaster.addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).

Specified by:
resumeAll in interface Broadcaster

releaseExternalResources

public void releaseExternalResources()
Release external resources associated with this Broadcaster. This is useful when a Broadcaster no longer have suspended AtmosphereResource and some resource (like database connections, JMS queue, etc.) needs to be closed.

Specified by:
releaseExternalResources in interface Broadcaster

setBroadcasterLifeCyclePolicy

public void setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy lifeCyclePolicy)
Set the BroadcasterLifeCyclePolicy. Make sure you are selecting the right policy to avoid unexpected situation.

Specified by:
setBroadcasterLifeCyclePolicy in interface Broadcaster
Parameters:
lifeCyclePolicy - a BroadcasterLifeCyclePolicy

addBroadcasterLifeCyclePolicyListener

public void addBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
Add a BroadcasterLifeCyclePolicyListener

Specified by:
addBroadcasterLifeCyclePolicyListener in interface Broadcaster
Parameters:
b - a BroadcasterLifeCyclePolicyListener

removeBroadcasterLifeCyclePolicyListener

public void removeBroadcasterLifeCyclePolicyListener(BroadcasterLifeCyclePolicyListener b)
Remove a BroadcasterLifeCyclePolicyListener

Specified by:
removeBroadcasterLifeCyclePolicyListener in interface Broadcaster
Parameters:
b - a BroadcasterLifeCyclePolicyListener

isDestroyed

public boolean isDestroyed()
Return true if that Broadcaster has been destroyed

Specified by:
isDestroyed in interface Broadcaster
Returns:
true if that Broadcaster has been destroyed

awaitAndBroadcast

public <T> Future<T> awaitAndBroadcast(T t,
                                       long time,
                                       TimeUnit timeUnit)
Await for available AtmosphereResource before broadcasting. This method will block until Broadcaster.addAtmosphereResource(AtmosphereResource) gets invoked.

Specified by:
awaitAndBroadcast in interface Broadcaster

addBroadcasterListener

public Broadcaster addBroadcasterListener(BroadcasterListener b)
Add a BroadcasterListener

Specified by:
addBroadcasterListener in interface Broadcaster
Parameters:
b - a BroadcasterListener
Returns:
this

removeBroadcasterListener

public Broadcaster removeBroadcasterListener(BroadcasterListener b)
Remove a BroadcasterListener

Specified by:
removeBroadcasterListener in interface Broadcaster
Parameters:
b - a BroadcasterListener
Returns:
this

getBroadcastHandler

protected Runnable getBroadcastHandler()

start

protected void start()

push

protected void push(DefaultBroadcaster.Entry entry)

push

protected void push(DefaultBroadcaster.Entry entry,
                    boolean rec)

queueWriteIO

protected void queueWriteIO(AtmosphereResource r,
                            Object finalMsg,
                            DefaultBroadcaster.Entry entry)
                     throws InterruptedException
Throws:
InterruptedException

perRequestFilter

protected Object perRequestFilter(AtmosphereResource r,
                                  DefaultBroadcaster.Entry msg)

executeAsyncWrite

protected void executeAsyncWrite(DefaultBroadcaster.AsyncWriteToken token)

getAsyncWriteHandler

protected Runnable getAsyncWriteHandler()

checkCachedAndPush

protected void checkCachedAndPush(AtmosphereResource r,
                                  AtmosphereResourceEvent e)

retrieveTrackedBroadcast

protected boolean retrieveTrackedBroadcast(AtmosphereResource r,
                                           AtmosphereResourceEvent e)

trackBroadcastMessage

protected void trackBroadcastMessage(AtmosphereResource r,
                                     Object msg)

broadcast

protected void broadcast(AtmosphereResource r,
                         AtmosphereResourceEvent e)

onException

public void onException(Throwable t,
                        AtmosphereResource ar)

cacheLostMessage

public void cacheLostMessage(AtmosphereResource r)
Cache the message because an unexpected exception occurred.

Parameters:
r -

cacheLostMessage

public void cacheLostMessage(AtmosphereResource r,
                             DefaultBroadcaster.AsyncWriteToken token)
Cache the message because an unexpected exception occurred.

Parameters:
r -

setSuspendPolicy

public void setSuspendPolicy(long maxSuspendResource,
                             Broadcaster.POLICY policy)
Description copied from interface: Broadcaster
Set the maximum number of suspended AtmosphereResource. If the max 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 uunlimited.

Specified by:
setSuspendPolicy in interface Broadcaster
Parameters:
maxSuspendResource - max suspended

broadcast

public <T> Future<T> broadcast(T msg)
Broadcast the Object to all suspended response, e.g. invoke AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent).

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - and Object to be broadcasted.
Returns:
a Future that can be used to synchronize using the Future.get()

filter

protected Object filter(Object msg)
Invoke the BroadcastFilter

Parameters:
msg -
Returns:

broadcast

public <T> Future<T> broadcast(T msg,
                               AtmosphereResource r)
Broadcast the Object to all suspended response, e.g. invoke AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent) with an instance of AtmosphereResource, representing a single suspended response..

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - and Object to be broadcasted.
r - an AtmosphereResource
Returns:
a Future that can be used to synchronize using the Future.get()

broadcastOnResume

public <T> Future<T> broadcastOnResume(T msg)
Broadcast the Object when an AtmosphereResource is resumed by a timeout or when using AtmosphereResource.resume()

Specified by:
broadcastOnResume in interface Broadcaster
Parameters:
msg - and Object to be broadcasted.
Returns:
a Future that can be used to synchronize using the Future.get()

broadcastOnResume

protected void broadcastOnResume(AtmosphereResource r)

broadcast

public <T> Future<T> broadcast(T msg,
                               Set<AtmosphereResource> subset)
Broadcast the Object to all suspended response, e.g. invoke AtmosphereHandler.onStateChange(org.atmosphere.cpr.AtmosphereResourceEvent) with a Set of AtmosphereResource, representing a set of AtmosphereHandler.

Specified by:
broadcast in interface Broadcaster
Parameters:
msg - and Object to be broadcasted.
subset - a Set of AtmosphereResource
Returns:
a Future that can be used to synchronize using the Future.get()

addAtmosphereResource

public Broadcaster addAtmosphereResource(AtmosphereResource r)
Add a AtmosphereResource to the list of item to be notified when the Broadcaster.broadcast(T) is invoked.

Specified by:
addAtmosphereResource in interface Broadcaster
Parameters:
r - an AtmosphereResource
Returns:
this

removeAtmosphereResource

public Broadcaster removeAtmosphereResource(AtmosphereResource r)
Remove a AtmosphereResource from the list of item to be notified when the Broadcaster.broadcast(T) is invoked.

Specified by:
removeAtmosphereResource in interface Broadcaster
Parameters:
r - an AtmosphereResource
Returns:
this

removeAtmosphereResource

protected Broadcaster removeAtmosphereResource(AtmosphereResource r,
                                               boolean executeDone)

setBroadcasterConfig

public void setBroadcasterConfig(BroadcasterConfig bc)
Set the BroadcasterConfig instance.

Specified by:
setBroadcasterConfig in interface Broadcaster
Parameters:
bc -

getBroadcasterConfig

public BroadcasterConfig getBroadcasterConfig()
Return the current BroadcasterConfig

Specified by:
getBroadcasterConfig in interface Broadcaster
Returns:
the current BroadcasterConfig

delayBroadcast

public <T> Future<T> delayBroadcast(T o)
Delay the broadcast operation. The Object will be broadcasted when the first Broadcaster.broadcast(java.lang.Object) is invoked.

Specified by:
delayBroadcast in interface Broadcaster
Parameters:
o - and Object to be broadcasted.
Returns:
a Future that can be used to synchronize using the Future.get()

delayBroadcast

public <T> Future<T> delayBroadcast(T o,
                                    long delay,
                                    TimeUnit t)
Delay the broadcast operation. The Object will be broadcasted once the specified delay expires or when the first Broadcaster.broadcast(java.lang.Object)

Specified by:
delayBroadcast in interface Broadcaster
Parameters:
o - and Object to be broadcasted.
delay - Amount of time to delay.
t - a TimeUnit of delay.
Returns:
a Future that can be used to synchronize using the Future.get()

scheduleFixedBroadcast

public Future<?> scheduleFixedBroadcast(Object o,
                                        long period,
                                        TimeUnit t)
Broadcast periodically. The 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 Broadcaster.broadcast(java.lang.Object) is invoked.

Specified by:
scheduleFixedBroadcast in interface Broadcaster
Parameters:
o - and Object to be broadcasted.
period - Every so often broadcast.
t - a TimeUnit of period.
Returns:
a Future that can be used to synchronize using the Future.get()

scheduleFixedBroadcast

public Future<?> scheduleFixedBroadcast(Object o,
                                        long waitFor,
                                        long period,
                                        TimeUnit t)
Broadcast periodically. The 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 Broadcaster.broadcast(java.lang.Object) * is invoked.

Specified by:
scheduleFixedBroadcast in interface Broadcaster
Parameters:
o - and Object to be broadcasted.
waitFor - Wait for that long before first broadcast.
period - The period inbetween broadcast.
t - a TimeUnit of waitFor and period.
Returns:
a Future that can be used to synchronize using the Future.get()

toString

public String toString()
Overrides:
toString in class Object

notifyOnPostCreate

public void notifyOnPostCreate()


Copyright © 2012. All Rights Reserved.