Top Description Inners Fields Constructors Methods
org.glassfish.grizzly.comet

public Class CometContext<E>

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.List, .Map, java.util.concurrent.ConcurrentHashMap, .CopyOnWriteArrayList, .TimeUnit, java.util.logging.Level, .Logger, org.glassfish.grizzly.CloseType, .Closeable, .CompletionHandler, .Connection, .GenericCloseListener, .ReadHandler, org.glassfish.grizzly.http.io.NIOInputStream, org.glassfish.grizzly.http.server.Request, .Response, .TimeoutHandler, org.glassfish.grizzly.http.util.Header

The main object used by CometHandler and Servlet to push information amongst suspended request/response. The CometContext is always available for CometHandler and can be used to notify, or share information with other CometHandler. This is the equivalent of server push as the CometContext will invoke all registered CometHandler (addCometHandler) sequentially.

A CometContext can be considered as a topic where CometHandler register for information. A CometContext can be shared amongst Servlet of the same application, or globally across all deployed web applications. Normally, a CometContext is created using a topic's name like:


CometEngine ce = CometEngine.getEngine(); CometContext cc = ce.registerContext("MyTopic");

and then inside a Servlet.service() method, you just need to call:

cc.addCometListener(myNewCometListener()); cc.notify("I'm pushing data to all registered CometHandler");

As soon as addCometHandler is invoked, Grizzly will automatically suspend the request/response (will not commit the response). A response can be resumed by invoking resumeCometHandler, which will automatically commit the response and remove the associated CometHandler from the CometContext.

A CometContext uses a NotificationHandler to invoke, using the calling thread or a Grizzly thread pool, all CometHandler than have been added using the addCometHandler. A NotificationHandler can be used to filter or transform the content that will eventually be pushed back to all connected clients. You can also use a NotificationHandler to throttle push like invoking only a subset of the CometHandler, etc.

Idle suspended connection can be timed out by configuring the setExpirationDelay(long). The value needs to be in milliseconds. If there is no I/O operations and no invocation of notify during the expiration delay, Grizzly will resume all suspended connection. An application will have a chance to send back data using the connection as Grizzly will invoke the CometHandler#onInterrupt before resuming the connection. Note that setting the expiration delay to -1 disable the above mechanism, e.g. idle connection will never get resumed by Grizzly.

Attributes can be added/removed the same way HttpServletSession is doing. It is not recommended to use attributes if this CometContext is not shared amongst multiple context path (uses HttpServletSession instead).

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
CometContext.CometCompletionHandler

References Deprecated GenericCloseListener is deprecated or references (maybe indirectly) at least one deprecated element.
private static class
private class

Field Summary

Modifier and TypeField and Description
protected static final String
private final Map<Object, Object>
private static final String
private final CometEvent<CometContext<E>>
protected final CometEvent<CometContext<E>>
protected final CometEvent<CometContext<E>>
private long
expirationDelay

The default delay expiration before a CometContext's CometHandler are interrupted.

private final List<CometHandler<E>>
handlers

The list of registered CometHandler

protected static final String
INVALID_COMET_HANDLER

Generic error message

private boolean
isDetectClosedConnections

true, if we want to enable mechanism, which detects closed connections, or false otherwise.

protected static final Logger
protected NotificationHandler
notificationHandler

The default NotificationHandler.

protected String
topic

The context path associated with this instance.

Constructor Summary

AccessConstructor and Description
public
CometContext(String
the context path
contextTopic
)

Create a new instance

Method Summary

Modifier and TypeMethod and Description
public void
addAttribute(Object
the key
key
,
Object
the value
value
)

Add an attribute.

public int

Returns:

The hash code of the handler.
addCometHandler
(final Request request, CometHandler<E> handler)

Add a CometHandler.

public Object

Returns:

Object the value.
getAttribute
(Object
the key
key
)

Retrieve an attribute.

public List<CometHandler<E>>

Returns:

the current list of active CometHandler
getCometHandlers
()

Return the current list of active CometHandler

public String

Returns:

topic the context path associated with this instance
getContextPath
()
Deprecated - use getTopic.

Get the context path associated with this instance.

public long

Returns:

long the long delay, in millisecond, before a request is resumed.
getExpirationDelay
()

Return the long delay, in millisecond, before a request is resumed.

public NotificationHandler
public String

Returns:

topic the topic associated with this instance
getTopic
()

Get the topic representing this instance with this instance.

private void
initDefaultValues()

init of default values.

protected void
initialize(CometHandler<E> handler)

Initialize the newly added CometHandler.

public boolean
interrupt(CometHandler<E>
The CometHandler encapsulating the suspended connection.
handler
,
boolean
Finish the current execution.
finishExecution
)

Interrupt a CometHandler by invoking CometHandler#onInterrupt

protected void
interrupt0(CometHandler<E>
The CometHandler encapsulating the suspended connection.
handler
,
boolean
Finish the current execution.
finishExecution
)

Interrupt logic in its own method, so it can be executed either async or sync.

protected void
public boolean

Returns:

true
isActive
(CometHandler<E> handler)

Return true if this CometHandler is still active, e.g. there is still a suspended connection associated with it.

public boolean
isDetectClosedConnections()

Returns true if connection terminate detection is on.

public void
notify(E
An object shared amongst CometHandler.
attachment
)

Notify all CometHandler.

public void
notify(E
An object shared amongst CometHandler.
attachment
,
CometHandler<E>
CometHandler to notify.
cometHandler
)

Notify a single CometHandler#onEvent(CometEvent).

public void
notify(E
An object shared amongst CometHandler.
attachment
,
CometEvent.Type
The type of notification.
eventType
,
CometHandler<E>
CometHandler to notify.
cometHandler
)

Notify a single CometHandler.

public void
notify(E
An object shared amongst CometHandler.
attachment
,
CometEvent.Type
The type of notification.
eventType
)

Notify all CometHandler.

private static void
public void
recycle()

Recycle this object.

public Object

Returns:

Object the value
removeAttribute
(Object
the key
key
)

Remove an attribute.

public boolean

Returns:

true if the operation succeeded.
removeCometHandler
(CometHandler<E> handler)

Remove a CometHandler.

public boolean

Returns:

true if the operation succeeded.
removeCometHandler
(CometHandler<E>
The CometHandler to remove.
handler
,
boolean
True is the connection can be resumed if no CometHandler are associated with the underlying SelectionKey.
resume
)

Remove a CometHandler.

public boolean

Returns:

true if the operation succeeded.
resumeCometHandler
(CometHandler<E>
The CometHandler to resume.
handler
)

Resume the Comet request and remove it from the active CometHandler list.

public void
setDetectClosedConnections(final boolean isDetectClosedConnections)

Enable/disable the mechanism, which detects closed connections and notifies user's handlers via CometHandler#onInterrupt(org.glassfish.grizzly.comet.CometEvent) method.

public void
setExpirationDelay(long
the long delay before a request is resumed. Value is in milliseconds.
expirationDelay
)

Set the long delay before a request is resumed.

public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

ALREADY_REMOVEDback to summary
protected static final String ALREADY_REMOVED
attributesback to summary
private final Map<Object, Object> attributes
COMET_NOT_ENABLEDback to summary
private static final String COMET_NOT_ENABLED
eventInitializeback to summary
private final CometEvent<CometContext<E>> eventInitialize
eventInterruptback to summary
protected final CometEvent<CometContext<E>> eventInterrupt
eventTerminateback to summary
protected final CometEvent<CometContext<E>> eventTerminate
expirationDelayback to summary
private long expirationDelay

The default delay expiration before a CometContext's CometHandler are interrupted.

handlersback to summary
private final List<CometHandler<E>> handlers

The list of registered CometHandler

INVALID_COMET_HANDLERback to summary
protected static final String INVALID_COMET_HANDLER

Generic error message

isDetectClosedConnectionsback to summary
private boolean isDetectClosedConnections

true, if we want to enable mechanism, which detects closed connections, or false otherwise. The mentioned mechanism should be disabled if we expect client to use HTTP pipelining.

LOGGERback to summary
protected static final Logger LOGGER
notificationHandlerback to summary
protected NotificationHandler notificationHandler

The default NotificationHandler.

topicback to summary
protected String topic

The context path associated with this instance.

Constructor Detail

CometContextback to summary
public CometContext(String contextTopic)

Create a new instance

Parameters
contextTopic:String

the context path

Method Detail

addAttributeback to summary
public void addAttribute(Object key, Object value)

Add an attribute.

Parameters
key:Object

the key

value:Object

the value

addCometHandlerback to summary
public int addCometHandler(final Request request, CometHandler<E> handler)

Add a CometHandler. The underlying HttpServletResponse will not get committed until CometContext#resumeCometHandler(CometHandler) is invoked, unless the CometContext#setExpirationDelay(long) expires. If set to alreadySuspended is set to true, no I/O operations are allowed inside the CometHandler as the underlying HttpServletResponse has not been suspended. Adding such CometHandler is useful only when no I/O operations on the HttpServletResponse are required. Examples include calling a remote EJB when a push operations happens, storing data inside a database, etc.

Parameters
handler:CometHandler<E>

a new CometHandler

Returns:int

The hash code of the handler.

getAttributeback to summary
public Object getAttribute(Object key)

Retrieve an attribute.

Parameters
key:Object

the key

Returns:Object

Object the value.

getCometHandlersback to summary
public List<CometHandler<E>> getCometHandlers()

Return the current list of active CometHandler

Returns:List<CometHandler<E>>

the current list of active CometHandler

getContextPathback to summary
public String getContextPath()

Deprecated

- use getTopic.

Get the context path associated with this instance.

Returns:String

topic the context path associated with this instance

Annotations
@Deprecated
getExpirationDelayback to summary
public long getExpirationDelay()

Return the long delay, in millisecond, before a request is resumed.

Returns:long

long the long delay, in millisecond, before a request is resumed.

getNotificationHandlerback to summary
public NotificationHandler getNotificationHandler()

Return the associated NotificationHandler

getTopicback to summary
public String getTopic()

Get the topic representing this instance with this instance. This is the value to uses when invoking CometEngine#getCometContext

Returns:String

topic the topic associated with this instance

initDefaultValuesback to summary
private void initDefaultValues()

init of default values. used by constructor and the cache recycle mechanism

initializeback to summary
protected void initialize(CometHandler<E> handler) throws IOException

Initialize the newly added CometHandler.

interruptback to summary
public boolean interrupt(CometHandler<E> handler, boolean finishExecution) throws IOException

Interrupt a CometHandler by invoking CometHandler#onInterrupt

Parameters
handler:CometHandler<E>

The CometHandler encapsulating the suspended connection.

finishExecution:boolean

Finish the current execution.

interrupt0back to summary
protected void interrupt0(CometHandler<E> handler, boolean finishExecution) throws IOException

Interrupt logic in its own method, so it can be executed either async or sync.
cometHandler.onInterrupt is performed async due to its functionality is unknown, hence not safe to run in the performance critical selector thread.

Parameters
handler:CometHandler<E>

The CometHandler encapsulating the suspended connection.

finishExecution:boolean

Finish the current execution.

invokeCometHandlerback to summary
protected void invokeCometHandler(CometEvent<E> event, CometHandler<E> cometHandler) throws IOException

Invoke a CometHandler using the CometEvent

Parameters
event:CometEvent<E>

- CometEvent

cometHandler:CometHandler<E>

- CometHandler

isActiveback to summary
public boolean isActive(CometHandler<E> handler)

Return true if this CometHandler is still active, e.g. there is still a suspended connection associated with it.

Returns:boolean

true

isDetectClosedConnectionsback to summary
public boolean isDetectClosedConnections()

Returns true if connection terminate detection is on. If this feature is on - HTTP pipelining can not be used. The feature is enabled by default.

notifyback to summary
public void notify(E attachment) throws IOException

Notify all CometHandler. All CometHandler#onEvent will be invoked with a CometEvent of type NOTIFY.

Parameters
attachment:E

An object shared amongst CometHandler.

notifyback to summary
public void notify(E attachment, CometHandler<E> cometHandler) throws IOException

Notify a single CometHandler#onEvent(CometEvent).

Parameters
attachment:E

An object shared amongst CometHandler.

cometHandler:CometHandler<E>

CometHandler to notify.

notifyback to summary
public void notify(E attachment, CometEvent.Type eventType, CometHandler<E> cometHandler) throws IOException

Notify a single CometHandler. The CometEvent#getType() will determine which CometHandler method will be invoked:


CometEvent.INTERRUPT -> CometHandler#onInterrupt(CometEvent)
CometEvent.Type.NOTIFY -> CometHandler#onEvent(CometEvent)
CometEvent.INITIALIZE -> CometHandler#onInitialize(CometEvent)
CometEvent.TERMINATE -> CometHandler#onTerminate(CometEvent)
CometEvent.READ -> CometHandler#onEvent(CometEvent)

Parameters
attachment:E

An object shared amongst CometHandler.

eventType:CometEvent.Type

The type of notification.

cometHandler:CometHandler<E>

CometHandler to notify.

notifyback to summary
public void notify(E attachment, CometEvent.Type eventType) throws IOException

Notify all CometHandler. The CometEvent#getType() will determine which CometHandler method will be invoked:


CometEvent.Type.INTERRUPT -> CometHandler#onInterrupt
CometEvent.Type.NOTIFY -> CometHandler#onEvent
CometEvent.Type.INITIALIZE -> CometHandler#onInitialize
CometEvent.Type.TERMINATE -> CometHandler#onTerminate
CometEvent.Type.READ -> CometHandler#onEvent

Parameters
attachment:E

An object shared amongst CometHandler.

eventType:CometEvent.Type

The type of notification.

notifyOnAsyncReadback to summary
private static void notifyOnAsyncRead(CometHandler<E> handler)
recycleback to summary
public void recycle()

Recycle this object.

removeAttributeback to summary
public Object removeAttribute(Object key)

Remove an attribute.

Parameters
key:Object

the key

Returns:Object

Object the value

removeCometHandlerback to summary
public boolean removeCometHandler(CometHandler<E> handler)

Remove a CometHandler. If the continuation (connection) associated with this CometHandler no longer have CometHandler associated to it, it will be resumed by Grizzly by calling CometContext#resumeCometHandler(CometHandler)

Returns:boolean

true if the operation succeeded.

removeCometHandlerback to summary
public boolean removeCometHandler(CometHandler<E> handler, boolean resume)

Remove a CometHandler. If the continuation (connection) associated with this CometHandler no longer have CometHandler associated to it, it will be resumed.

Parameters
handler:CometHandler<E>

The CometHandler to remove.

resume:boolean

True is the connection can be resumed if no CometHandler are associated with the underlying SelectionKey.

Returns:boolean

true if the operation succeeded.

resumeCometHandlerback to summary
public boolean resumeCometHandler(CometHandler<E> handler) throws IOException

Resume the Comet request and remove it from the active CometHandler list. Once resumed, a CometHandler must never manipulate the HttpServletRequest or HttpServletResponse as those object will be recycled and may be re-used to serve another request.

If you cache them for later reuse by another thread there is a possibility to introduce corrupted responses next time a request is made.

Parameters
handler:CometHandler<E>

The CometHandler to resume.

Returns:boolean

true if the operation succeeded.

setDetectClosedConnectionsback to summary
public void setDetectClosedConnections(final boolean isDetectClosedConnections)

Enable/disable the mechanism, which detects closed connections and notifies user's handlers via CometHandler#onInterrupt(org.glassfish.grizzly.comet.CometEvent) method. If this feature is on - HTTP pipelining can not be used.

setExpirationDelayback to summary
public void setExpirationDelay(long expirationDelay)

Set the long delay before a request is resumed.

Parameters
expirationDelay:long

the long delay before a request is resumed. Value is in milliseconds.

setNotificationHandlerback to summary
public void setNotificationHandler(NotificationHandler notificationHandler)

Set the current NotificationHandler

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
org.glassfish.grizzly.comet back to summary

private Class CometContext.CometCompletionHandler

extends Object
implements CompletionHandler<Response>, GenericCloseListener
Class Inheritance
  • java.lang.Object
  • org.glassfish.grizzly.comet.CometContext.CometCompletionHandler
All Implemented Interfaces
org.glassfish.grizzly.GenericCloseListener, org.glassfish.grizzly.CloseListener, org.glassfish.grizzly.CompletionHandler

References Deprecated

GenericCloseListener is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Field Summary

Modifier and TypeField and Description
private final CometHandler<E>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
cancelled()

Implements org.glassfish.grizzly.CompletionHandler.cancelled.

The operation was cancelled.

public void
completed(Response result)

Implements org.glassfish.grizzly.CompletionHandler.completed.

The operation was completed.

public void
failed(Throwable throwable)

Implements org.glassfish.grizzly.CompletionHandler.failed.

The operation was failed.

public void
onClosed(final Closeable closeable, final CloseType type)

Implements org.glassfish.grizzly.CloseListener.onClosed.

References Deprecated CloseType is deprecated or references (maybe indirectly) at least one deprecated element.

public void
updated(Response result)

Implements org.glassfish.grizzly.CompletionHandler.updated.

The callback method may be called, when there is some progress in operation execution, but it is still not completed

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

handlerback to summary
private final CometHandler<E> handler

Constructor Detail

CometCompletionHandlerback to summary
public CometCompletionHandler(CometHandler<E> handler)

Method Detail

cancelledback to summary
public void cancelled()

Implements org.glassfish.grizzly.CompletionHandler.cancelled.

Doc from org.glassfish.grizzly.CompletionHandler.cancelled.

The operation was cancelled.

Annotations
@Override
completedback to summary
public void completed(Response result)

Implements org.glassfish.grizzly.CompletionHandler.completed.

Doc from org.glassfish.grizzly.CompletionHandler.completed.

The operation was completed.

Annotations
@Override
failedback to summary
public void failed(Throwable throwable)

Implements org.glassfish.grizzly.CompletionHandler.failed.

Doc from org.glassfish.grizzly.CompletionHandler.failed.

The operation was failed.

Annotations
@Override
onClosedback to summary
public void onClosed(final Closeable closeable, final CloseType type) throws IOException

Implements org.glassfish.grizzly.CloseListener.onClosed.

References Deprecated

CloseType is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Annotations
@Override
updatedback to summary
public void updated(Response result)

Implements org.glassfish.grizzly.CompletionHandler.updated.

Doc from org.glassfish.grizzly.CompletionHandler.updated.

The callback method may be called, when there is some progress in operation execution, but it is still not completed

Annotations
@Override
org.glassfish.grizzly.comet back to summary

private Class CometContext.CometInputHandler

extends Object
implements ReadHandler
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.ReadHandler

Field Summary

Modifier and TypeField and Description
private final CometHandler<E>
pack-priv final NIOInputStream

Constructor Summary

AccessConstructor and Description
public
CometInputHandler(NIOInputStream nioInputStream, CometHandler<E> handler)

Method Summary

Modifier and TypeMethod and Description
public void
onAllDataRead()

Implements org.glassfish.grizzly.ReadHandler.onAllDataRead.

Invoked when all data for the current request has been read.

public void
onDataAvailable()

Implements org.glassfish.grizzly.ReadHandler.onDataAvailable.

Invoked when data is available to be read without blocking.

public void
onError(Throwable t)

Implements org.glassfish.grizzly.ReadHandler.onError.

Invoked when an error occurs processing the request asynchronously.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

handlerback to summary
private final CometHandler<E> handler
nioInputStreamback to summary
pack-priv final NIOInputStream nioInputStream

Constructor Detail

CometInputHandlerback to summary
public CometInputHandler(NIOInputStream nioInputStream, CometHandler<E> handler)

Method Detail

onAllDataReadback to summary
public void onAllDataRead()

Implements org.glassfish.grizzly.ReadHandler.onAllDataRead.

Doc from org.glassfish.grizzly.ReadHandler.onAllDataRead.

Invoked when all data for the current request has been read.

Annotations
@Override
onDataAvailableback to summary
public void onDataAvailable()

Implements org.glassfish.grizzly.ReadHandler.onDataAvailable.

Doc from org.glassfish.grizzly.ReadHandler.onDataAvailable.

Invoked when data is available to be read without blocking. Data must be consumed by the handler implementation before re-registering.

Annotations
@Override
onErrorback to summary
public void onError(Throwable t)

Implements org.glassfish.grizzly.ReadHandler.onError.

Doc from org.glassfish.grizzly.ReadHandler.onError.

Invoked when an error occurs processing the request asynchronously.

Annotations
@Override
org.glassfish.grizzly.comet back to summary

private Class CometContext.CometTimeoutHandler

extends Object
implements TimeoutHandler
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.http.server.TimeoutHandler

Field Summary

Modifier and TypeField and Description
private final CometHandler<E>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
onTimeout(Response response)

Implements org.glassfish.grizzly.http.server.TimeoutHandler.onTimeout.

Method is called, when suspended Response timeout expired.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

handlerback to summary
private final CometHandler<E> handler

Constructor Detail

CometTimeoutHandlerback to summary
public CometTimeoutHandler(CometHandler<E> handler)

Method Detail

onTimeoutback to summary
public boolean onTimeout(Response response)

Implements org.glassfish.grizzly.http.server.TimeoutHandler.onTimeout.

Doc from org.glassfish.grizzly.http.server.TimeoutHandler.onTimeout.

Method is called, when suspended Response timeout expired. The custom implementation may decide to confirm timeout and return true, so the Response will be canceled immediately after that, or return false to reset the timeout and give more time for processing.

Annotations
@Override