Class MessageAsync<M extends Message>

Type Parameters:
M - the type of the messages
All Implemented Interfaces:
MessageReceiver<M>, AutoCloseable

public class MessageAsync<M extends Message> extends MessagePasser<M> implements AutoCloseable
A subclass 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.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 Details

    • queue

      final LinkedBlockingQueue<M extends Message> queue
      Buffer for the written messages, waiting to be delivered asynchronouously to the drain.
    • interrupted

      boolean interrupted
      Flag which reflects the state of the thread.
    • running

      boolean running
      Flag which reflects the state of the thread.
    • thread

      final Thread thread
      Thread which reads the buffer queue and sends it contents asynchronouously to the drain SingleSender.drain.
  • Constructor Details

  • Method Details

    • close

      public void close()
      Interrupt the consuming thread to process all remaining messages, and forbid further receives by this instance.
      Specified by:
      close in interface AutoCloseable
    • receive

      public void receive(M m)
      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_VALUE messages.

      Specified by:
      receive in interface MessageReceiver<M extends Message>
      Overrides:
      receive in class MessagePasser<M extends Message>
      Parameters:
      m - the message to propagate
      Throws:
      IllegalStateException - when this instance has been closed before.