Class MemoryMessageQueue

java.lang.Object
org.pipservices3.messaging.queues.MessageQueue
org.pipservices3.messaging.queues.MemoryMessageQueue
All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.refer.IReferenceable, org.pipservices3.commons.run.IClosable, org.pipservices3.commons.run.IOpenable, IMessageQueue

public class MemoryMessageQueue
extends MessageQueue
Message queue that sends and receives messages within the same process by using shared memory.

This queue is typically used for testing to mock real queues.

### Configuration parameters ###

  • name: name of the message queue

### References ###

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements

### Example ###

 
 MessageQueue queue = new MessageQueue("myqueue");
 
 queue.send("123", new MessageEnvelop(null, "mymessage", "ABC"));
 
 queue.receive("123", 0);
 
 
See Also:
MessageQueue, MessagingCapabilities
  • Constructor Details

    • MemoryMessageQueue

      public MemoryMessageQueue()
      Creates a new instance of the message queue.
    • MemoryMessageQueue

      public MemoryMessageQueue​(String name)
      Creates a new instance of the message queue.
      Parameters:
      name - (optional) a queue name.
      See Also:
      MessagingCapabilities
  • Method Details

    • isOpen

      public boolean isOpen()
      Checks if the component is opened.
      Specified by:
      isOpen in interface org.pipservices3.commons.run.IOpenable
      Specified by:
      isOpen in class MessageQueue
      Returns:
      true if the component has been opened and false otherwise.
    • open

      public void open​(String correlationId, org.pipservices3.components.connect.ConnectionParams connection, org.pipservices3.components.auth.CredentialParams credential)
      Opens the component with given connection and credential parameters.
      Specified by:
      open in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      connection - connection parameters
      credential - credential parameters
    • close

      public void close​(String correlationId)
      Closes component and frees used resources.
      Specified by:
      close in interface org.pipservices3.commons.run.IClosable
      Specified by:
      close in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
    • clear

      public void clear​(String correlationId)
      Clears component state.
      Specified by:
      clear in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
    • getMessageCount

      public Long getMessageCount()
      Gets the current number of messages in the queue to be delivered.
      Specified by:
      getMessageCount in interface IMessageQueue
      Specified by:
      getMessageCount in class MessageQueue
      Returns:
      number of messages.
    • send

      public void send​(String correlationId, MessageEnvelop message)
      Sends a message into the queue.
      Specified by:
      send in interface IMessageQueue
      Specified by:
      send in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      message - a message envelop to be sent.
    • peek

      public MessageEnvelop peek​(String correlationId)
      Peeks a single incoming message from the queue without removing it. If there are no messages available in the queue it returns null.
      Specified by:
      peek in interface IMessageQueue
      Specified by:
      peek in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Returns:
      a message envelop object.
    • peekBatch

      public List<MessageEnvelop> peekBatch​(String correlationId, int messageCount)
      Peeks multiple incoming messages from the queue without removing them. If there are no messages available in the queue it returns an empty list.
      Specified by:
      peekBatch in interface IMessageQueue
      Specified by:
      peekBatch in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      messageCount - a maximum number of messages to peek.
      Returns:
      a list with messages.
    • receive

      public MessageEnvelop receive​(String correlationId, long waitTimeout)
      Receives an incoming message and removes it from the queue.
      Specified by:
      receive in interface IMessageQueue
      Specified by:
      receive in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      waitTimeout - a timeout in milliseconds to wait for a message to come.
      Returns:
      a message envelop object.
    • renewLock

      public void renewLock​(MessageEnvelop message, long lockTimeout)
      Renews a lock on a message that makes it invisible from other receivers in the queue. This method is usually used to extend the message processing time.
      Specified by:
      renewLock in interface IMessageQueue
      Specified by:
      renewLock in class MessageQueue
      Parameters:
      message - a message to extend its lock.
      lockTimeout - a locking timeout in milliseconds.
    • abandon

      public void abandon​(MessageEnvelop message)
      Returnes message into the queue and makes it available for all subscribers to receive it again. This method is usually used to return a message which could not be processed at the moment to repeat the attempt. Messages that cause unrecoverable errors shall be removed permanently or/and send to dead letter queue.
      Specified by:
      abandon in interface IMessageQueue
      Specified by:
      abandon in class MessageQueue
      Parameters:
      message - a message to return.
    • complete

      public void complete​(MessageEnvelop message)
      Permanently removes a message from the queue. This method is usually used to remove the message after successful processing.
      Specified by:
      complete in interface IMessageQueue
      Specified by:
      complete in class MessageQueue
      Parameters:
      message - a message to remove.
    • moveToDeadLetter

      public void moveToDeadLetter​(MessageEnvelop message)
      Permanently removes a message from the queue and sends it to dead letter queue.
      Specified by:
      moveToDeadLetter in interface IMessageQueue
      Specified by:
      moveToDeadLetter in class MessageQueue
      Parameters:
      message - a message to be removed.
    • listen

      public void listen​(String correlationId, IMessageReceiver receiver)
      Listens for incoming messages and blocks the current thread until queue is closed.
      Specified by:
      listen in interface IMessageQueue
      Specified by:
      listen in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      receiver - a receiver to receive incoming messages.
      See Also:
      IMessageReceiver, receive(String, long)
    • endListen

      public void endListen​(String correlationId)
      Ends listening for incoming messages. When this method is call listen() unblocks the thread and execution continues.
      Specified by:
      endListen in interface IMessageQueue
      Specified by:
      endListen in class MessageQueue
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
    • toString

      public String toString()
      Override toString() method, string representation of queue.
      Overrides:
      toString in class MessageQueue
      Returns:
      queue name