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>
- Direct Known Subclasses:
SseBroadcaster
Used for broadcasting response chunks to multiple
ChunkedOutput instances.- Author:
- Pavel Bucek, Martin Matula
-
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new instance.protectedBroadcaster(Class<? extends Broadcaster> subclass) Can be used by subclasses to override the default functionality of adding self to the set oflisteners. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(BroadcasterListener<T> listener) RegisterBroadcasterListenerforBroadcasterevents listening.<OUT extends ChunkedOutput<T>>
booleanadd(OUT chunkedOutput) RegisterChunkedOutputto thisBroadcasterinstance.voidBroadcast a chunk to all registeredChunkedOutputinstances.voidcloseAll()Close all registeredChunkedOutputinstances.voidonClose(ChunkedOutput<T> chunkedOutput) Called when the chunkedOutput has been closed (either by client closing the connection or by callingChunkedOutput.close()on the server side.voidonException(ChunkedOutput<T> chunkedOutput, Exception exception) Called when exception was thrown by a given chunked response when trying to write to it or close it.booleanremove(BroadcasterListener<T> listener) Un-registerBroadcasterListener.<OUT extends ChunkedOutput<T>>
booleanremove(OUT chunkedOutput) Un-registerChunkedOutputfrom thisBroadcasterinstance.
-
Constructor Details
-
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
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:
-
-
Method Details
-
add
RegisterChunkedOutputto thisBroadcasterinstance.- Parameters:
chunkedOutput-ChunkedOutputto register.- Returns:
trueif the instance was successfully registered,falseotherwise.
-
remove
Un-registerChunkedOutputfrom thisBroadcasterinstance. This method does not close theChunkedOutputbeing unregistered.- Parameters:
chunkedOutput-ChunkedOutputinstance to un-register from this broadcaster.- Returns:
trueif the instance was unregistered,falseotherwise.
-
add
RegisterBroadcasterListenerforBroadcasterevents listening.This operation is potentially slow, especially if large number of listeners get registered in the broadcaster. The
Broadcasterimplementation 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:
trueif registered,falseotherwise.
-
remove
Un-registerBroadcasterListener.This operation is potentially slow, especially if large number of listeners get registered in the broadcaster. The
Broadcasterimplementation 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:
trueif unregistered,falseotherwise.
-
broadcast
Broadcast a chunk to all registeredChunkedOutputinstances.- Parameters:
chunk- chunk to be sent.
-
closeAll
public void closeAll()Close all registeredChunkedOutputinstances. -
onException
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 particularChunkedOutputinstance when trying to write to it or close it.- Specified by:
onExceptionin interfaceBroadcasterListener<T>- Parameters:
chunkedOutput- instance that threw exception.exception- exception that was thrown.
-
onClose
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 ofChunkedOutputbeing closed.- Specified by:
onClosein interfaceBroadcasterListener<T>- Parameters:
chunkedOutput- instance that was closed.
-