Top Description Fields Constructors Methods
org.glassfish.grizzly.comet.concurrent

public abstract Class DefaultConcurrentCometHandler<E>

extends Object
implements CometHandler<E>
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.comet.CometHandler
Imports
java.io.IOException, java.util.LinkedList, .Queue, java.util.logging.Logger, org.glassfish.grizzly.comet.CometContext, .CometEvent, .CometHandler, org.glassfish.grizzly.http.server.Response

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

Modifier and TypeField and Description
private CometContext<E>
protected static final Logger
protected final Queue<CometEvent<E>>
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.

private final int
messageQueueLimit

max number of events to locally queue for this CometHandler.

private int
queueSize

current queue size

private boolean
readyForWork

true means that no thread is currently active on this comet handlers queue logic

private Response
private boolean
shuttingDown

used for preventing the worker threads from the executor event queue from adding events to the comet handlers local queue or starting IO logic after shut down.

Constructor Summary

AccessConstructor and Description
public
DefaultConcurrentCometHandler(final CometContext<E> context, final Response response)

public
DefaultConcurrentCometHandler(int messageQueueLimit)

Method Summary

Modifier and TypeMethod and Description
public void
enqueueEvent(CometEvent<E> event)

Queues event if another thread is currently working on this handler.

public CometContext<E>
public Response
public void
onInterrupt(CometEvent<E> event)

Implements org.glassfish.grizzly.comet.CometHandler.onInterrupt.

Receive CometEvent notification when the underlying tcp communication is resumed by Grizzly.

public void
onQueueFull(CometEvent<E> event)

Called in synchronized context when the comet handler's local event queue is full.

public void
onTerminate(CometEvent<E> event)

Implements org.glassfish.grizzly.comet.CometHandler.onTerminate.

Receive CometEvent notification when the response is resumed by a CometHandler or by the CometContext default impl calls terminate()

public void
public void
protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contextback to summary
private CometContext<E> context
loggerback to summary
protected static final Logger logger
messageQueueback to summary
protected final Queue<CometEvent<E>> 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.

messageQueueLimitback to summary
private final int messageQueueLimit

max number of events to locally queue for this CometHandler.
(a global event queue normally exists in form of a thread pool too)

queueSizeback to summary
private int queueSize

current queue size

readyForWorkback to summary
private boolean readyForWork

true means that no thread is currently active on this comet handlers queue logic

responseback to summary
private Response response
shuttingDownback to summary
private boolean shuttingDown

used for preventing the worker threads from the executor event queue from adding events to the comet handlers local queue or starting IO logic after shut down.

Constructor Detail

DefaultConcurrentCometHandlerback to summary
public DefaultConcurrentCometHandler(final CometContext<E> context, final Response response)
DefaultConcurrentCometHandlerback to summary
public DefaultConcurrentCometHandler(int messageQueueLimit)

Method Detail

enqueueEventback to summary
public void enqueueEvent(CometEvent<E> event) throws IOException

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.

getCometContextback to summary
public CometContext<E> getCometContext()

Implements org.glassfish.grizzly.comet.CometHandler.getCometContext.

Annotations
@Override
getResponseback to summary
public Response getResponse()

Implements org.glassfish.grizzly.comet.CometHandler.getResponse.

Annotations
@Override
onInterruptback to summary
public void onInterrupt(CometEvent<E> event) throws IOException

Implements org.glassfish.grizzly.comet.CometHandler.onInterrupt.

Doc from org.glassfish.grizzly.comet.CometHandler.onInterrupt.

Receive CometEvent notification when the underlying tcp communication is resumed by Grizzly. This happens when the CometContext#setExpirationDelay expires or when the remote client close the connection.
default impl calls terminate()

Annotations
@Override
onQueueFullback to summary
public void onQueueFull(CometEvent<E> event) throws IOException

Called in synchronized context when the comet handler's local event queue is full.
default impl resumes the comet handler

Parameters
event:CometEvent<E>

CometEvent

onTerminateback to summary
public void onTerminate(CometEvent<E> event) throws IOException

Implements org.glassfish.grizzly.comet.CometHandler.onTerminate.

Doc from org.glassfish.grizzly.comet.CometHandler.onTerminate.

Receive CometEvent notification when the response is resumed by a CometHandler or by the CometContext
default impl calls terminate()

Annotations
@Override
setCometContextback to summary
public void setCometContext(final CometContext<E> context)

Implements org.glassfish.grizzly.comet.CometHandler.setCometContext.

Annotations
@Override
setResponseback to summary
public void setResponse(final Response response)

Implements org.glassfish.grizzly.comet.CometHandler.setResponse.

Annotations
@Override
terminateback to summary
protected void terminate()