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
-
Field Summary
Fields inherited from class org.pipservices3.messaging.queues.MessageQueue
_capabilities, _connectionResolver, _counters, _credentialResolver, _kind, _lock, _logger, _name
-
Constructor Summary
Constructors Constructor Description MemoryMessageQueue()
Creates a new instance of the message queue.MemoryMessageQueue(String name)
Creates a new instance of the message queue. -
Method Summary
Modifier and Type Method Description void
abandon(MessageEnvelop message)
Returnes message into the queue and makes it available for all subscribers to receive it again.void
clear(String correlationId)
Clears component state.void
close(String correlationId)
Closes component and frees used resources.void
complete(MessageEnvelop message)
Permanently removes a message from the queue.void
endListen(String correlationId)
Ends listening for incoming messages.Long
getMessageCount()
Gets the current number of messages in the queue to be delivered.boolean
isOpen()
Checks if the component is opened.void
listen(String correlationId, IMessageReceiver receiver)
Listens for incoming messages and blocks the current thread until queue is closed.void
moveToDeadLetter(MessageEnvelop message)
Permanently removes a message from the queue and sends it to dead letter queue.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.MessageEnvelop
peek(String correlationId)
Peeks a single incoming message from the queue without removing it.List<MessageEnvelop>
peekBatch(String correlationId, int messageCount)
Peeks multiple incoming messages from the queue without removing them.MessageEnvelop
receive(String correlationId, long waitTimeout)
Receives an incoming message and removes it from the queue.void
renewLock(MessageEnvelop message, long lockTimeout)
Renews a lock on a message that makes it invisible from other receivers in the queue.void
send(String correlationId, MessageEnvelop message)
Sends a message into the queue.String
toString()
Override toString() method, string representation of queue.Methods inherited from class org.pipservices3.messaging.queues.MessageQueue
beginListen, configure, getCapabilities, getName, open, sendAsObject, setReferences
-
Constructor Details
-
MemoryMessageQueue
public MemoryMessageQueue()Creates a new instance of the message queue. -
MemoryMessageQueue
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 interfaceorg.pipservices3.commons.run.IOpenable
- Specified by:
isOpen
in classMessageQueue
- 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 classMessageQueue
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.connection
- connection parameterscredential
- credential parameters
-
close
Closes component and frees used resources.- Specified by:
close
in interfaceorg.pipservices3.commons.run.IClosable
- Specified by:
close
in classMessageQueue
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.
-
clear
Clears component state.- Specified by:
clear
in classMessageQueue
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.
-
getMessageCount
Gets the current number of messages in the queue to be delivered.- Specified by:
getMessageCount
in interfaceIMessageQueue
- Specified by:
getMessageCount
in classMessageQueue
- Returns:
- number of messages.
-
send
Sends a message into the queue.- Specified by:
send
in interfaceIMessageQueue
- Specified by:
send
in classMessageQueue
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.message
- a message envelop to be sent.
-
peek
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 interfaceIMessageQueue
- Specified by:
peek
in classMessageQueue
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.- Returns:
- a message envelop object.
-
peekBatch
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 interfaceIMessageQueue
- Specified by:
peekBatch
in classMessageQueue
- 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
Receives an incoming message and removes it from the queue.- Specified by:
receive
in interfaceIMessageQueue
- Specified by:
receive
in classMessageQueue
- 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
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 interfaceIMessageQueue
- Specified by:
renewLock
in classMessageQueue
- Parameters:
message
- a message to extend its lock.lockTimeout
- a locking timeout in milliseconds.
-
abandon
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 interfaceIMessageQueue
- Specified by:
abandon
in classMessageQueue
- Parameters:
message
- a message to return.
-
complete
Permanently removes a message from the queue. This method is usually used to remove the message after successful processing.- Specified by:
complete
in interfaceIMessageQueue
- Specified by:
complete
in classMessageQueue
- Parameters:
message
- a message to remove.
-
moveToDeadLetter
Permanently removes a message from the queue and sends it to dead letter queue.- Specified by:
moveToDeadLetter
in interfaceIMessageQueue
- Specified by:
moveToDeadLetter
in classMessageQueue
- Parameters:
message
- a message to be removed.
-
listen
Listens for incoming messages and blocks the current thread until queue is closed.- Specified by:
listen
in interfaceIMessageQueue
- Specified by:
listen
in classMessageQueue
- 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
Ends listening for incoming messages. When this method is call listen() unblocks the thread and execution continues.- Specified by:
endListen
in interfaceIMessageQueue
- Specified by:
endListen
in classMessageQueue
- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.
-
toString
Override toString() method, string representation of queue.- Overrides:
toString
in classMessageQueue
- Returns:
- queue name
-