Class DefaultConcurrentCometHandler<E>
java.lang.Object
org.glassfish.grizzly.comet.concurrent.DefaultConcurrentCometHandler<E>
- All Implemented Interfaces:
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 .
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
FieldsModifier and TypeFieldDescriptionprotected final Queue<CometEvent>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
ConstructorsConstructorDescriptionDefaultConcurrentCometHandler(int messageQueueLimit) DefaultConcurrentCometHandler(CometContext<E> context, org.glassfish.grizzly.http.server.Response response) -
Method Summary
Modifier and TypeMethodDescriptionvoidenqueueEvent(CometEvent event) Queues event if another thread is currently working on this handler.org.glassfish.grizzly.http.server.ResponsevoidonInterrupt(CometEvent event) ReceiveCometEventnotification when the underlying tcp communication is resumed by Grizzly.voidonQueueFull(CometEvent event) Called in synchronized context when the comet handler's local event queue is full.
default impl resumes the comet handlervoidonTerminate(CometEvent event) ReceiveCometEventnotification when the response is resumed by aCometHandleror by theCometContextvoidsetCometContext(CometContext<E> context) voidsetResponse(org.glassfish.grizzly.http.server.Response response) protected voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.glassfish.grizzly.comet.CometHandler
onEvent, onInitialize
-
Field Details
-
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 Details
-
DefaultConcurrentCometHandler
public DefaultConcurrentCometHandler(CometContext<E> context, org.glassfish.grizzly.http.server.Response response) -
DefaultConcurrentCometHandler
public DefaultConcurrentCometHandler(int messageQueueLimit) - Parameters:
messageQueueLimit-
-
-
Method Details
-
getCometContext
- Specified by:
getCometContextin interfaceCometHandler<E>
-
setCometContext
- Specified by:
setCometContextin interfaceCometHandler<E>
-
getResponse
public org.glassfish.grizzly.http.server.Response getResponse()- Specified by:
getResponsein interfaceCometHandler<E>- Returns:
- the response associated with the handler.
-
setResponse
public void setResponse(org.glassfish.grizzly.http.server.Response response) - Specified by:
setResponsein interfaceCometHandler<E>
-
enqueueEvent
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.- Throws:
IOException
-
onQueueFull
Called in synchronized context when the comet handler's local event queue is full.
default impl resumes the comet handler- Parameters:
event-CometEvent- Throws:
IOException
-
onInterrupt
ReceiveCometEventnotification when the underlying tcp communication is resumed by Grizzly. This happens when theCometContext.setExpirationDelay(long)expires or when the remote client close the connection.
default impl calls terminate()- Specified by:
onInterruptin interfaceCometHandler<E>- Throws:
IOException
-
onTerminate
ReceiveCometEventnotification when the response is resumed by aCometHandleror by theCometContext
default impl calls terminate()- Specified by:
onTerminatein interfaceCometHandler<E>- Throws:
IOException
-
terminate
protected void terminate()
-