Class CometContext<E>
CometHandler and Servlet to push information amongst suspended request/response. The
CometContext is always available for CometHandler and can be used to notify(E), or share
information with other CometHandler. This is the equivalent of server push as the CometContext will invoke
all registered CometHandler (addCometHandler(org.glassfish.grizzly.comet.CometHandler<E>)) 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(org.glassfish.grizzly.comet.CometHandler<E>) is invoked, Grizzly will automatically suspend the
request/response (will not commit the response). A response can be resumed by invoking resumeCometHandler(org.glassfish.grizzly.comet.CometHandler), 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(org.glassfish.grizzly.comet.CometHandler<E>). 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(E) 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(org.glassfish.grizzly.comet.CometEvent) 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).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Stringprotected final CometEvent<CometContext>protected final CometEvent<CometContext>protected static final StringGeneric error messageprotected static final org.slf4j.Loggerprotected NotificationHandlerThe default NotificationHandler.protected static final ThreadLocal<org.glassfish.grizzly.http.server.Request>protected StringThe context path associated with this instance. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttribute(Object key, Object value) Add an attribute.intaddCometHandler(CometHandler<E> handler) Add aCometHandler.getAttribute(Object key) Retrieve an attribute.Return the current list of activeCometHandlerDeprecated.- use getTopic.longReturn thelongdelay, in millisecond, before a request is resumed.Return the associatedNotificationHandlergetTopic()Get the topic representing this instance with this instance.protected voidinitialize(CometHandler handler) Initialize the newly addedCometHandler.booleaninterrupt(CometHandler handler, boolean finishExecution) Interrupt aCometHandlerby invokingCometHandler.onInterrupt(org.glassfish.grizzly.comet.CometEvent)protected voidinterrupt0(CometHandler handler, boolean finishExecution) 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.protected voidinvokeCometHandler(CometEvent event, CometHandler cometHandler) Invoke aCometHandlerusing theCometEventbooleanisActive(CometHandler handler) Return true if thisCometHandleris still active, e.g.booleanReturns true if connection terminate detection is on.voidNotify allCometHandler.voidnotify(E attachment, CometEvent.Type eventType) Notify allCometHandler.voidnotify(E attachment, CometEvent.Type eventType, CometHandler cometHandler) Notify a singleCometHandler.voidnotify(E attachment, CometHandler cometHandler) Notify a singleCometHandler.onEvent(CometEvent).voidrecycle()Recycle this object.removeAttribute(Object key) Remove an attribute.booleanremoveCometHandler(CometHandler handler) Remove aCometHandler.booleanremoveCometHandler(CometHandler handler, boolean resume) Remove aCometHandler.booleanresumeCometHandler(CometHandler handler) Resume the Comet request and remove it from the activeCometHandlerlist.voidsetDetectClosedConnections(boolean isDetectClosedConnections) Enable/disable the mechanism, which detects closed connections and notifies user's handlers viaCometHandler.onInterrupt(org.glassfish.grizzly.comet.CometEvent)method.voidsetExpirationDelay(long expirationDelay) Set thelongdelay before a request is resumed.voidsetNotificationHandler(NotificationHandler notificationHandler) Set the currentNotificationHandlertoString()
-
Field Details
-
INVALID_COMET_HANDLER
Generic error message- See Also:
-
ALREADY_REMOVED
- See Also:
-
LOGGER
protected static final org.slf4j.Logger LOGGER -
REQUEST_LOCAL
-
topic
The context path associated with this instance. -
notificationHandler
The default NotificationHandler. -
eventInterrupt
-
eventTerminate
-
-
Constructor Details
-
CometContext
Create a new instance- Parameters:
contextTopic- the context path
-
-
Method Details
-
getContextPath
Deprecated.- use getTopic.Get the context path associated with this instance.- Returns:
- topic the context path associated with this instance
-
getTopic
Get the topic representing this instance with this instance. This is the value to uses when invokingCometEngine.getCometContext(java.lang.String)- Returns:
- topic the topic associated with this instance
-
addAttribute
Add an attribute.- Parameters:
key- the keyvalue- the value
-
getAttribute
Retrieve an attribute.- Parameters:
key- the key- Returns:
- Object the value.
-
removeAttribute
Remove an attribute.- Parameters:
key- the key- Returns:
- Object the value
-
addCometHandler
Add aCometHandler. The underlying HttpServletResponse will not get committed untilresumeCometHandler(CometHandler)is invoked, unless thesetExpirationDelay(long)expires. If set to alreadySuspended is set to true, no I/O operations are allowed inside theCometHandleras the underlying HttpServletResponse has not been suspended. Adding suchCometHandleris 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- a newCometHandler- Returns:
- The hash code of the handler.
-
recycle
public void recycle()Recycle this object. -
invokeCometHandler
Invoke aCometHandlerusing theCometEvent- Parameters:
event- -CometEventcometHandler- -CometHandler- Throws:
IOException
-
removeCometHandler
Remove aCometHandler. If the continuation (connection) associated with thisCometHandlerno longer haveCometHandlerassociated to it, it will be resumed by Grizzly by callingresumeCometHandler(CometHandler)- Returns:
- true if the operation succeeded.
-
removeCometHandler
Remove aCometHandler. If the continuation (connection) associated with thisCometHandlerno longer haveCometHandlerassociated to it, it will be resumed.- Parameters:
handler- The CometHandler to remove.resume- True is the connection can be resumed if no CometHandler are associated with the underlying SelectionKey.- Returns:
- true if the operation succeeded.
-
resumeCometHandler
Resume the Comet request and remove it from the activeCometHandlerlist. 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- The CometHandler to resume.- Returns:
- true if the operation succeeded.
- Throws:
IOException
-
interrupt
Interrupt aCometHandlerby invokingCometHandler.onInterrupt(org.glassfish.grizzly.comet.CometEvent)- Parameters:
handler- TheCometHandlerencapsulating the suspended connection.finishExecution- Finish the current execution.- Throws:
IOException
-
interrupt0
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- TheCometHandlerencapsulating the suspended connection.finishExecution- Finish the current execution.- Throws:
IOException
-
isActive
Return true if thisCometHandleris still active, e.g. there is still a suspended connection associated with it.- Returns:
- true
-
notify
Notify allCometHandler. AllCometHandler.onEvent(org.glassfish.grizzly.comet.CometEvent)will be invoked with aCometEventof type NOTIFY.- Parameters:
attachment- An object shared amongstCometHandler.- Throws:
IOException
-
notify
Notify a singleCometHandler.onEvent(CometEvent).- Parameters:
attachment- An object shared amongstCometHandler.cometHandler-CometHandlerto notify.- Throws:
IOException
-
notify
public void notify(E attachment, CometEvent.Type eventType, CometHandler cometHandler) throws IOException Notify a singleCometHandler. TheCometEvent.getType()will determine whichCometHandlermethod 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- An object shared amongstCometHandler.eventType- The type of notification.cometHandler-CometHandlerto notify.- Throws:
IOException
-
notify
Notify allCometHandler. TheCometEvent.getType()will determine whichCometHandlermethod will be invoked:CometEvent.Type.INTERRUPT ->CometHandler.onInterrupt(org.glassfish.grizzly.comet.CometEvent)CometEvent.Type.NOTIFY ->CometHandler.onEvent(org.glassfish.grizzly.comet.CometEvent)CometEvent.Type.INITIALIZE ->CometHandler.onInitialize(org.glassfish.grizzly.comet.CometEvent)CometEvent.Type.TERMINATE ->CometHandler.onTerminate(org.glassfish.grizzly.comet.CometEvent)CometEvent.Type.READ ->CometHandler.onEvent(org.glassfish.grizzly.comet.CometEvent)- Parameters:
attachment- An object shared amongstCometHandler.eventType- The type of notification.- Throws:
IOException
-
initialize
Initialize the newly addedCometHandler.- Throws:
IOException
-
toString
-
getExpirationDelay
public long getExpirationDelay()Return thelongdelay, in millisecond, before a request is resumed.- Returns:
- long the
longdelay, in millisecond, before a request is resumed.
-
setExpirationDelay
public void setExpirationDelay(long expirationDelay) Set thelongdelay before a request is resumed.- Parameters:
expirationDelay- thelongdelay before a request is resumed. Value is in milliseconds.
-
getCometHandlers
Return the current list of activeCometHandler- Returns:
- the current list of active
CometHandler
-
setNotificationHandler
Set the currentNotificationHandler -
getNotificationHandler
Return the associatedNotificationHandler -
setDetectClosedConnections
public void setDetectClosedConnections(boolean isDetectClosedConnections) Enable/disable the mechanism, which detects closed connections and notifies user's handlers viaCometHandler.onInterrupt(org.glassfish.grizzly.comet.CometEvent)method. If this feature is on - HTTP pipelining can not be used.- Parameters:
isDetectClosedConnections-
-
isDetectClosedConnections
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.
-