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).
| Modifier and Type | Class and Description |
|---|---|
| private class | CometContext.
References Deprecated
GenericCloseListener is deprecated or references (maybe indirectly) at least one deprecated element.
|
| private static class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| protected static final String | |
| private final Map | |
| private static final String | |
| private final CometEvent | |
| protected final CometEvent | |
| protected final CometEvent | |
| private long | expirationDelay
The default delay expiration before a |
| private final List | handlers
The list of registered |
| 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. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public int | Returns: The hash code of the handler.a new handler)CometHandlerAdd a |
| public Object | |
| public List | Returns: the current list of activeCometHandlerReturn the current list of active |
| public String | Returns: topic the context path associated with this instance
Deprecated
- use getTopic.
Get the context path associated with this instance. |
| public long | Returns: long thelong delay, in millisecond, before a request is resumed.Return the |
| public NotificationHandler | |
| public String | Returns: topic the topic associated with this instanceGet the topic representing this instance with this instance. |
| private void | |
| protected void | |
| public boolean | interrupt(CometHandler<E>
The handler, boolean CometHandler encapsulating the suspended connection.Finish the current execution. finishExecution)Interrupt a |
| protected void | interrupt0(CometHandler<E>
The handler, boolean CometHandler encapsulating the suspended connection.Finish the current execution. finishExecution)Interrupt logic in its own method, so it can be executed either async or sync. |
| protected void | invokeCometHandler(CometEvent<E> event, CometHandler<E> cometHandler)
Invoke a |
| public boolean | Returns: trueReturn true if this |
| public boolean | |
| public void | |
| public void | notify(E
An object shared amongst attachment, CometHandler<E> CometHandler.CometHandler to notify.Notify a single |
| public void | notify(E
An object shared amongst attachment, CometEvent.CometHandler.The type of notification. eventType,CometHandler to notify.Notify a single |
| public void | notify(E
An object shared amongst attachment, CometEvent.CometHandler.The type of notification. eventTypeNotify all |
| private static void | |
| public void | |
| public Object | |
| public boolean | Returns: true if the operation succeeded.Remove a |
| public boolean | Returns: true if the operation succeeded.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 |
| public boolean | Returns: true if the operation succeeded.The CometHandler to resume. handler)Resume the Comet request and remove it from the active |
| public void | setDetectClosedConnections(final boolean isDetectClosedConnections)
Enable/disable the mechanism, which detects closed connections and notifies user's handlers via
|
| public void | setExpirationDelay(long
the expirationDelay)long delay before a request is resumed. Value is in milliseconds.Set the |
| public void | |
| public String |
| ALREADY_REMOVED | back to summary |
|---|---|
| protected static final String ALREADY_REMOVED | |
| attributes | back to summary |
|---|---|
| private final Map<Object, Object> attributes | |
| COMET_NOT_ENABLED | back to summary |
|---|---|
| private static final String COMET_NOT_ENABLED | |
| eventInitialize | back to summary |
|---|---|
| private final CometEvent<CometContext<E>> eventInitialize | |
| eventInterrupt | back to summary |
|---|---|
| protected final CometEvent<CometContext<E>> eventInterrupt | |
| eventTerminate | back to summary |
|---|---|
| protected final CometEvent<CometContext<E>> eventTerminate | |
| expirationDelay | back to summary |
|---|---|
| private long expirationDelay The default delay expiration before a | |
| handlers | back to summary |
|---|---|
| private final List<CometHandler<E>> handlers The list of registered | |
| INVALID_COMET_HANDLER | back to summary |
|---|---|
| protected static final String INVALID_COMET_HANDLER Generic error message | |
| isDetectClosedConnections | back 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. | |
| LOGGER | back to summary |
|---|---|
| protected static final Logger LOGGER | |
| notificationHandler | back to summary |
|---|---|
| protected NotificationHandler notificationHandler The default NotificationHandler. | |
| topic | back to summary |
|---|---|
| protected String topic The context path associated with this instance. | |
| CometContext | back to summary |
|---|---|
| public CometContext(String contextTopic) Create a new instance
| |
| addAttribute | back to summary |
|---|---|
| public void addAttribute(Object key, Object value) Add an attribute. | |
| addCometHandler | back to summary |
|---|---|
| public int addCometHandler(final Request request, CometHandler<E> handler) Add a
| |
| getAttribute | back to summary |
|---|---|
| public Object getAttribute(Object key) Retrieve an attribute. | |
| getCometHandlers | back to summary |
|---|---|
| public List Return the current list of active
| |
| getContextPath | back to summary |
|---|---|
| public String getContextPath()
Deprecated - use getTopic. Get the context path associated with this instance.
| |
| getExpirationDelay | back to summary |
|---|---|
| public long getExpirationDelay() Return the
| |
| getNotificationHandler | back to summary |
|---|---|
| public NotificationHandler getNotificationHandler() Return the associated | |
| getTopic | back to summary |
|---|---|
| public String getTopic() Get the topic representing this instance with this instance. This is the value to uses when invoking
| |
| initDefaultValues | back to summary |
|---|---|
| private void initDefaultValues() init of default values. used by constructor and the cache recycle mechanism | |
| initialize | back to summary |
|---|---|
| protected void initialize(CometHandler<E> handler) throws IOException Initialize the newly added | |
| interrupt | back to summary |
|---|---|
| public boolean interrupt(CometHandler<E> handler, boolean finishExecution) throws IOException Interrupt a
| |
| interrupt0 | back 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.
| |
| invokeCometHandler | back to summary |
|---|---|
| protected void invokeCometHandler(CometEvent<E> event, CometHandler<E> cometHandler) throws IOException Invoke a
| |
| isActive | back to summary |
|---|---|
| public boolean isActive(CometHandler<E> handler) Return true if this
| |
| isDetectClosedConnections | back 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. | |
| notify | back to summary |
|---|---|
| public void notify(E attachment) throws IOException Notify all
| |
| notify | back to summary |
|---|---|
| public void notify(E attachment, CometHandler<E> cometHandler) throws IOException Notify a single
| |
| notify | back to summary |
|---|---|
| public void notify(E attachment, CometEvent. Notify a single
| |
| notify | back to summary |
|---|---|
| public void notify(E attachment, CometEvent. Notify all
| |
| notifyOnAsyncRead | back to summary |
|---|---|
| private static void notifyOnAsyncRead(CometHandler<E> handler) | |
| recycle | back to summary |
|---|---|
| public void recycle() Recycle this object. | |
| removeAttribute | back to summary |
|---|---|
| public Object removeAttribute(Object key) Remove an attribute. | |
| removeCometHandler | back to summary |
|---|---|
| public boolean removeCometHandler(CometHandler<E> handler) Remove a
| |
| removeCometHandler | back to summary |
|---|---|
| public boolean removeCometHandler(CometHandler<E> handler, boolean resume) Remove a
| |
| resumeCometHandler | back to summary |
|---|---|
| public boolean resumeCometHandler(CometHandler<E> handler) throws IOException Resume the Comet request and remove it from the active If you cache them for later reuse by another thread there is a possibility to introduce corrupted responses next time a request is made.
| |
| setDetectClosedConnections | back to summary |
|---|---|
| public void setDetectClosedConnections(final boolean isDetectClosedConnections) Enable/disable the mechanism, which detects closed connections and notifies user's handlers via
| |
| setExpirationDelay | back to summary |
|---|---|
| public void setExpirationDelay(long expirationDelay) Set the
| |
| setNotificationHandler | back to summary |
|---|---|
| public void setNotificationHandler(NotificationHandler notificationHandler) Set the current | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
References Deprecated
GenericCloseListener is deprecated or references (maybe indirectly) at least one deprecated element.
See corresponding docs for further information.
| Modifier and Type | Field and Description |
|---|---|
| private final CometHandler |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | cancelled()
Implements org. The operation was cancelled. |
| public void | completed(Response result)
Implements org. The operation was completed. |
| public void | failed(Throwable throwable)
Implements org. The operation was failed. |
| public void | onClosed(final Closeable closeable, final CloseType type)
Implements org.
References Deprecated
CloseType is deprecated or references (maybe indirectly) at least one deprecated element.
|
| public void | updated(Response result)
Implements org. The callback method may be called, when there is some progress in operation execution, but it is still not completed |
| handler | back to summary |
|---|---|
| private final CometHandler<E> handler | |
| CometCompletionHandler | back to summary |
|---|---|
| public CometCompletionHandler(CometHandler<E> handler) | |
| cancelled | back to summary |
|---|---|
| public void cancelled() Implements org. Doc from org. The operation was cancelled.
| |
| completed | back to summary |
|---|---|
| public void completed(Response result) Implements org. Doc from org. The operation was completed.
| |
| failed | back to summary |
|---|---|
| public void failed(Throwable throwable) Implements org. Doc from org. The operation was failed.
| |
| onClosed | back to summary |
|---|---|
| public void onClosed(final Closeable closeable, final CloseType type) throws IOException Implements org. References Deprecated
See corresponding docs for further information.
| |
| updated | back to summary |
|---|---|
| public void updated(Response result) Implements org. Doc from org. The callback method may be called, when there is some progress in operation execution, but it is still not completed
| |
| Modifier and Type | Field and Description |
|---|---|
| private final CometHandler | |
| pack-priv final NIOInputStream |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | onAllDataRead()
Implements org. Invoked when all data for the current request has been read. |
| public void | onDataAvailable()
Implements org. Invoked when data is available to be read without blocking. |
| public void | onError(Throwable t)
Implements org. Invoked when an error occurs processing the request asynchronously. |
| handler | back to summary |
|---|---|
| private final CometHandler<E> handler | |
| nioInputStream | back to summary |
|---|---|
| pack-priv final NIOInputStream nioInputStream | |
| CometInputHandler | back to summary |
|---|---|
| public CometInputHandler(NIOInputStream nioInputStream, CometHandler<E> handler) | |
| onAllDataRead | back to summary |
|---|---|
| public void onAllDataRead() Implements org. Doc from org. Invoked when all data for the current request has been read.
| |
| onDataAvailable | back to summary |
|---|---|
| public void onDataAvailable() Implements org. Doc from org. Invoked when data is available to be read without blocking. Data must be consumed by the handler implementation before re-registering.
| |
| onError | back to summary |
|---|---|
| public void onError(Throwable t) Implements org. Doc from org. Invoked when an error occurs processing the request asynchronously.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final CometHandler |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | onTimeout(Response response)
Implements org. Method is called, when suspended |
| handler | back to summary |
|---|---|
| private final CometHandler<E> handler | |
| CometTimeoutHandler | back to summary |
|---|---|
| public CometTimeoutHandler(CometHandler<E> handler) | |
| onTimeout | back to summary |
|---|---|
| public boolean onTimeout(Response response) Implements org. Doc from org. Method is called, when suspended
| |