Class MessageAsync<M extends Message>
- Type Parameters:
M- the type of the messages
- All Implemented Interfaces:
MessageReceiver<M>,AutoCloseable
SingleSender which lets the drain's receive method
run asynchronouously in a separate thread.
All received messages are stored internally in a buffer,
and the MessageReceiver.receive(Message) method of the currently set
SingleSender.drain
is executed in a loop in an own, specially constructed
Thread, consuming all these messages asynchronouously.
The caller of the here defined receive(Message) thus returns
immediately, after writing the message into a buffer object.
(This is currently a LinkedBlockingQueue.)
Once an instance of this class has been closed, sending to it is no longer possible.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) booleanFlag which reflects the state of the thread.(package private) LinkedBlockingQueue<M> Buffer for the written messages, waiting to be delivered asynchronouously to the drain.(package private) booleanFlag which reflects the state of the thread.(package private) final ThreadThread which reads the bufferqueueand sends it contents asynchronouously to the drainSingleSender.drain.Fields inherited from class eu.bandm.tools.message.SingleSender
drain -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an instance with a queue of practically unlimited capacity.MessageAsync(int capacity) Constructor which sets the capacity of the employed queue.MessageAsync(MessageReceiver<? super M> r) Construct an instance with a queue of practically unlimited capacity and set its drain.MessageAsync(MessageReceiver<? super M> r, int capacity) Constructor which sets the capacity of the employed queue and the drain. -
Method Summary
Methods inherited from class eu.bandm.tools.message.SingleSender
getDrain, send, setDrainMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface eu.bandm.tools.message.MessageReceiver
ifSubcriticalThen, ifSubcriticalThen, ifSubcriticalThenElse
-
Field Details
-
queue
LinkedBlockingQueue<M extends Message> queueBuffer for the written messages, waiting to be delivered asynchronouously to the drain. -
interrupted
boolean interruptedFlag which reflects the state of the thread. -
running
boolean runningFlag which reflects the state of the thread. -
thread
Thread which reads the bufferqueueand sends it contents asynchronouously to the drainSingleSender.drain.
-
-
Constructor Details
-
MessageAsync
public MessageAsync(int capacity) Constructor which sets the capacity of the employed queue. After calling this constructor, a call toSingleSender.setDrain(MessageReceiver)is necessary beforeSingleSender.send(Message)can be used. Otherwise code will crash intentionally.- Parameters:
capacity- of the internally used queue- See Also:
-
MessageAsync
Constructor which sets the capacity of the employed queue and the drain.- Parameters:
r- the drain.capacity- of the internally used queue
-
MessageAsync
public MessageAsync()Construct an instance with a queue of practically unlimited capacity. After calling this constructor, a call toSingleSender.setDrain(MessageReceiver)is necessary beforeSingleSender.send(Message)can be used. Otherwise code will crash intentionally.- See Also:
-
MessageAsync
Construct an instance with a queue of practically unlimited capacity and set its drain.- Parameters:
r- the drain.- See Also:
-
-
Method Details
-
close
public void close()Interrupt the consuming thread to process all remaining messages, and forbid further receives by this instance.- Specified by:
closein interfaceAutoCloseable
-
receive
Returns immediately after storing the message to the internal buffer.Will block whenever this buffer is full. Normally this should never happen, since the consumer thread should not starve under normal scheduling discipline.
Overrun is probably caused by kamikaze behaviour of the producer(s), since the buffer has a capacity of
Integer.MAX_VALUEmessages.- Specified by:
receivein interfaceMessageReceiver<M extends Message>- Overrides:
receivein classMessagePasser<M extends Message>- Parameters:
m- the message to propagate- Throws:
IllegalStateException- when this instance has beenclosed before.
-