public class EventQueue
extends java.lang.Object
Title: Framework Support Library
Description: The Event Queue is a variation on a standard Queue concept.
The event queue is blocking and single-threaded. It relies on synchronization locks
of its enqueue()
and dequeue()
methods to provide a thread-safe
set of operations. Likewise if a listener is set the delivery thread is potentially
the only thread that is removing messages from the queue.
Copyright: Copyright (c) 2009
Company: StreamScape Technologies
Modifier and Type | Field and Description |
---|---|
static DeliveryMode |
DEFAULT_DELIVERY_MODE |
static long |
DEFAULT_DELIVERY_SPIN_WAIT |
static FlowControlMode |
DEFAULT_FLOW_CONTROL_MODE |
static int |
DEFAULT_MAX_DEPTH |
static int |
DEFAULT_WARN_DEPTH |
Constructor and Description |
---|
EventQueue(java.lang.String name)
Constructs a new queue without a listener which will receive delivered objects.
|
EventQueue(java.lang.String name,
EventQueueListener listener)
Constructs a new queue with the specified listener which will receive delivered objects.
|
EventQueue(java.lang.String name,
EventQueueListener listener,
DeliveryMode deliveryMode)
Constructs a new queue with the specified listener which will receive delivered objects.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the queue.
|
void |
close()
Closes the queue.
|
java.lang.Object |
dequeue()
Dequeues an object, expecting an appearance of this object in the queue during unlimited time interval.
|
java.lang.Object |
dequeue(long waitTime)
Dequeues an object, expecting an appearance of this object in the queue during the specified time interval.
|
java.lang.Object |
dequeue(Matcher matcher)
Dequeues an object, expecting an appearance of this object in the queue during unlimited time interval.
|
java.lang.Object |
dequeue(Matcher matcher,
long waitTime)
Dequeues an object, expecting an appearance of this object in the queue during the specified time interval.
|
java.util.List |
dequeueList(int objectsNumber)
Dequeues the specified number of objects, expecting an appearance of these objects in the queue during unlimited time interval.
|
java.util.List |
dequeueList(int objectsNumber,
long waitTime)
Dequeues the specified number of objects, expecting an appearance of these objects in the queue during the specified time interval.
|
java.lang.Object |
dequeueNoWait()
Dequeues an object without any waiting.
|
java.lang.Object |
dequeueNoWait(Matcher matcher)
Dequeues an object, expecting an appearance of this object in the queue during unlimited time interval.
|
java.util.List |
drain()
Dequeues all objects, expecting an appearance at least of one object in the queue during unlimited time interval.
|
java.util.List |
drain(long waitTime)
Dequeues all objects, expecting an appearance at least of one object in the queue during the specified time interval.
|
void |
drainTo(EventQueue otherQueue)
Dequeues all objects to the specified queue.
|
void |
enqueue(java.lang.Object object)
Enqueues the specified object, placing it at a tail of the queue.
|
void |
enqueueWithPriority(java.lang.Object object)
Enqueues the specified object, placing it at a head of the queue.
|
DeliveryMode |
getDeliveryMode()
Returns a delivery mode of the queue.
|
long |
getDeliverySpinWait()
Returns a delay between successive delivery operations.
|
long |
getDeliveryThreadId()
Returns an identifier of the internal thread that delivers objects to the listener.
|
FlowControlMode |
getFlowControlMode()
Returns a flow control mode of the queue.
|
EventQueueListener |
getListener()
Returns a listener associated with the queue.
|
int |
getMaxDepth()
Returns a maximum queue depth.
|
java.lang.String |
getName()
Returns a name of the queue.
|
NotificationListener |
getNotificationListener()
Returns a notification listener associated with the current thread.
|
int |
getWarningThreshold()
Returns a warning threshold of maximum queue depth.
|
boolean |
isEmpty()
Checks if the queue is empty.
|
boolean |
isOpened()
Checks if the queue is opened (can enqueue objects).
|
boolean |
isStarted()
Checks if the queue is started (will deliver queued objects).
|
boolean |
isSuspended()
Checks if the queue is suspended.
|
void |
open()
Opens the queue.
|
java.lang.Object |
peek()
Peeks a first element of the queue.
|
void |
resume()
Resumes the delivery thread of the queue.
|
void |
setDeliveryMode(DeliveryMode deliveryMode)
Sets a delivery mode of the queue and creates a new delivery thread.
|
void |
setDeliverySpinWait(long deliverySpinWait)
Sets a delay between successive delivery operations.
|
void |
setFlowControlMode(FlowControlMode flowControlMode)
Sets a flow control mode of the queue.
|
void |
setListener(EventQueueListener listener)
Sets a listener associated with the queue.
|
void |
setMaxDepth(int maxDepth)
Sets a maximum queue depth.
|
void |
setNotificationListener(NotificationListener notificationListener)
Sets a notification listener associated with the current thread.
|
void |
setWarningThreshold(double maxDepthPercent)
Sets a warning threshold in percents of maximum queue depth.
|
int |
size()
Return a size of the queue.
|
void |
start()
Starts the queue if the associated listener has been set before.
|
void |
stop()
Stops the delivery thread of the queue.
|
void |
stopDelayed(long timeout)
Performs a delayed stop of the delivery thread of the queue.
|
void |
suspend()
Suspends the delivery thread of the event queue.
|
public static final int DEFAULT_MAX_DEPTH
public static final int DEFAULT_WARN_DEPTH
public static final FlowControlMode DEFAULT_FLOW_CONTROL_MODE
public static final DeliveryMode DEFAULT_DELIVERY_MODE
public static final long DEFAULT_DELIVERY_SPIN_WAIT
public EventQueue(java.lang.String name)
name
- the name of the queue.public EventQueue(java.lang.String name, EventQueueListener listener)
name
- the name of the queue.listener
- the listener associated with the queue.public EventQueue(java.lang.String name, EventQueueListener listener, DeliveryMode deliveryMode)
name
- the name of the queue.listener
- the listener associated with the queue.deliveryMode
- the delivery mode of the queue.public java.lang.String getName()
public EventQueueListener getListener()
public void setListener(EventQueueListener listener) throws IllegalQueueStateException
listener
- the associated listener to be set.IllegalQueueStateException
- if the queue is started.public NotificationListener getNotificationListener()
public void setNotificationListener(NotificationListener notificationListener)
notificationListener
- the associated notification listener to be set.public int getMaxDepth()
public void setMaxDepth(int maxDepth)
enqueue
will lead to
throwing of an exception until the condition clears. The condition may be cleared by consuming messages from the queue.
The parameter is taken into account only if it has positive value.maxDepth
- the maximum queue depth to be set.public int getWarningThreshold()
public void setWarningThreshold(double maxDepthPercent)
maxDepthPercent
- the percent of the maximum queue depth.public FlowControlMode getFlowControlMode()
public void setFlowControlMode(FlowControlMode flowControlMode)
flowControlMode
- the queue flow control mode.public DeliveryMode getDeliveryMode()
public void setDeliveryMode(DeliveryMode deliveryMode) throws IllegalQueueStateException
deliveryMode
- the delivery mode to be set.IllegalQueueStateException
- if the queue is started.public long getDeliverySpinWait()
public void setDeliverySpinWait(long deliverySpinWait)
DeliveryMode
it is recommended this number be increased and the associate queue size be increased to accommodate a backlog of events.
For higher latency it is recommended the value be lowered.
1
milliseconds.deliverySpinWait
- the delivery delay.public boolean isOpened()
true
if the queue is opened, false otherwise.
public void open()
public void close()
dequeue
) to be released.
enqueue
will lead to throwing of the exception.public boolean isStarted()
true
if the queue is started, false otherwise.
public void start()
public void stop()
public void stopDelayed(long timeout)
timeout
- the wait time (in milliseconds) for processing of all objects in the queue before stopping the delivery thread.public boolean isSuspended()
true
if the queue is suspended, false otherwise.
public void suspend()
enqueue
.public void resume()
public long getDeliveryThreadId()
-1
.-1
if this thread is not available.public void enqueue(java.lang.Object object) throws EventQueueException
IllegalQueueStateException exception if the queue is not opened.
object
- the object to be enqueued.EventQueueException
- if the queue is not opened or the maximum depth is reached.public void enqueueWithPriority(java.lang.Object object) throws EventQueueException
object
- the object to be enqueued.EventQueueException
- if the queue is not opened or the maximum depth is reached.public java.lang.Object dequeue() throws EventQueueInterruptException
EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.lang.Object dequeue(Matcher matcher) throws EventQueueInterruptException
matcher
- the specified matcher.EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.util.List dequeueList(int objectsNumber) throws EventQueueInterruptException
objectsNumber
- the specified number of objects that will be dequeued.EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.lang.Object dequeue(long waitTime) throws EventQueueInterruptException
waitTime
- the specified waiting time interval.null
if the time interval is elapsed.EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.lang.Object dequeue(Matcher matcher, long waitTime) throws EventQueueInterruptException
matcher
- the specified matcher.waitTime
- the specified waiting time interval.null
if the time interval is elapsed.EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.util.List dequeueList(int objectsNumber, long waitTime) throws EventQueueInterruptException
objectsNumber
- the specified number of objects that will be dequeued.waitTime
- the specified waiting time interval.EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.lang.Object dequeueNoWait()
null
if no objects are in the queue.public java.lang.Object dequeueNoWait(Matcher matcher)
matcher
- the specified matcher.null
if no corresponding objects are in the queue.public java.util.List drain() throws EventQueueInterruptException
EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public java.util.List drain(long waitTime) throws EventQueueInterruptException
waitTime
- the specified waiting time interval.EventQueueInterruptException
- if a waiting of new object in the queue is interrupted by other thread.public void drainTo(EventQueue otherQueue) throws EventQueueException
otherQueue
- the queue to which all objects will be dequeued.EventQueueException
- if the specified queue has not enough space.public java.lang.Object peek() throws EventQueueException
null
if the queue is empty.EventQueueException
public int size()
public boolean isEmpty()
true
if the queue is empty, false
otherwise.public void clear()
Copyright © 2015-2024 StreamScape Technologies. All rights reserved.