Class 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 Artemis EmbeddedActiveMQ to 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 Detail

      • Async

        public Async​(String dataDirectory,
                     boolean useLibAio,
                     QueueConfig... queueConfigs)
        Creates and configures a new instance.
        Parameters:
        dataDirectory - root directory where persistent messages are stored
        useLibAio - 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 stored
        useLibAio - 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 to
        port - port to listen on
      • send

        public void send​(String queueName,
                         Command command)
        Sends a command into a queue for processing
        Parameters:
        queueName - name of queue
        command - command instance.
      • send

        public void send​(String queueName,
                         Command command,
                         Date deliveryTime)
        Sends a command into a queue for processing
        Parameters:
        queueName - name of queue
        command - command instance.
        deliveryTime - delivery time in the future. If null, 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 queue
        command - command to process
        deliveryMode - 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 queue
        command - command to process
        deliveryMode - 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 queue
        command - command to process
        deliveryMode - 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 queue
        command - command to process
        deliveryMode - 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() + 5000 would 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 queue
        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 <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 queue
        timeout - 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 queue
        timeout - 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 queue
        timeout - timeout in millis.
        Returns:
        message if found, null if not.
      • sendTextMessage

        public void sendTextMessage​(String queueName,
                                    String text)
        Sends a non-expiring TextMessage with average priority.
        Parameters:
        queueName - name of queue
        text - body of message
      • sendTextMessage

        public void sendTextMessage​(String queueName,
                                    String text,
                                    long deliveryTime)
        Sends a non-expiring TextMessage with average priority.
        Parameters:
        queueName - name of queue
        text - body of message
        deliveryTime - The specified value must be a positive long corresponding to the time the message must be delivered (in milliseconds). For instance, System.currentTimeMillis() + 5000 would be 5 seconds from now.
      • sendTextMessage

        public void sendTextMessage​(String queueName,
                                    String text,
                                    int deliveryMode,
                                    int priority,
                                    long timeToLive)
        Sends a TextMessage.
        Parameters:
        queueName - name of queue
        text - body of message
        deliveryMode - 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 a TextMessage.
        Parameters:
        queueName - name of queue
        text - body of message
        deliveryMode - 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() + 5000 would be 5 seconds from now.
      • getBatchReceiver

        public BatchReceiver getBatchReceiver​(String queueName,
                                              long timeout)
        Generate a BatchReceiver to receive and process stored messages. This method ALWAYS works in the context of a transaction.
        Parameters:
        queueName - name of queue
        timeout - 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 top TextMessages 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 name
        filter - 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 queue
        target - 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 move
        source - name of source queue
        target - name of target queue
        Returns:
        true if message moved
      • getConfig

        public org.apache.activemq.artemis.core.config.Configuration getConfig()
        Get additional server configuration.