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 cometContext path on which Comet support will be applied:
     CometEngine cometEngine = CometEngine.getEngine()
     CometContext cometContext = cometEngine.register(contextPath)
 (2) Second, add an instance of CometHandler to the
     CometContext returned by the register method:
     cometContext.addCometHandler(handler);
 (3) Finally, you can notify other CometHandler by doing:
     cometContext.notify(Object)(handler);
 
You can also select the stage where the request polling happens when registering the cometContext path (see register(String,int);

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  ConcurrentLinkedQueue<CometContext> cometContexts
          Cache of CometContext instance.
protected  CometSelector cometSelector
          The CometSelector used to poll requests.
protected  ConcurrentLinkedQueue<CometTask> cometTasks
          Cache of CometTask instance
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()
          Creat 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 contextPath)
          Return the CometContext associated with the cometContext path.
protected  CometTask getCometTask(CometContext cometContext, SelectionKey key, Pipeline ctxPipeline)
          Return a clean and configured CometTask
static CometEngine getEngine()
          Return a singleton of this Class.
static String getNotificationHandlerClassName()
          Return the default NotificationHandler class name.
protected  boolean handle(AsyncProcessorTask apt)
          Handle an interrupted(or polled) request by matching the current context path with the registered one.
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 contextPath)
          Register a context path with this CometEngine.
 CometContext register(String contextPath, int type)
          Register a context path with this CometEngine.
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.
 CometContext unregister(String contextPath)
          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

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 Pipeline pipeline
The Pipeline used to execute CometTask


activeContexts

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


cometTasks

protected ConcurrentLinkedQueue<CometTask> cometTasks
Cache of CometTask instance


cometContexts

protected ConcurrentLinkedQueue<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()
Creat 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 contextPath)
Unregister the CometHandler to the list of the CometContext.


register

public CometContext register(String contextPath)
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:
contextPath - the context path used to create the CometContext
Returns:
CometContext a configured CometContext.

register

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

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

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 notify 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.

getCometTask

protected CometTask getCometTask(CometContext cometContext,
                                 SelectionKey key,
                                 Pipeline ctxPipeline)
Return a clean and configured CometTask

Parameters:
cometContext - the CometContext to clean
key - The current SelectionKey
Returns:
a new CometContext

getCometContext

public CometContext getCometContext(String contextPath)
Return the CometContext associated with the cometContext path. XXX: This is not secure as a you can get a CometContext associated with another cometContext path. But this allow interesting application... MUST BE REVISTED.

Parameters:
contextPath - the request's cometContext path.

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

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.

loadNotificationHandlerInstance

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


logger

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



Copyright © 2009 SUN Microsystems. All Rights Reserved.