com.sun.grizzly.comet.concurrent
Class DefaultConcurrentCometHandler<E>

java.lang.Object
  extended by com.sun.grizzly.comet.concurrent.DefaultConcurrentCometHandler<E>
All Implemented Interfaces:
CometHandler<E>
Direct Known Subclasses:
DefaultConcurrentCometHandler

public abstract class DefaultConcurrentCometHandler<E>
extends Object
implements CometHandler<E>

We queue events in each CometHandler to lower the probability that slow or massive IO for one CometHandler severely delays events to others.

only streaming mode can benefit from buffering messages like this.
only 1 thread at a time is allowed to do IO, other threads put events in the queue and return to the thread pool.

a thread initially calls enqueueEvent and stay there until there are no more events in the queue, calling the onEVent method in synchronized context for each Event.

on IOE in onEvent we terminate.
we have a limit, to keep memory usage under control.

if queue limit is reached onQueueFull is called, and then we terminate.


whats not optimal is that a worker thread is sticky to the client depending upon available events in the handlers local queue, that can in theory allow a few clients to block all threads for extended time.
that effect can make this implementation unusable depending on the scenario, its not a perfect design be any means.
The potential improvement is that only 1 worker thread is tied up to a client instead of several being blocked by synchronized io wait for one CometHandler .

Author:
Gustav Trede

Field Summary
protected  E attachment
           
protected static Logger logger
           
protected  Queue<CometEvent> messageQueue
          todo replace with non array copying list for non resizing add situations, using internal index to keep track of state , not a linked list, it has too much overhead and eats memory.
 
Constructor Summary
DefaultConcurrentCometHandler()
           
DefaultConcurrentCometHandler(int messageQueueLimit)
           
 
Method Summary
 void attach(E attachment)
          Attach an intance of E to this class.
 E attachment()
          returns the attachment
 void enqueueEvent(CometEvent event)
          Queues event if another thread is currently working on this handler.
 void onInterrupt(CometEvent event)
          Receive CometEvent notification when the underlying tcp communication is resumed by Grizzly.
 void onQueueFull(CometEvent event)
          called in synchronized context.
 void onTerminate(CometEvent event)
          Receive CometEvent notification when the response is resumed by a CometHandler or by the CometContext
protected  void terminate()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sun.grizzly.comet.CometHandler
onEvent, onInitialize
 

Field Detail

logger

protected static final Logger logger

messageQueue

protected final Queue<CometEvent> messageQueue
todo replace with non array copying list for non resizing add situations, using internal index to keep track of state , not a linked list, it has too much overhead and eats memory.


attachment

protected E attachment
Constructor Detail

DefaultConcurrentCometHandler

public DefaultConcurrentCometHandler()

DefaultConcurrentCometHandler

public DefaultConcurrentCometHandler(int messageQueueLimit)
Parameters:
messageQueueLimit -
Method Detail

enqueueEvent

public void enqueueEvent(CometEvent event)
Queues event if another thread is currently working on this handler. The first thread to start working will keep doing so until there are no further events in the internal queue.


onQueueFull

public void onQueueFull(CometEvent event)
called in synchronized context. when the comethandler's local event queue is full.
default impl resumes the comethandler

Parameters:
event - CometEvent

attachment

public E attachment()
returns the attachment


attach

public void attach(E attachment)
Description copied from interface: CometHandler
Attach an intance of E to this class.

Specified by:
attach in interface CometHandler<E>

onInterrupt

public void onInterrupt(CometEvent event)
                 throws IOException
Receive CometEvent notification when the underlying tcp communication is resumed by Grizzly. This happens when the CometContext.setExpirationDelay(long) expires or when the remote client close the connection.
default impl calls terminate()

Specified by:
onInterrupt in interface CometHandler<E>
Throws:
IOException

onTerminate

public void onTerminate(CometEvent event)
                 throws IOException
Receive CometEvent notification when the response is resumed by a CometHandler or by the CometContext
default impl calls terminate()

Specified by:
onTerminate in interface CometHandler<E>
Throws:
IOException

terminate

protected void terminate()


Copyright © 2013 Oracle Corporation. All Rights Reserved.