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 voidabandon(MessageEnvelop message)Returnes message into the queue and makes it available for all subscribers to receive it again.voidclear(String correlationId)Clears component state.voidclose(String correlationId)Closes component and frees used resources.voidcomplete(MessageEnvelop message)Permanently removes a message from the queue.voidendListen(String correlationId)Ends listening for incoming messages.LonggetMessageCount()Gets the current number of messages in the queue to be delivered.booleanisOpen()Checks if the component is opened.voidlisten(String correlationId, IMessageReceiver receiver)Listens for incoming messages and blocks the current thread until queue is closed.voidmoveToDeadLetter(MessageEnvelop message)Permanently removes a message from the queue and sends it to dead letter queue.voidopen(String correlationId, org.pipservices3.components.connect.ConnectionParams connection, org.pipservices3.components.auth.CredentialParams credential)Opens the component with given connection and credential parameters.MessageEnveloppeek(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.MessageEnvelopreceive(String correlationId, long waitTimeout)Receives an incoming message and removes it from the queue.voidrenewLock(MessageEnvelop message, long lockTimeout)Renews a lock on a message that makes it invisible from other receivers in the queue.voidsend(String correlationId, MessageEnvelop message)Sends a message into the queue.StringtoString()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:
isOpenin interfaceorg.pipservices3.commons.run.IOpenable- Specified by:
isOpenin 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:
openin 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:
closein interfaceorg.pipservices3.commons.run.IClosable- Specified by:
closein classMessageQueue- Parameters:
correlationId- (optional) transaction id to trace execution through call chain.
-
clear
Clears component state.- Specified by:
clearin 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:
getMessageCountin interfaceIMessageQueue- Specified by:
getMessageCountin classMessageQueue- Returns:
- number of messages.
-
send
Sends a message into the queue.- Specified by:
sendin interfaceIMessageQueue- Specified by:
sendin 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:
peekin interfaceIMessageQueue- Specified by:
peekin 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:
peekBatchin interfaceIMessageQueue- Specified by:
peekBatchin 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:
receivein interfaceIMessageQueue- Specified by:
receivein 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:
renewLockin interfaceIMessageQueue- Specified by:
renewLockin 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:
abandonin interfaceIMessageQueue- Specified by:
abandonin 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:
completein interfaceIMessageQueue- Specified by:
completein classMessageQueue- Parameters:
message- a message to remove.
-
moveToDeadLetter
Permanently removes a message from the queue and sends it to dead letter queue.- Specified by:
moveToDeadLetterin interfaceIMessageQueue- Specified by:
moveToDeadLetterin classMessageQueue- Parameters:
message- a message to be removed.
-
listen
Listens for incoming messages and blocks the current thread until queue is closed.- Specified by:
listenin interfaceIMessageQueue- Specified by:
listenin 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:
endListenin interfaceIMessageQueue- Specified by:
endListenin classMessageQueue- Parameters:
correlationId- (optional) transaction id to trace execution through call chain.
-
toString
Override toString() method, string representation of queue.- Overrides:
toStringin classMessageQueue- Returns:
- queue name
-