|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.enterprise.web.connector.grizzly.comet.CometContext<E>
public class CometContext<E>
The main object used by CometHandler and Servlet to push information
amongs 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(com.sun.enterprise.web.connector.grizzly.comet.CometHandler, boolean)) sequentially
or using a thread pool when the setBlockingNotification(boolean) is set to
false
A CometContext can be considered as a topic where CometHandler register for information. A CometContext can be shared amongs Servlet of the same application, or globally accros all deployed web applications. Normally, a CometContext is created using a topic's name like:<
CometEngine ce = CometEngine.getEngine();
CometContext cc = ce.registerContext("MyTopic");
cc.setBlockingNotification(false); // Use multiple thread to execute the server push
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 soom as addCometHandler(com.sun.enterprise.web.connector.grizzly.comet.CometHandler, boolean) 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(com.sun.enterprise.web.connector.grizzly.comet.CometHandler, boolean). 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.
Attributes can be added/removed the same way HttpServletSession
is doing. It is not recommended to use attributes if this
CometContext is not shared amongs multiple
context path (uses HttpServletSession instead).
| Field Summary | |
|---|---|
protected Queue<CometTask> |
activeTasks
Current associated list of CometTask |
protected boolean |
blockingNotification
true if the caller of notify(E) should block when
notifying other CometHandler. |
protected boolean |
cancelled
Is the CometContext instance been cancelled. |
protected CometSelector |
cometSelector
The CometSelector used to register SelectionKey
for upcoming bytes. |
protected int |
continuationType
The CometContext continuationType. |
protected ConcurrentHashMap<CometHandler,SelectionKey> |
handlers
The list of registered CometHandler |
protected static String |
INVALID_COMET_HANDLER
Generic error message |
protected NotificationHandler |
notificationHandler
The default NotificationHandler. |
| Constructor Summary | |
|---|---|
CometContext(String topic,
int continuationType)
Create a new instance |
|
| Method Summary | |
|---|---|
protected void |
addActiveCometTask(CometTask cometTask)
Add a CometTask to the active list. |
void |
addAttribute(Object key,
Object value)
Add an attibute. |
int |
addCometHandler(CometHandler handler)
Add a CometHandler. |
int |
addCometHandler(CometHandler handler,
boolean completeExecution)
Add a CometHandler. |
protected static void |
addInProgressSelectionKey(SelectionKey key)
Add a SelectionKey to the list of current operations. |
Object |
getAttribute(Object key)
Retrive an attribute. |
CometHandler |
getCometHandler(int hashCode)
Retrive a CometHandler using its hashKey; |
protected CometHandler |
getCometHandler(SelectionKey key)
Retrive a CometHandler using its SelectionKey. |
Set<CometHandler> |
getCometHandlers()
Return the current list of active CometHandler
return the current list of active CometHandler |
String |
getContextPath()
Deprecated. - use getTopic. |
long |
getExpirationDelay()
Return the long delay before a request is resumed. |
NotificationHandler |
getNotificationHandler()
Return the associated NotificationHandler |
String |
getTopic()
Get the topic representing this instance with this instance. |
protected void |
initialize(SelectionKey key)
Initialize the newly added CometHandler. |
protected void |
interrupt(CometTask task)
Interrupt a CometHandler by invoking CometHandler.onInterrupt(com.sun.enterprise.web.connector.grizzly.comet.CometEvent) |
protected void |
invokeCometHandler(CometEvent event,
int eventType,
SelectionKey key)
Notify all CometHandler. |
boolean |
isActive(CometHandler cometHandler)
Return true if this CometHandler is still active, e.g. |
boolean |
isBlockingNotification()
Return true if the invoker of notify(E) should block when
notifying Comet Handlers. |
protected boolean |
isCancelled()
Is this instance beeing cancelled by the CometSelector |
void |
notify(E attachment)
Notify all CometHandler. |
void |
notify(E attachment,
int eventType)
Notify all CometHandler. |
void |
notify(E attachment,
int eventType,
int cometHandlerID)
Notify a single CometHandler. |
protected void |
recycle()
Recycle this object. |
boolean |
registerAsyncRead(CometHandler handler)
Register for asynchronous read. |
boolean |
registerAsyncWrite(CometHandler handler)
Register for asynchronous write. |
Object |
removeAttribute(Object key)
Remove an attribute. |
void |
removeCometHandler(CometHandler handler)
Remove a CometHandler. |
boolean |
removeCometHandler(CometHandler handler,
boolean resume)
Remove a CometHandler. |
boolean |
removeCometHandler(int hashCode)
Remove a CometHandler based on its hashcode. |
boolean |
removeCometHandler0(CometHandler handler)
Remove a CometHandler. |
protected static boolean |
removeInProgressSelectionKey(SelectionKey key)
Remove a SelectionKey to the list of current operations. |
protected void |
resetSuspendIdleTimeout()
Reset the current timestamp on a suspended connection. |
void |
resumeCometHandler(CometHandler handler)
Resume the Comet request and remove it from the active CometHandler list. |
protected boolean |
resumeCometHandler(CometHandler handler,
boolean remove)
Resume the Comet request. |
boolean |
resumeCometHandler0(CometHandler handler)
Resume the Comet request and remove it from the active CometHandler list. |
void |
setBlockingNotification(boolean blockingNotification)
Set to true if the invoker of notify(E) should block when
notifying Comet Handlers. |
protected void |
setCancelled(boolean cancelled)
Cancel this object or "uncancel". |
protected void |
setCometSelector(CometSelector cometSelector)
Set the CometSelector associated with this instance. |
void |
setExpirationDelay(long expirationDelay)
Set the long delay before a request is resumed. |
void |
setNotificationHandler(NotificationHandler notificationHandler)
Set the current NotificationHandler |
String |
toString()
Helper. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected static final String INVALID_COMET_HANDLER
protected boolean cancelled
CometContext instance been cancelled.
protected ConcurrentHashMap<CometHandler,SelectionKey> handlers
CometHandler
protected CometSelector cometSelector
CometSelector used to register SelectionKey
for upcoming bytes.
protected int continuationType
CometContext continuationType. See CometEngine
protected boolean blockingNotification
notify(E) should block when
notifying other CometHandler.
protected NotificationHandler notificationHandler
protected Queue<CometTask> activeTasks
CometTask
| Constructor Detail |
|---|
public CometContext(String topic,
int continuationType)
topic - the context pathtype - when the Comet processing will happen (see CometEngine).| Method Detail |
|---|
public String getContextPath()
public String getTopic()
CometEngine.getCometContext(java.lang.String)
public void addAttribute(Object key,
Object value)
key - the keyvalue - the valuepublic Object getAttribute(Object key)
key - the key
public Object removeAttribute(Object key)
key - the key
public int addCometHandler(CometHandler handler,
boolean completeExecution)
CometHandler. Client of this method might
make sure the CometHandler is removed when the
CometHandler.onInterrupt is invoked.
handler - a new CometHandlercompleteExecution - Add the Comethandler but don't block waiting
for event.
CometHandler#hashCode value.public int addCometHandler(CometHandler handler)
CometHandler. Client on this method might
make sure the CometHandler is removed when the
CometHandler.onInterrupt is invoked.
handler - a new CometHandlerpublic CometHandler getCometHandler(int hashCode)
CometHandler using its hashKey;
protected CometHandler getCometHandler(SelectionKey key)
CometHandler using its SelectionKey. The
SelectionKey is not exposed to the Comet API, hence this
method must be protected.
protected void invokeCometHandler(CometEvent event,
int eventType,
SelectionKey key)
throws IOException
CometHandler. The attachment can be null.
The type will determine which code>CometHandler}
method will be invoked:
CometEvent.INTERRUPT -> CometHandler.onInterrupt(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.NOTIFY -> CometHandler.onEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.INITIALIZE -> CometHandler.onInitialize(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.TERMINATE -> CometHandler.onTerminate(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.READ -> CometHandler.onEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.WRITE -> @link CometHandler#onEvent}
}
attachment - An object shared amongst CometHandler.type - The type of notification.key - The SelectionKey associated with the CometHandler.
IOExceptionpublic void removeCometHandler(CometHandler handler)
CometHandler. If the continuation (connection)
associated with this CometHandler no longer have
CometHandler associated to it, it will be resumed.
public boolean removeCometHandler0(CometHandler handler)
CometHandler. If the continuation (connection)
associated with this CometHandler no longer have
CometHandler associated to it, it will be resumed.
public boolean removeCometHandler(CometHandler handler,
boolean resume)
CometHandler. If the continuation (connection)
associated with this CometHandler no longer have
CometHandler associated to it, it will be resumed.
handler - The CometHandler to remove.resume - True is the connection can be resumed if no CometHandler
are associated with the underlying SelectionKey.
public boolean removeCometHandler(int hashCode)
CometHandler based on its hashcode. Return true
if the operation was sucessfull.
hashCode - The hashcode of the CometHandler to remove.
public void resumeCometHandler(CometHandler handler)
HttpServletRequest or HttpServletResponse as
those are recycled. If you cache them for later reuse by another thread there is a
possibility to introduce corrupted responses next time a request is made.
handler - The CometHandler to resume.public boolean resumeCometHandler0(CometHandler handler)
handler - The CometHandler to resume.
protected boolean resumeCometHandler(CometHandler handler,
boolean remove)
handler - The CometHandler associated with the current continuation.remove - true if the CometHandler needs to be removed.
public boolean isActive(CometHandler cometHandler)
public void notify(E attachment)
throws IOException
CometHandler. The attachment can be null. All
CometHandler.onEvent() will be invoked.
attachment - An object shared amongst CometHandler.
IOException
public void notify(E attachment,
int eventType,
int cometHandlerID)
throws IOException
CometHandler. The attachment can be null.
The type will determine which code>CometHandler}
method will be invoked:
CometEvent.INTERRUPT -> CometHandler.onInterrupt(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.NOTIFY -> CometHandler.onEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.INITIALIZE -> CometHandler.onInitialize(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.TERMINATE -> CometHandler.onTerminate(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.READ -> CometHandler.onEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
attachment - An object shared amongst CometHandler.type - The type of notification.cometHandlerID - Notify a single CometHandler.
IOException
protected void initialize(SelectionKey key)
throws IOException
CometHandler.
attachment - An object shared amongst CometHandler.type - The type of notification.key - The SelectionKey representing the CometHandler.
IOException
public void notify(E attachment,
int eventType)
throws IOException
CometHandler. The attachment can be null.
The type will determine which code>CometHandler}
method will be invoked:
CometEvent.INTERRUPT -> CometHandler.onInterrupt(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.NOTIFY -> CometHandler.onEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.INITIALIZE -> CometHandler.onInitialize(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.TERMINATE -> CometHandler.onTerminate(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
CometEvent.READ -> CometHandler.onEvent(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
attachment - An object shared amongst CometHandler.type - The type of notification.
IOExceptionprotected void resetSuspendIdleTimeout()
public boolean registerAsyncRead(CometHandler handler)
CometRead that will read the next http request. In that
case, it is strongly recommended to not use that method unless your
CometHandler can handle the http request.
public boolean registerAsyncWrite(CometHandler handler)
protected void recycle()
protected boolean isCancelled()
CometSelector
protected void setCancelled(boolean cancelled)
cancelled - true or false.protected void setCometSelector(CometSelector cometSelector)
CometSelector associated with this instance.
CometSelector - the CometSelector associated with
this instance.public String toString()
toString in class Objectpublic long getExpirationDelay()
long delay before a request is resumed.
long delay before a request is resumed.public void setExpirationDelay(long expirationDelay)
long delay before a request is resumed.
long - the long delay before a request is resumed.protected void interrupt(CometTask task)
CometHandler by invoking CometHandler.onInterrupt(com.sun.enterprise.web.connector.grizzly.comet.CometEvent)
public Set<CometHandler> getCometHandlers()
CometHandler
return the current list of active CometHandler
protected void addActiveCometTask(CometTask cometTask)
CometTask to the active list.
cometTask - public boolean isBlockingNotification()
notify(E) should block when
notifying Comet Handlers.
public void setBlockingNotification(boolean blockingNotification)
notify(E) should block when
notifying Comet Handlers.
public void setNotificationHandler(NotificationHandler notificationHandler)
NotificationHandler
notificationHandler - public NotificationHandler getNotificationHandler()
NotificationHandler
protected static void addInProgressSelectionKey(SelectionKey key)
SelectionKey to the list of current operations.
key - protected static boolean removeInProgressSelectionKey(SelectionKey key)
SelectionKey to the list of current operations.
key -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||