Package org.glassfish.jersey.server
Class Broadcaster<T>
- java.lang.Object
-
- org.glassfish.jersey.server.Broadcaster<T>
-
- Type Parameters:
T
- broadcast type.
- All Implemented Interfaces:
BroadcasterListener<T>
public class Broadcaster<T> extends java.lang.Object implements BroadcasterListener<T>
Used for broadcasting response chunks to multipleChunkedOutput
instances.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interface
Broadcaster.Task<T>
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentLinkedQueue<ChunkedOutput<T>>
chunkedOutputs
private java.util.concurrent.CopyOnWriteArrayList<BroadcasterListener<T>>
listeners
-
Constructor Summary
Constructors Modifier Constructor Description Broadcaster()
Creates a new instance.protected
Broadcaster(java.lang.Class<? extends Broadcaster> subclass)
Can be used by subclasses to override the default functionality of adding self to the set oflisteners
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(BroadcasterListener<T> listener)
RegisterBroadcasterListener
forBroadcaster
events listening.<OUT extends ChunkedOutput<T>>
booleanadd(OUT chunkedOutput)
RegisterChunkedOutput
to thisBroadcaster
instance.void
broadcast(T chunk)
Broadcast a chunk to all registeredChunkedOutput
instances.void
closeAll()
Close all registeredChunkedOutput
instances.private void
fireOnClose(ChunkedOutput<T> chunkedOutput)
private void
fireOnException(ChunkedOutput<T> chunkedOutput, java.lang.Exception exception)
private void
forEachListener(Broadcaster.Task<BroadcasterListener<T>> t)
private void
forEachOutput(Broadcaster.Task<ChunkedOutput<T>> t)
void
onClose(ChunkedOutput<T> chunkedOutput)
Called when the chunkedOutput has been closed (either by client closing the connection or by callingChunkedOutput.close()
on the server side.void
onException(ChunkedOutput<T> chunkedOutput, java.lang.Exception exception)
Called when exception was thrown by a given chunked response when trying to write to it or close it.boolean
remove(BroadcasterListener<T> listener)
Un-registerBroadcasterListener
.<OUT extends ChunkedOutput<T>>
booleanremove(OUT chunkedOutput)
Un-registerChunkedOutput
from thisBroadcaster
instance.
-
-
-
Field Detail
-
listeners
private final java.util.concurrent.CopyOnWriteArrayList<BroadcasterListener<T>> listeners
-
chunkedOutputs
private final java.util.concurrent.ConcurrentLinkedQueue<ChunkedOutput<T>> chunkedOutputs
-
-
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 implementonClose(ChunkedOutput)
andonException(ChunkedOutput, Exception)
methods, so it adds the newly created instance as the listener. To avoid this, subclasses may callBroadcaster(Class)
passing their class as an argument.
-
Broadcaster
protected Broadcaster(java.lang.Class<? extends Broadcaster> subclass)
Can be used by subclasses to override the default functionality of adding self to the set oflisteners
. If creating a direct instance of a subclass passed in the parameter, the broadcaster will not register itself as a listener.- Parameters:
subclass
- subclass of Broadcaster that should not be registered as a listener - if creating a direct instance of this subclass, this constructor will not register the new instance as a listener.- See Also:
Broadcaster()
-
-
Method Detail
-
add
public <OUT extends ChunkedOutput<T>> boolean add(OUT chunkedOutput)
RegisterChunkedOutput
to thisBroadcaster
instance.- Parameters:
chunkedOutput
-ChunkedOutput
to register.- Returns:
true
if the instance was successfully registered,false
otherwise.
-
remove
public <OUT extends ChunkedOutput<T>> boolean remove(OUT chunkedOutput)
Un-registerChunkedOutput
from thisBroadcaster
instance. This method does not close theChunkedOutput
being unregistered.- Parameters:
chunkedOutput
-ChunkedOutput
instance to un-register from this broadcaster.- Returns:
true
if the instance was unregistered,false
otherwise.
-
add
public boolean add(BroadcasterListener<T> listener)
RegisterBroadcasterListener
forBroadcaster
events listening.This operation is potentially slow, especially if large number of listeners get registered in the broadcaster. The
Broadcaster
implementation is optimized to efficiently handle small amounts of concurrent listener registrations and removals and large amounts of registered listener notifications.- Parameters:
listener
- listener to be registered.- Returns:
true
if registered,false
otherwise.
-
remove
public boolean remove(BroadcasterListener<T> listener)
Un-registerBroadcasterListener
.This operation is potentially slow, especially if large number of listeners get registered in the broadcaster. The
Broadcaster
implementation is optimized to efficiently handle small amounts of concurrent listener registrations and removals and large amounts of registered listener notifications.- Parameters:
listener
- listener to be unregistered.- Returns:
true
if unregistered,false
otherwise.
-
broadcast
public void broadcast(T chunk)
Broadcast a chunk to all registeredChunkedOutput
instances.- Parameters:
chunk
- chunk to be sent.
-
closeAll
public void closeAll()
Close all registeredChunkedOutput
instances.
-
onException
public void onException(ChunkedOutput<T> chunkedOutput, java.lang.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 particularChunkedOutput
instance when trying to write to it or close it.- Specified by:
onException
in interfaceBroadcasterListener<T>
- Parameters:
chunkedOutput
- instance that threw exception.exception
- exception that was thrown.
-
onClose
public void onClose(ChunkedOutput<T> chunkedOutput)
Called when the chunkedOutput has been closed (either by client closing the connection or by callingChunkedOutput.close()
on the server side. Can be implemented by subclasses to handle the event ofChunkedOutput
being closed.- Specified by:
onClose
in interfaceBroadcasterListener<T>
- Parameters:
chunkedOutput
- instance that was closed.
-
forEachOutput
private void forEachOutput(Broadcaster.Task<ChunkedOutput<T>> t)
-
forEachListener
private void forEachListener(Broadcaster.Task<BroadcasterListener<T>> t)
-
fireOnException
private void fireOnException(ChunkedOutput<T> chunkedOutput, java.lang.Exception exception)
-
fireOnClose
private void fireOnClose(ChunkedOutput<T> chunkedOutput)
-
-