Class CometContext<E>

java.lang.Object
org.glassfish.grizzly.comet.CometContext<E>

public class CometContext<E> extends Object
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(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).