|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.sun.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(java.lang.Object)
, or share information with other
CometHandler
. This is the equivalent of server push as the CometContext
will invoke all registered CometHandler (addCometHandler(com.sun.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.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(com.sun.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(com.sun.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.
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
invokation of notify(java.lang.Object)
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(com.sun.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 | |
---|---|
protected static String |
ALREADY_REMOVED
|
protected boolean |
blockingNotification
true if the caller of notify(java.lang.Object) should block when
notifying other CometHandler. |
protected int |
continuationType
The CometContext continuationType. |
protected CometEvent |
eventInterrupt
|
protected CometEvent |
eventTerminate
|
protected ConcurrentHashMap<CometHandler,CometTask> |
handlers
The list of registered CometHandler |
protected static String |
INVALID_COMET_HANDLER
Generic error message |
protected static Logger |
logger
Main logger |
protected NotificationHandler |
notificationHandler
The default NotificationHandler. |
protected String |
topic
The context path associated with this instance. |
Constructor Summary | |
---|---|
CometContext(String contextTopic,
int type)
Create a new instance |
Method Summary | |
---|---|
protected void |
addActiveHandler(CometTask cometTask)
adds a CometTask to the active set |
void |
addAttribute(Object key,
Object value)
Add an attibute. |
int |
addCometHandler(CometHandler handler)
Add a CometHandler which will starts the process of suspending
the underlying response. |
int |
addCometHandler(CometHandler handler,
boolean alreadySuspended)
Add a CometHandler . |
Object |
getAttribute(Object key)
Retrieve an attribute. |
CometHandler |
getCometHandler(int hashCode)
Deprecated. |
Set<CometHandler> |
getCometHandlers()
Return the current list of active CometHandler |
String |
getContextPath()
Deprecated. - use getTopic. |
long |
getExpirationDelay()
Return the long delay, in millisecond, before a request is resumed. |
NotificationHandler |
getNotificationHandler()
Return the associated NotificationHandler |
String |
getTopic()
Get the topic representing this instance with this instance. |
protected ConcurrentHashMap<CometHandler,CometTask> |
handlers()
Return the internal list of active CometHandler |
protected void |
initialize(CometHandler handler)
Initialize the newly added CometHandler . |
protected void |
invokeCometHandler(CometEvent event,
CometHandler cometHandler)
Invoke a CometHandler using the CometEvent |
boolean |
isActive(CometHandler handler)
Return true if this CometHandler is still active, e.g. |
boolean |
isBlockingNotification()
Return true if the invoker of notify(java.lang.Object) should block when
notifying Comet Handlers. |
void |
notify(Object attachment)
Notify all CometHandler . |
void |
notify(Object attachment,
CometHandler cometHandler)
Notify a single CometHandler#onEvent(CometEvent . |
void |
notify(Object attachment,
int eventType)
Notify all CometHandler . |
void |
notify(Object attachment,
int eventType,
CometHandler cometHandler)
Notify a single CometHandler . |
void |
notify(Object attachment,
int eventType,
int cometHandlerID)
Deprecated. - use notify(attachment,eventType,CometHandler; |
void |
recycle()
Recycle this object. |
boolean |
registerAsyncRead(CometHandler handler)
Register for asynchronous read event (CometEvent#READ}. |
boolean |
registerAsyncWrite(CometHandler handler)
Register for asynchronous write event (CometEvent#WRITE} .As soon as Grizzly detects there is some OS buffer available for write operations, your CometHandler.onEvent(CometEvent) will be invoked. |
Object |
removeAttribute(Object key)
Remove an attribute. |
boolean |
removeCometHandler(CometHandler handler)
Remove a CometHandler . |
boolean |
removeCometHandler(CometHandler handler,
boolean resume)
Remove a CometHandler . |
boolean |
removeCometHandler(int hashCode)
Deprecated. |
protected void |
resetSuspendIdleTimeout()
Reset the current timestamp on a suspended connection. |
boolean |
resumeCometHandler(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(java.lang.Object) should block when
notifying Comet Handlers. |
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 static final String ALREADY_REMOVED
protected static final Logger logger
protected String topic
protected int continuationType
CometContext
continuationType. See CometEngine
protected boolean blockingNotification
notify(java.lang.Object)
should block when
notifying other CometHandler.
protected NotificationHandler notificationHandler
protected final ConcurrentHashMap<CometHandler,CometTask> handlers
CometHandler
protected final CometEvent eventInterrupt
protected final CometEvent eventTerminate
Constructor Detail |
---|
public CometContext(String contextTopic, int type)
contextTopic
- 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 alreadySuspended)
CometHandler
. The underlying HttpServletResponse will
not get commited until resumeCometHandler(CometHandler)
is invoked, unless the 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 usefull 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.
handler
- a new CometHandler
alreadySuspended
- Add the Comethandler but don't suspend
the HttpServletResponse. If set to true, no
I/O operations are allowed inside the CometHandler
as the
underlying HttpServletResponse has not been suspended, unless
the CometHandler
is shared amongs more than one CometContext
CometHandler#hashCode
value.public int addCometHandler(CometHandler handler)
CometHandler
which will starts the process of suspending
the underlying response. The underlying HttpServletResponse will
not get committed until resumeCometHandler(CometHandler)
is invoked, unless the setExpirationDelay(long)
expires.
handler
- a new CometHandler
@Deprecated public CometHandler getCometHandler(int hashCode)
CometHandler
using its based on its CometHandler#hashCode
;
public void recycle()
protected void addActiveHandler(CometTask cometTask)
CometTask
to the active set
cometTask
- CometTask
protected void invokeCometHandler(CometEvent event, CometHandler cometHandler) throws IOException
CometHandler
using the CometEvent
event
- - CometEvent
cometHandler
- - CometHandler
IOException
public boolean removeCometHandler(CometHandler handler)
CometHandler
. If the continuation (connection)
associated with this CometHandler
no longer have
CometHandler
associated to it, it will be resumed by Grizzly
by calling resumeCometHandler(CometHandler)
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.
@Deprecated public boolean removeCometHandler(int hashCode)
CometHandler
based on its hashcode. Return true
if the operation was sucessfull.
O(n) performance.
hashCode
- The hashcode of the CometHandler to remove.
public boolean resumeCometHandler(CometHandler handler)
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.
handler
- The CometHandler to resume.
public boolean isActive(CometHandler handler)
CometHandler
is still active, e.g. there is
still a suspended connection associated with it.
protected ConcurrentHashMap<CometHandler,CometTask> handlers()
CometHandler
CometHandler
public void notify(Object attachment) throws IOException
CometHandler
. All
CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
will be invoked with a CometEvent
of type NOTIFY.
attachment
- An object shared amongst CometHandler
.
IOException
public void notify(Object attachment, int eventType, int cometHandlerID) throws IOException
CometHandler
. The CometEvent.getType()
will determine which CometHandler
method will be invoked:
CometEvent.INTERRUPT -> CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)
CometEvent.NOTIFY -> CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
CometEvent.INITIALIZE -> CometHandler.onInitialize(com.sun.grizzly.comet.CometEvent)
CometEvent.TERMINATE -> CometHandler.onTerminate(com.sun.grizzly.comet.CometEvent)
CometEvent.READ -> CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
attachment
- An object shared amongst CometHandler
.eventType
- The type of notification.cometHandlerID
- Notify a single CometHandler.
IOException
public void notify(Object attachment, CometHandler cometHandler) throws IOException
CometHandler#onEvent(CometEvent
.
attachment
- An object shared amongst CometHandler
.{@link
- CometHandler} to notify.
IOException
public void notify(Object attachment, int eventType, CometHandler cometHandler) throws IOException
CometHandler
. The CometEvent.getType()
will determine which CometHandler
method will be invoked:
CometEvent.INTERRUPT -> CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)
CometEvent.NOTIFY -> CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
CometEvent.INITIALIZE -> CometHandler.onInitialize(com.sun.grizzly.comet.CometEvent)
CometEvent.TERMINATE -> CometHandler.onTerminate(com.sun.grizzly.comet.CometEvent)
CometEvent.READ -> CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
attachment
- An object shared amongst CometHandler
.eventType
- The type of notification.{@link
- CometHandler} to notify.
IOException
public void notify(Object attachment, int eventType) throws IOException
CometHandler
. The CometEvent.getType()
will determine which CometHandler
method will be invoked:
CometEvent.INTERRUPT -> CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)
CometEvent.NOTIFY -> CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
CometEvent.INITIALIZE -> CometHandler.onInitialize(com.sun.grizzly.comet.CometEvent)
CometEvent.TERMINATE -> CometHandler.onTerminate(com.sun.grizzly.comet.CometEvent)
CometEvent.READ -> CometHandler.onEvent(com.sun.grizzly.comet.CometEvent)
attachment
- An object shared amongst CometHandler
.eventType
- The type of notification.
IOException
protected void initialize(CometHandler handler) throws IOException
CometHandler
.
IOException
protected void resetSuspendIdleTimeout()
public boolean registerAsyncRead(CometHandler handler)
CometHandler.onEvent(CometEvent)
will be invoked.
CometEvent.attachment()
will return an instance of CometReader
that can be used to asynchronously read the available bytes.
If your client supports http pipelining,
invoking this method might result in a state where your CometHandler
is invoked with a CometReader
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.
handler
- The CometHandler that will be invoked.public boolean registerAsyncWrite(CometHandler handler)
CometHandler.onEvent(CometEvent)
will be invoked.
CometEvent.attachment()
will return an instance of CometWriter
that can be used to asynchronously write the available bytes.
handler
- The CometHandler that will be invoked.public String toString()
toString
in class Object
public long getExpirationDelay()
long
delay, in millisecond, before a request is resumed.
long
delay, in millisecond, before a request is resumed.public void setExpirationDelay(long expirationDelay)
long
delay before a request is resumed.
expirationDelay
- the long
delay before a request is resumed.
Value is in milliseconds.public Set<CometHandler> getCometHandlers()
CometHandler
CometHandler
public boolean isBlockingNotification()
notify(java.lang.Object)
should block when
notifying Comet Handlers.
public void setBlockingNotification(boolean blockingNotification)
notify(java.lang.Object)
should block when
notifying Comet Handlers.
public void setNotificationHandler(NotificationHandler notificationHandler)
NotificationHandler
notificationHandler
- public NotificationHandler getNotificationHandler()
NotificationHandler
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |