Package org.javalite.async
Class Async
- java.lang.Object
-
- org.javalite.async.Async
-
public class Async extends Object
Wrapper for embedded Apache ActiveMQ Artemis. It is an embedded in-memory JMS server for asynchronous processing. JavaLite Async can be used in standalone applications, but specifically useful in web apps for processing asynchronous jobs without delaying rendering web responses. It sets many configuration parameters of ArtemisEmbeddedActiveMQto sensible values so you do not have to. This class also implements a Command Pattern for ease of writing asynchronous code.- Author:
- Igor Polevoy on 3/4/15.
-
-
Constructor Summary
Constructors Constructor Description Async(String dataDirectory, boolean useLibAio, com.google.inject.Injector injector, QueueConfig... queueConfigs)Creates and configures a new instance.Async(String dataDirectory, boolean useLibAio, QueueConfig... queueConfigs)Creates and configures a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigureNetty(String host, int port)Call this method once after a constructor in order to create a Netty instance to accept out of VM messages.BatchReceivergetBatchReceiver(String queueName, long timeout)Generate aBatchReceiverto receive and process stored messages.static byte[]getBytes(javax.jms.BytesMessage message)org.apache.activemq.artemis.core.config.ConfigurationgetConfig()Get additional server configuration.longgetMessageCount(String queue)Returns number of messages currently in queueMap<String,Long>getMessageCounts()Returns counts of messages for all queues.List<Command>getTopCommands(int count, String queueName)Returns top commands in queue.List<String>getTopTextMessages(int maxSize, String queueName)Returns topTextMessages in queue.booleanisPaused(String queueName)booleanmoveMessage(String jmsMessageId, String source, String target)Moves a message from one queue to anotherintmoveMessages(String source, String target)Moves all messages from one queue to anothervoidpause(String queueName)Pauses a queue.CommandreceiveCommand(String queueName)Receives a command from a queue synchronously.<T extends Command>
TreceiveCommand(String queueName, int timeout, Class<T> type)Receives a command from a queue synchronously.CommandreceiveCommand(String queueName, long timeout)Receives a command from a queue synchronously.<T extends Command>
TreceiveCommand(String queueName, Class<T> type)Receives a command from a queue synchronously.javax.jms.MessagereceiveMessage(String queueName, long timeout)Receives a messafge from a queue asynchronously.If this queue also has listeners, then messages will be distributed across all consumers.intremoveAllMessages(String queueName)Removes all messages from queue.intremoveMessages(String queueName, String filter)Removes messages from queue.voidresume(String queueName)Resumes a paused queuevoidsend(String queueName, Command command)Sends a command into a queue for processingvoidsend(String queueName, Command command, int deliveryMode)Sends a command into a queue for processingvoidsend(String queueName, Command command, int deliveryMode, int priority, long timeToLive)Sends a command into a queue for processingvoidsend(String queueName, Command command, int deliveryMode, int priority, long timeToLive, long deliveryTime)Sends a command into a queue for processingvoidsend(String queueName, Command command, int deliveryMode, long deliveryTime)Sends a command into a queue for processingvoidsend(String queueName, Command command, Date deliveryTime)Sends a command into a queue for processingvoidsendTextMessage(String queueName, String text)Sends a non-expiringTextMessagewith average priority.voidsendTextMessage(String queueName, String text, int deliveryMode, int priority, long timeToLive)Sends aTextMessage.voidsendTextMessage(String queueName, String text, int deliveryMode, int priority, long timeToLive, long deliveryTime)Sends aTextMessage.voidsendTextMessage(String queueName, String text, long deliveryTime)Sends a non-expiringTextMessagewith average priority.voidsetBinaryMode(boolean binaryMode)If true, uses binary mode to send messages.voidstart()Starts the server.voidstop()Stops this JMS server.
-
-
-
Constructor Detail
-
Async
public Async(String dataDirectory, boolean useLibAio, QueueConfig... queueConfigs)
Creates and configures a new instance.- Parameters:
dataDirectory- root directory where persistent messages are storeduseLibAio- true to use libaio, false not to use (See Artemis log statements to check if it was detected).queueConfigs- vararg of QueueConfig instances.
-
Async
public Async(String dataDirectory, boolean useLibAio, com.google.inject.Injector injector, QueueConfig... queueConfigs)
Creates and configures a new instance.- Parameters:
dataDirectory- root directory where persistent messages are storeduseLibAio- true to use libaio, false to use NIO.injector- Google Guice injector. Used to inject dependency members into commands if needed.queueConfigs- vararg of QueueConfig> instances.
-
-
Method Detail
-
start
public void start()
Starts the server.
-
stop
public void stop()
Stops this JMS server.
-
setBinaryMode
public void setBinaryMode(boolean binaryMode)
If true, uses binary mode to send messages. If set to false (default), will send messages as strings. Test which method is faster in your environment for your CPU and IO performance. generally, binary mode will use a lot less IO, but more CPU and vice versa.- Parameters:
binaryMode- true to send messages in binary mode, false to send as strings.
-
configureNetty
public void configureNetty(String host, int port)
Call this method once after a constructor in order to create a Netty instance to accept out of VM messages.- Parameters:
host- host to bind toport- port to listen on
-
send
public void send(String queueName, Command command)
Sends a command into a queue for processing- Parameters:
queueName- name of queuecommand- command instance.
-
send
public void send(String queueName, Command command, Date deliveryTime)
Sends a command into a queue for processing- Parameters:
queueName- name of queuecommand- command instance.deliveryTime- delivery time in the future. Ifnull, or in the past, the message will be delivered as usual.
-
send
public void send(String queueName, Command command, int deliveryMode)
Sends a command into a queue for processing- Parameters:
queueName- name of queuecommand- command to processdeliveryMode- delivery mode:DeliveryMode.
-
send
public void send(String queueName, Command command, int deliveryMode, long deliveryTime)
Sends a command into a queue for processing- Parameters:
queueName- name of queuecommand- command to processdeliveryMode- delivery mode:DeliveryMode.deliveryTime- delivery time in milliseconds
-
send
public void send(String queueName, Command command, int deliveryMode, int priority, long timeToLive)
Sends a command into a queue for processing- Parameters:
queueName- name of queuecommand- command to processdeliveryMode- delivery mode:DeliveryMode.priority- priority of the message. Correct values are from 0 to 9, with higher number denoting a higher priority.timeToLive- the message's lifetime (in milliseconds, where 0 is to never expire)
-
send
public void send(String queueName, Command command, int deliveryMode, int priority, long timeToLive, long deliveryTime)
Sends a command into a queue for processing- Parameters:
queueName- name of queuecommand- command to processdeliveryMode- delivery mode:DeliveryMode. 1 for non-persistent, 2 for persistent.priority- priority of the message. Correct values are from 0 to 9, with higher number denoting a higher priority.timeToLive- the message's lifetime (in milliseconds, where 0 is to never expire)deliveryTime- The specified value must be a positive long corresponding to the time the message must be delivered (in milliseconds). For instance,System.currentTimeMillis() + 5000would be 5 seconds from now.
-
receiveCommand
public Command receiveCommand(String queueName)
Receives a command from a queue synchronously. If this queue also has listeners, then commands will be distributed across all consumers.- Parameters:
queueName- name of queue- Returns:
- command if found. If command not found, this method will block till a command is present in queue.
see
receiveCommand(String, long)
-
receiveCommand
public <T extends Command> T receiveCommand(String queueName, Class<T> type)
Receives a command from a queue synchronously. If this queue also has listeners, then commands will be distributed across all consumers. This method will block until a command becomes available for this consumer.- Parameters:
queueName- name of queuetype- expected class of a command- Returns:
- command if found. If command not found, this method will block till a command is present in queue.
see
receiveCommand(String, long)
-
receiveCommand
public <T extends Command> T receiveCommand(String queueName, int timeout, Class<T> type)
Receives a command from a queue synchronously. If this queue also has listeners, then commands will be distributed across all consumers.- Parameters:
queueName- name of queuetimeout- timeout in milliseconds. If a command is not received during a timeout, this methods returns null.type- expected class of a command- Returns:
- command if found. If command not found, this method will block till a command is present in queue.
see
receiveCommand(String, long)
-
receiveCommand
public Command receiveCommand(String queueName, long timeout)
Receives a command from a queue synchronously. If this queue also has listeners, then commands will be distributed across all consumers.- Parameters:
queueName- name of queuetimeout- timeout in milliseconds. If a command is not received during a timeout, this methods returns null.- Returns:
- command if found. If command not found, this method will block till a command is present in queue or a timeout expires.
-
receiveMessage
public javax.jms.Message receiveMessage(String queueName, long timeout)
Receives a messafge from a queue asynchronously.If this queue also has listeners, then messages will be distributed across all consumers.- Parameters:
queueName- name of queuetimeout- timeout in millis.- Returns:
- message if found, null if not.
-
sendTextMessage
public void sendTextMessage(String queueName, String text)
Sends a non-expiringTextMessagewith average priority.- Parameters:
queueName- name of queuetext- body of message
-
sendTextMessage
public void sendTextMessage(String queueName, String text, long deliveryTime)
Sends a non-expiringTextMessagewith average priority.- Parameters:
queueName- name of queuetext- body of messagedeliveryTime- The specified value must be a positive long corresponding to the time the message must be delivered (in milliseconds). For instance,System.currentTimeMillis() + 5000would be 5 seconds from now.
-
sendTextMessage
public void sendTextMessage(String queueName, String text, int deliveryMode, int priority, long timeToLive)
Sends aTextMessage.- Parameters:
queueName- name of queuetext- body of messagedeliveryMode- delivery mode:DeliveryMode.priority- priority of the message. Correct values are from 0 to 9, with higher number denoting a higher priority.timeToLive- the message's lifetime (in milliseconds, where 0 is to never expire)
-
sendTextMessage
public void sendTextMessage(String queueName, String text, int deliveryMode, int priority, long timeToLive, long deliveryTime)
Sends aTextMessage.- Parameters:
queueName- name of queuetext- body of messagedeliveryMode- delivery mode:DeliveryMode.priority- priority of the message. Correct values are from 0 to 9, with higher number denoting a higher priority.timeToLive- the message's lifetime (in milliseconds, where 0 is to never expire)deliveryTime- The specified value must be a positive long corresponding to the time the message must be delivered (in milliseconds). For instance,System.currentTimeMillis() + 5000would be 5 seconds from now.
-
getBatchReceiver
public BatchReceiver getBatchReceiver(String queueName, long timeout)
Generate aBatchReceiverto receive and process stored messages. This method ALWAYS works in the context of a transaction.- Parameters:
queueName- name of queuetimeout- timeout to wait.- Returns:
- instance of
BatchReceiver.
-
getTopCommands
public List<Command> getTopCommands(int count, String queueName)
Returns top commands in queue. Does not remove anything from queue. This method can be used for an admin tool to peek inside the queue.- Parameters:
count- number of commands to lookup.- Returns:
- top commands in queue or empty list is nothing is found in queue.
-
getTopTextMessages
public List<String> getTopTextMessages(int maxSize, String queueName)
Returns topTextMessages in queue. Does not remove anything from queue. This method can be used for an admin tool to peek inside the queue.- Parameters:
maxSize- max number of messages to lookup.- Returns:
- top commands in queue or empty list is nothing is found in queue.
-
getBytes
public static byte[] getBytes(javax.jms.BytesMessage message) throws javax.jms.JMSException- Throws:
javax.jms.JMSException
-
getMessageCounts
public Map<String,Long> getMessageCounts()
Returns counts of messages for all queues.- Returns:
- map, where a key is a queue name, and value is a number of messages currently in that queue.0
-
getMessageCount
public long getMessageCount(String queue)
Returns number of messages currently in queue- Parameters:
queue- queue name- Returns:
- number of messages currently in queue
-
resume
public void resume(String queueName)
Resumes a paused queue- Parameters:
queueName- queue name
-
pause
public void pause(String queueName)
Pauses a queue. A paused queue stops delivering commands to listeners. It still can accumulate commands.- Parameters:
queueName- queue name.
-
isPaused
public boolean isPaused(String queueName)
- Parameters:
queueName- queue name- Returns:
- true if queue is paused, false if not.
-
removeMessages
public int removeMessages(String queueName, String filter)
Removes messages from queue.- Parameters:
queueName- queue namefilter- filter selector as in JMS specification. See: JMS Message Selectors- Returns:
- number of messages removed
-
removeAllMessages
public int removeAllMessages(String queueName)
Removes all messages from queue.- Parameters:
queueName- queue name.- Returns:
- number of messages removed
-
moveMessages
public int moveMessages(String source, String target)
Moves all messages from one queue to another- Parameters:
source- name of source queuetarget- name of target queue- Returns:
- number of messages moved
-
moveMessage
public boolean moveMessage(String jmsMessageId, String source, String target)
Moves a message from one queue to another- Parameters:
jmsMessageId- JMS message id of a message to movesource- name of source queuetarget- name of target queue- Returns:
- true if message moved
-
getConfig
public org.apache.activemq.artemis.core.config.Configuration getConfig()
Get additional server configuration.
-
-