Package eu.bandm.tools.message
Class MessageAsync<M extends Message>
java.lang.Object
eu.bandm.tools.message.SingleSender<M>
eu.bandm.tools.message.MessageAsync<M>
- All Implemented Interfaces:
MessageReceiver<M>
,AutoCloseable
public class MessageAsync<M extends Message>
extends SingleSender<M>
implements MessageReceiver<M>, AutoCloseable
A sub-class of
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.receiver
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
.)
-
Field Summary
Modifier and TypeFieldDescription(package private) boolean
(package private) final LinkedBlockingQueue<M>
(package private) boolean
(package private) final Thread
Fields inherited from class eu.bandm.tools.message.SingleSender
receiver
-
Constructor Summary
ConstructorDescriptionAfter calling this constructor, a call toSingleSender.setReceiver(MessageReceiver)
is necessary beforeSingleSender.send(Message)
can be used.MessageAsync
(MessageReceiver<? super M> r) Constructor which sets the receiver. -
Method Summary
Methods inherited from class eu.bandm.tools.message.SingleSender
getReceiver, send, setReceiver
-
Field Details
-
queue
-
interrupted
boolean interrupted -
running
boolean running -
thread
-
-
Constructor Details
-
MessageAsync
public MessageAsync()After calling this constructor, a call toSingleSender.setReceiver(MessageReceiver)
is necessary beforeSingleSender.send(Message)
can be used. Otherwise code will crash intentionally. Cf the documentation ofthe super constructor
. -
MessageAsync
Constructor which sets the receiver.
-
-
Method Details
-
close
public void close()- Specified by:
close
in 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 some kamikaze problem with the producer, since the buffer has a capacity ofInteger.MAX_VALUE
messages.- Specified by:
receive
in interfaceMessageReceiver<M extends Message>
-