com.sun.enterprise.web.connector.grizzly.comet
Class CometEngine

java.lang.Object
  extended by com.sun.enterprise.web.connector.grizzly.comet.CometEngine

public final 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.enterprise.web.connector.grizzly.comet.CometHandler, boolean). Executing this operation
     will tells Grizzly to suspend the response.
 (3) Finally, you can CometContext.notify(E) 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

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> cometContexts
          Cache of CometContext instance.
protected  CometSelector cometSelector
          The CometSelector used to poll requests.
protected  Queue<CometTask> cometTasks
          Cache of CometTask instance
static int DISABLE_CLIENT_DISCONNECTION_DETECTION
           
static int DISABLE_SUSPEND_TIMEOUT
           
protected static String notificationHandlerClassName
          The default class to use when deciding which NotificationHandler to use.
protected  Pipeline pipeline
          The Pipeline used to execute CometTask
protected  ConcurrentHashMap<Long,SelectionKey> threadsId
          Temporary repository that associate a Thread ID with a Key.
protected  ConcurrentHashMap<Long,CometContext> updatedCometContexts
          Store modified CometContext.
 
Constructor Summary
protected CometEngine()
          Create a singleton and initialize all lists required.
 
Method Summary
protected  SelectionKey activateContinuation(Long threadId, CometContext cometContext, boolean continueExecution)
          Tell the CometEngine to activate Grizzly ARP on that CometContext.
 CometContext getCometContext(String topic)
          Return the CometContext associated with the topic.
static CometEngine getEngine()
          Return a singleton of this Class.
static String getNotificationHandlerClassName()
          Return the default NotificationHandler class name.
 Pipeline getPipeline()
           
protected  boolean handle(AsyncProcessorTask apt)
          Handle an interrupted(or polled) request by matching the current context path with the registered one.
protected  void interrupt(CometTask cometTask)
           
protected  void interrupt(SelectionKey key)
          The CometSelector is expiring idle SelectionKey, hence we need to resume the current request.
protected static NotificationHandler loadNotificationHandlerInstance(String className)
          Util to load classes using reflection.
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 CometContext> contextclass)
          Instanciate a new CometContext.
protected  void resume(SelectionKey key)
          Resume the long polling request by unblocking the current SelectionKey
protected  void returnTask(CometTask cometTask)
          Return a Task to the pool.
static void setNotificationHandlerClassName(String aNotificationHandlerClassName)
          Set the default NotificationHandler class name.
 void setPipeline(Pipeline pipeline)
           
 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

pipeline

protected volatile Pipeline pipeline
The Pipeline used to execute CometTask


activeContexts

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


cometTasks

protected Queue<CometTask> cometTasks
Cache of CometTask instance


cometContexts

protected Queue<CometContext> cometContexts
Cache of CometContext instance.


cometSelector

protected CometSelector cometSelector
The CometSelector used to poll requests.


notificationHandlerClassName

protected static String notificationHandlerClassName
The default class to use when deciding which NotificationHandler to use. The default is DefaultNotificationHandler.


threadsId

protected ConcurrentHashMap<Long,SelectionKey> threadsId
Temporary repository that associate a Thread ID with a Key. NOTE: A ThreadLocal might be more efficient.


updatedCometContexts

protected ConcurrentHashMap<Long,CometContext> updatedCometContexts
Store modified CometContext.

Constructor Detail

CometEngine

protected CometEngine()
Create a singleton and initialize all lists required. Also create and start the CometSelector

Method Detail

getEngine

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

Returns:
CometEngine the singleton.

unregister

public CometContext unregister(String topic)
Unregister the CometHandler to the list of the CometContext. Invoking this method will invoke all CometHandler.onTerminate(com.sun.enterprise.web.connector.grizzly.comet.CometEvent) before removing the associated CometContext. Invoking that method will also resume the underlying connection associated with the CometHandler, similar to what CometContext.resumeCometHandler(com.sun.enterprise.web.connector.grizzly.comet.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 CometContext> contextclass)
Instanciate 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
contextclass - The CometContext class to instanticate.
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(E) the CometHandler

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

activateContinuation

protected SelectionKey activateContinuation(Long threadId,
                                            CometContext cometContext,
                                            boolean continueExecution)
Tell the CometEngine to activate Grizzly ARP on that CometContext. This method is called when CometContext.addCometHandler() is invoked.

Parameters:
threadId - the Thread.getId().
cometContext - An instance of CometContext.
Returns:
key The SelectionKey associated with the current request.

getCometContext

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

Parameters:
topic - the topic used to creates the CometContext

interrupt

protected void interrupt(SelectionKey key)
The CometSelector is expiring idle SelectionKey, hence we need to resume the current request.

Parameters:
key - the expired SelectionKey

interrupt

protected void interrupt(CometTask cometTask)

returnTask

protected void returnTask(CometTask cometTask)
Return a Task to the pool.


resume

protected void resume(SelectionKey key)
Resume the long polling request by unblocking the current SelectionKey


getNotificationHandlerClassName

public static String getNotificationHandlerClassName()
Return the default NotificationHandler class name.

Returns:
the default NotificationHandler class name.

setNotificationHandlerClassName

public static void setNotificationHandlerClassName(String aNotificationHandlerClassName)
Set the default NotificationHandler class name.

Parameters:
the - default NotificationHandler class name.

getPipeline

public Pipeline getPipeline()

setPipeline

public void setPipeline(Pipeline pipeline)

loadNotificationHandlerInstance

protected static final NotificationHandler loadNotificationHandlerInstance(String className)
Util to load classes using reflection.


logger

public static final Logger logger()
Return the current logger.



Copyright © 2005-2015 Oracle Corporation. All Rights Reserved.