org.glassfish.jersey.server
Class Broadcaster<T>

java.lang.Object
  extended by org.glassfish.jersey.server.Broadcaster<T>
All Implemented Interfaces:
BroadcasterListener<T>

public class Broadcaster<T>
extends Object
implements BroadcasterListener<T>

Used for broadcasting response chunks to multiple ChunkedResponse instances.

Author:
Pavel Bucek (pavel.bucek at oracle.com), Martin Matula (martin.matula at oracle.com)

Constructor Summary
  Broadcaster()
          Creates a new instance.
protected Broadcaster(boolean listen)
          Can be used by subclasses to override the default functionality of adding self to the set of listeners by passing false as a parameter.
 
Method Summary
 boolean add(ChunkedResponse<T> chunkedResponse)
          Register ChunkedResponse to this Broadcaster instance.
 boolean addBroadcasterListener(BroadcasterListener<T> listener)
          Register BroadcasterListener for Broadcaster events listening.
 void broadcast(T chunk)
          Broadcast a chunk to all registered ChunkedResponse instances.
 void closeAll()
          Close all registered ChunkedResponse instances.
 void onClose(ChunkedResponse<T> chunkedResponse)
          Called when the chunkedResponse has been closed (either by client closing the connection or by calling ChunkedResponse.close() on the server side.
 void onException(ChunkedResponse<T> chunkedResponse, Exception exception)
          Called when exception was thrown by a given chunked response when trying to write to it or close it.
 boolean remove(ChunkedResponse<T> chunkedResponse)
          Unregister ChunkedResponse from this Broadcaster instance.
 boolean removeBroadcasterListener(BroadcasterListener<T> listener)
          Unregister BroadcasterListener.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Broadcaster

public Broadcaster()
Creates a new instance. If this constructor is called by a subclass, it assumes the the reason for the subclass to exist is to implement onClose(ChunkedResponse) and onException(ChunkedResponse, Exception) methods, so it adds the newly created instance as the listener. To avoid this, subclasses may call Broadcaster(boolean) passing false as argument.


Broadcaster

protected Broadcaster(boolean listen)
Can be used by subclasses to override the default functionality of adding self to the set of listeners by passing false as a parameter.

Parameters:
listen - if true, registers this instance as a listener (default), otherwise it doesn't
See Also:
Broadcaster()
Method Detail

add

public final boolean add(ChunkedResponse<T> chunkedResponse)
Register ChunkedResponse to this Broadcaster instance.

Parameters:
chunkedResponse - ChunkedResponse to register.
Returns:
true if the instance was successfully registered, false if this instance was already in the list of registered chunked responses.

remove

public final boolean remove(ChunkedResponse<T> chunkedResponse)
Unregister ChunkedResponse from this Broadcaster instance. This method does not close the ChunkedResponse being unregistered.

Parameters:
chunkedResponse - ChunkedResponse instance to unregister from this broadcaster.
Returns:
true if the instance was unregistered, false if the instance wasn't found in the list of registered chunked responses.

addBroadcasterListener

public final boolean addBroadcasterListener(BroadcasterListener<T> listener)
Register BroadcasterListener for Broadcaster events listening.

Parameters:
listener - listener to be registered
Returns:
true if registered, false if the listener was already in the list

removeBroadcasterListener

public final boolean removeBroadcasterListener(BroadcasterListener<T> listener)
Unregister BroadcasterListener.

Parameters:
listener - listener to be unregistered
Returns:
true if unregistered, false if the listener was not found in the list of registered listeners

broadcast

public final void broadcast(T chunk)
Broadcast a chunk to all registered ChunkedResponse instances.

Parameters:
chunk - chunk to be sent.

closeAll

public final void closeAll()
Close all registered ChunkedResponse instances.


onException

public void onException(ChunkedResponse<T> chunkedResponse,
                        Exception exception)
Called when exception was thrown by a given chunked response when trying to write to it or close it. Can be implemented by subclasses to handle the event of exception thrown from a particular ChunkedResponse instance when trying to write to it or close it.

Specified by:
onException in interface BroadcasterListener<T>
Parameters:
chunkedResponse - instance that threw exception.
exception - exception that was thrown.

onClose

public void onClose(ChunkedResponse<T> chunkedResponse)
Called when the chunkedResponse has been closed (either by client closing the connection or by calling ChunkedResponse.close() on the server side. Can be implemented by subclasses to hadnle the event of ChunkedResponse being closed.

Specified by:
onClose in interface BroadcasterListener<T>
Parameters:
chunkedResponse - instance that was closed.


Copyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.