com.sun.grizzly.comet
Class CometEngine

java.lang.Object
  extended by com.sun.grizzly.comet.CometEngine
Direct Known Subclasses:
CometEngine

public class CometEngine
extends Object

Main class allowing Comet support on top of Grizzly Asynchronous Request Processing mechanism. This class is the entry point to any component interested to execute Comet request style. Components can be Servlets, JSP, JSF or pure Java class. A component interested to support Comet request must do:


 (1) First, register the topic on which Comet support will be applied:
     CometEngine cometEngine = CometEngine.getEngine()
     CometContext cometContext = cometEngine.register(topic)
 (2) Second, add an instance of CometHandler to the
     CometContext returned by the register method:
     CometContext.addCometHandler(com.sun.grizzly.comet.CometHandler, boolean). Executing this operation
     will tells Grizzly to suspend the response.
 (3) Finally, you can CometContext.notify(java.lang.Object) other CometHandler 
     to share information between . When notified, 
     CometHandler can decides to push back the data, resume the 
     response, or simply ignore the content of the notification.
 
You can also select the stage where the suspension of the response happens when registering the CometContext's topic (see register(java.lang.String)), which can be before, during or after invoking a Servlet

Author:
Jeanfrancois Arcand, Gustav Trede

Field Summary
protected  ConcurrentHashMap<String,CometContext> activeContexts
          The current active CometContext keyed by context path.
static int AFTER_RESPONSE_PROCESSING
          The token used to support BEFORE_RESPONSE_PROCESSING polling.
static int AFTER_SERVLET_PROCESSING
          The token used to support AFTER_SERVLET_PROCESSING polling.
static int BEFORE_REQUEST_PROCESSING
          The token used to support BEFORE_REQUEST_PROCESSING polling.
protected  Queue<CometContext> cometContextCache
          cached CometContexts
protected static CometEngine cometEngine
          The single instance of this class.
static int DISABLE_CLIENT_DISCONNECTION_DETECTION
           
static int DISABLE_SUSPEND_TIMEOUT
           
protected static Logger logger
          Main logger
protected  ExtendedThreadPool threadPool
          The ExecutorService used to execute CometTask
protected static ThreadLocal<CometTask> updatedContexts
          Store updatedCometContext.
 
Constructor Summary
protected CometEngine()
          Creat a singleton and initialize all lists required.
 
Method Summary
protected  void flushPostExecute(CometTask task, boolean cancelKey)
          Ensures ProcessorTask is recycled and that SelectionKey is canceled when needed.
 CometContext getCometContext(String topic)
          Return the CometContext associated with the topic.
static CometEngine getEngine()
          Return a singleton of this Class.
 ExtendedThreadPool getThreadPool()
          returns the thread pool comet is using
protected  boolean handle(AsyncProcessorTask apt)
          Handle an interrupted(or polled) request by matching the current context path with the registered one.
protected  boolean interrupt(CometTask task, boolean finishExecution)
          Interrupt a CometHandler by invoking CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)
protected  void interrupt0(CometTask task, 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  boolean isCometEnabled()
          Return true is Comet is enabled, e.g.
static Logger logger()
          Return the current logger.
 CometContext register(String topic)
          Register a context path with this CometEngine.
 CometContext register(String topic, int type)
          Register a context path with this CometEngine.
 CometContext register(String topic, int type, Class<? extends NotificationHandler> notificationClass)
          Instantiate a new CometContext.
 void setThreadPool(ExtendedThreadPool pool)
          sets the default ThreadPool that DefaultNotificationHandler use.
 CometContext unregister(String topic)
          Unregister the CometHandler to the list of the CometContext.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DISABLE_SUSPEND_TIMEOUT

public static final int DISABLE_SUSPEND_TIMEOUT
See Also:
Constant Field Values

DISABLE_CLIENT_DISCONNECTION_DETECTION

public static final int DISABLE_CLIENT_DISCONNECTION_DETECTION
See Also:
Constant Field Values

BEFORE_REQUEST_PROCESSING

public static final int BEFORE_REQUEST_PROCESSING
The token used to support BEFORE_REQUEST_PROCESSING polling.

See Also:
Constant Field Values

AFTER_SERVLET_PROCESSING

public static final int AFTER_SERVLET_PROCESSING
The token used to support AFTER_SERVLET_PROCESSING polling.

See Also:
Constant Field Values

AFTER_RESPONSE_PROCESSING

public static final int AFTER_RESPONSE_PROCESSING
The token used to support BEFORE_RESPONSE_PROCESSING polling.

See Also:
Constant Field Values

logger

protected static final Logger logger
Main logger


threadPool

protected ExtendedThreadPool threadPool
The ExecutorService used to execute CometTask


cometEngine

protected static final CometEngine cometEngine
The single instance of this class.


activeContexts

protected final ConcurrentHashMap<String,CometContext> activeContexts
The current active CometContext keyed by context path.


cometContextCache

protected final Queue<CometContext> cometContextCache
cached CometContexts


updatedContexts

protected static final ThreadLocal<CometTask> updatedContexts
Store updatedCometContext.

Constructor Detail

CometEngine

protected CometEngine()
Creat a singleton and initialize all lists required.

Method Detail

isCometEnabled

protected boolean isCometEnabled()
Return true is Comet is enabled, e.g. SelectorThread.setEnableAsyncExecution(boolean) has been set to true

Returns:

getEngine

public static CometEngine getEngine()
Return a singleton of this Class.

Returns:
CometEngine the singleton.

setThreadPool

public final void setThreadPool(ExtendedThreadPool pool)
sets the default ThreadPool that DefaultNotificationHandler use. shutdownNow is called on the existing ThreadPool. does not update existing notification handlers.


getThreadPool

public ExtendedThreadPool getThreadPool()
returns the thread pool comet is using

Returns:
ExtendedThreadPool

unregister

public CometContext unregister(String topic)
Unregister the CometHandler to the list of the CometContext. Invoking this method will invoke all CometHandler.onTerminate(CometEvent) before removing the associated CometContext. Invoking that method will also resume the underlying connection associated with the CometHandler, similar to what CometContext.resumeCometHandler(CometHandler) do.


register

public CometContext register(String topic)
Register a context path with this CometEngine. The CometContext returned will be of type AFTER_SERVLET_PROCESSING, which means the request target (most probably a Servlet) will be executed first and then polled.

Parameters:
topic - the context path used to create the CometContext
Returns:
CometContext a configured CometContext.

register

public CometContext register(String topic,
                             int type)
Register a context path with this CometEngine. The CometContext returned will be of type type.

Parameters:
topic - the context path used to create the CometContext
type - when the request will be suspended, e.g. BEFORE_REQUEST_PROCESSING, AFTER_SERVLET_PROCESSING or AFTER_RESPONSE_PROCESSING
Returns:
CometContext a configured CometContext.

register

public CometContext register(String topic,
                             int type,
                             Class<? extends NotificationHandler> notificationClass)
Instantiate a new CometContext.

Parameters:
topic - the topic the new CometContext will represent.
type - when the request will be suspended, e.g. BEFORE_REQUEST_PROCESSING, AFTER_SERVLET_PROCESSING or AFTER_RESPONSE_PROCESSING
Returns:
a new CometContext if not already created, or the existing one.

handle

protected boolean handle(AsyncProcessorTask apt)
                  throws IOException
Handle an interrupted(or polled) request by matching the current context path with the registered one. If required, the bring the target component (Servlet) to the proper execution stage and then CometContext.notify(java.lang.Object) the CometHandler

Parameters:
apt - the current apt representing the request.
Returns:
boolean true if the request can be polled.
Throws:
IOException

getCometContext

public CometContext getCometContext(String topic)
Return the CometContext associated with the topic.

Parameters:
topic - the topic used to creates the CometContext

interrupt

protected boolean interrupt(CometTask task,
                            boolean finishExecution)
Interrupt a CometHandler by invoking CometHandler.onInterrupt(com.sun.grizzly.comet.CometEvent)

Parameters:
task - The CometTask encapsulating the suspended connection.
finishExecution - Finish the current execution.

interrupt0

protected void interrupt0(CometTask task,
                          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.

Parameters:
task - The CometTask encapsulating the suspended connection.
finishExecution - Finish the current execution.

flushPostExecute

protected void flushPostExecute(CometTask task,
                                boolean cancelKey)
Ensures ProcessorTask is recycled and that SelectionKey is canceled when needed.

Parameters:
task -
cancelKey -

logger

public static Logger logger()
Return the current logger.



Copyright © 2012 Oracle Corporation. All Rights Reserved.