Package adams.flow.control
Class StorageQueueHandler
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.flow.control.StorageQueueHandler
-
- All Implemented Interfaces:
LoggingSupporter
,SizeOfHandler
,Serializable
public class StorageQueueHandler extends LoggingObject
Wrapper class around aLinkedBlockingDeque
object stored in internal storage. Allows to limit the queue size and the specification of a logging actor. It is possible to define retrieval timestamps for queue items, i.e., only after the timestamp passes is the retrieval possible ("delayed retrieval"). The underlying assumption with delayed retrieveal is that items added to the queue at a later stage will have increasing retrieval timestamps (they queue items don't get reordered based on that timestamp).- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
LogEntry
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StorageQueueHandler.QueueItem
Container for queueing objects.
-
Field Summary
Fields Modifier and Type Field Description protected int
m_Limit
the limit for the queue (-1 is unlimited).protected Actor
m_Logging
the (optional) logging actor.protected Actor
m_Monitoring
the (optional) monitoring actor.protected String
m_Name
the name of the queue.protected LinkedBlockingDeque<StorageQueueHandler.QueueItem>
m_Queue
the queue itself.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description StorageQueueHandler(String name)
Initializes the limitless queue with no logging.StorageQueueHandler(String name, int limit, Actor logging, Actor monitoring)
Initializes the queue with the specified limit and logging.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(Object obj)
Adds the object to the queue.boolean
addDelayedAt(Object obj, long retrievalAt)
Adds the object to the queue with the specified timestamp from which to allow retrieval.boolean
addDelayedBy(Object obj, long delay)
Adds the object to the queue with the specified retrieval delay.boolean
canRemove()
Checks whether it is possible to dequeue an item.int
getLimit()
Returns the limit of the queue.Actor
getLogging()
Returns the logging actor.Actor
getMonitoring()
Returns the monitoring actor.String
getname()
Returns the name of the queue.boolean
hasLogging()
Checks whether a logging actor is available.boolean
hasMonitoring()
Checks whether a monitoring actor is available.protected void
log(LogEntry log, Actor actor)
Logs the log entry with the specified actor.protected void
logError(String msg, Object obj)
Creates a log entry and sends it to the logging actor.protected void
logMonitor(String type, Object obj)
Creates a log entry and sends it to the monitoring actor.Object
poll(long timeout, TimeUnit unit)
Polls the queue in a blocking fashion.Object
remove()
Removes the first element from the queue and returns it.void
setLoggingLevel(LoggingLevel value)
Sets the logging level.int
size()
Returns the size of the queue.String
toString()
Returns a short description of the queue.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
Field Detail
-
m_Name
protected String m_Name
the name of the queue.
-
m_Queue
protected LinkedBlockingDeque<StorageQueueHandler.QueueItem> m_Queue
the queue itself.
-
m_Limit
protected int m_Limit
the limit for the queue (-1 is unlimited).
-
m_Logging
protected Actor m_Logging
the (optional) logging actor.
-
m_Monitoring
protected Actor m_Monitoring
the (optional) monitoring actor.
-
-
Constructor Detail
-
StorageQueueHandler
public StorageQueueHandler(String name)
Initializes the limitless queue with no logging.- Parameters:
name
- the name of the queue
-
-
Method Detail
-
setLoggingLevel
public void setLoggingLevel(LoggingLevel value)
Sets the logging level.- Parameters:
value
- the level
-
getname
public String getname()
Returns the name of the queue.- Returns:
- the name
-
getLimit
public int getLimit()
Returns the limit of the queue.- Returns:
- the limit
-
hasLogging
public boolean hasLogging()
Checks whether a logging actor is available.- Returns:
- true if available
-
getLogging
public Actor getLogging()
Returns the logging actor.- Returns:
- the actor, null if not available
-
hasMonitoring
public boolean hasMonitoring()
Checks whether a monitoring actor is available.- Returns:
- true if available
-
getMonitoring
public Actor getMonitoring()
Returns the monitoring actor.- Returns:
- the actor, null if not available
-
log
protected void log(LogEntry log, Actor actor)
Logs the log entry with the specified actor.- Parameters:
log
- the log entry to sendactor
- the logging actor to send the LogEntry to
-
logError
protected void logError(String msg, Object obj)
Creates a log entry and sends it to the logging actor.- Parameters:
msg
- the msgobj
- the object
-
logMonitor
protected void logMonitor(String type, Object obj)
Creates a log entry and sends it to the monitoring actor.- Parameters:
type
- the monitoring event type (Add or Remove)obj
- the object to add, null in case of remove
-
add
public boolean add(Object obj)
Adds the object to the queue.- Parameters:
obj
- the object to add- Returns:
- true if successfully added
-
addDelayedBy
public boolean addDelayedBy(Object obj, long delay)
Adds the object to the queue with the specified retrieval delay.- Parameters:
obj
- the object to adddelay
- the retrieval delay to use for the object- Returns:
- true if successfully added
-
addDelayedAt
public boolean addDelayedAt(Object obj, long retrievalAt)
Adds the object to the queue with the specified timestamp from which to allow retrieval.- Parameters:
obj
- the object to addretrievalAt
- the retrieval timestamp to associate with the object, 0 for immediate retrieval- Returns:
- true if successfully added
-
canRemove
public boolean canRemove()
Checks whether it is possible to dequeue an item.- Returns:
- true if object can be dequeued
-
remove
public Object remove()
Removes the first element from the queue and returns it.- Returns:
- the first element from the queue
-
size
public int size()
Returns the size of the queue.- Returns:
- the size
-
poll
public Object poll(long timeout, TimeUnit unit) throws InterruptedException
Polls the queue in a blocking fashion.- Parameters:
timeout
- the timeout for the pollunit
- the time unit- Returns:
- the value from the queue, null if timeout or not allowed to retrieve yet
- Throws:
InterruptedException
- if interrupted
-
-