org.atmosphere.cpr
Class DefaultBroadcaster

java.lang.Object
  extended by org.atmosphere.cpr.DefaultBroadcaster
All Implemented Interfaces:
Broadcaster
Direct Known Subclasses:
GAEDefaultBroadcaster, 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
 class DefaultBroadcaster.Entry
           
 
Nested classes/interfaces inherited from interface org.atmosphere.cpr.Broadcaster
Broadcaster.SCOPE
 
Field Summary
protected  BroadcasterConfig bc
           
protected  ConcurrentLinkedQueue<DefaultBroadcaster.Entry> delayedBroadcast
           
protected  ConcurrentLinkedQueue<AtmosphereResource> events
           
protected  BlockingQueue<DefaultBroadcaster.Entry> messages
           
protected  String name
           
protected  Broadcaster.SCOPE scope
           
protected  AtomicBoolean started
           
 
Constructor Summary
DefaultBroadcaster()
           
DefaultBroadcaster(BroadcasterConfig bc, String name)
           
DefaultBroadcaster(String name)
           
 
Method Summary
 AtmosphereResource addAtmosphereResource(AtmosphereResource r)
          Add a AtmosphereResource to the list of item to be notified when the Broadcaster.broadcast(java.lang.Object) is invoked.
protected  ConcurrentLinkedQueue<AtmosphereResource> atmosphereResources()
          Return the list of AtmosphereResource registered with this Broadcaster
protected  void broadcast(AtmosphereResource r, AtmosphereResourceEvent e)
           
 BroadcasterFuture<Object> broadcast(Object msg)
          Broadcast the Object to all suspended response, e.g.
 BroadcasterFuture<Object> broadcast(Object msg, AtmosphereResource r)
          Broadcast the Object to all suspended response, e.g.
 BroadcasterFuture<Object> broadcast(Object msg, Set<AtmosphereResource> subset)
          Broadcast the Object to all suspended response, e.g.
 Future<Object> delayBroadcast(Object o)
          Delay the broadcast operation.
 Future<Object> delayBroadcast(Object o, long delay, TimeUnit t)
          Delay the broadcast operation.
 void destroy()
          Destroy this instance and shutdown it's associated ExecutorService
protected  Object filter(Object msg)
          Invoke the BroadcastFilter
 Iterator<AtmosphereResource> getAtmosphereResources()
          Return an Iterator of AtmosphereResource.
 BroadcasterConfig getBroadcasterConfig()
          Return the current BroadcasterConfig
 String getID()
          Return the id of this Broadcaster
 Broadcaster.SCOPE getScope()
          Return the Broadcaster.SCOPE
protected  void onException(Throwable t, AtmosphereResource r)
           
protected  void push(DefaultBroadcaster.Entry msg)
           
 AtmosphereResource removeAtmosphereResource(AtmosphereResource r)
          Remove a AtmosphereResource from the list of item to be notified when the Broadcaster.broadcast(java.lang.Object) is invoked.
 void resumeAll()
          Resume all suspended responses (AtmosphereResource) added via Broadcaster.addAtmosphereResource(org.atmosphere.cpr.AtmosphereResource).
 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 setID(String id)
          Set the id of this Broadcaster
 void setScope(Broadcaster.SCOPE scope)
          Set the scope.
protected  void start()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

events

protected final ConcurrentLinkedQueue<AtmosphereResource> events

bc

protected BroadcasterConfig bc

messages

protected final BlockingQueue<DefaultBroadcaster.Entry> messages

started

protected final AtomicBoolean started

scope

protected Broadcaster.SCOPE scope

name

protected String name

delayedBroadcast

protected final ConcurrentLinkedQueue<DefaultBroadcaster.Entry> delayedBroadcast
Constructor Detail

DefaultBroadcaster

public DefaultBroadcaster()

DefaultBroadcaster

public DefaultBroadcaster(String name)

DefaultBroadcaster

public DefaultBroadcaster(BroadcasterConfig bc,
                          String name)
Method Detail

destroy

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

Specified by:
destroy in interface Broadcaster

getAtmosphereResources

public Iterator<AtmosphereResource> getAtmosphereResources()
Return an Iterator of AtmosphereResource.

Specified by:
getAtmosphereResources in interface Broadcaster
Returns:
Iterator 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

start

protected void start()

push

protected void push(DefaultBroadcaster.Entry msg)

broadcast

protected void broadcast(AtmosphereResource r,
                         AtmosphereResourceEvent e)

onException

protected void onException(Throwable t,
                           AtmosphereResource r)

broadcast

public BroadcasterFuture<Object> broadcast(Object 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 BroadcasterFuture<Object> broadcast(Object 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()

broadcast

public BroadcasterFuture<Object> broadcast(Object 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 AtmosphereResource addAtmosphereResource(AtmosphereResource r)
Add a AtmosphereResource to the list of item to be notified when the Broadcaster.broadcast(java.lang.Object) is invoked.

Specified by:
addAtmosphereResource in interface Broadcaster
Parameters:
r - an AtmosphereResource
Returns:
AtmosphereResource if added, or null if it was already there.

removeAtmosphereResource

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

Specified by:
removeAtmosphereResource in interface Broadcaster
Parameters:
r - an AtmosphereResource
Returns:
AtmosphereResource if removed, or null if it was not.

atmosphereResources

protected ConcurrentLinkedQueue<AtmosphereResource> atmosphereResources()
Return the list of AtmosphereResource registered with this Broadcaster

Returns:

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 Future<Object> delayBroadcast(Object 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 Future<Object> delayBroadcast(Object 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()


Copyright © 2010 SUN Microsystems. All Rights Reserved.