Class BatchReceiver

java.lang.Object
org.javalite.async.BatchReceiver
All Implemented Interfaces:
Closeable, AutoCloseable

public class BatchReceiver extends Object implements Closeable
Use to batch-read text messages from Async in a cont5ext of a transaction. Usually used to pass JSON or XML documents.
Typical usage pattern:
 //send messages:
 async.sendTextMessage(QUEUE_NAME, jsonDocument);
 ... // repeat

 //receive messages:
 BatchReceiver br = async.getBatchReceiver(QUEUE_NAME, 100);
 List messages = br.receiveTextMessages(500);
 //... process messages, commit to database
 br.commit(); // <<< -- deletes messages from queue
 br.close();
 
*** NEVER FORGET TO COMMIT AND CLOSE! ***
Author:
igor on 8/8/17.
  • Constructor Details

    • BatchReceiver

      protected BatchReceiver(String queueName, long timeout, javax.jms.Connection connection) throws javax.jms.JMSException
      Throws:
      javax.jms.JMSException
  • Method Details

    • receiveTextMessages

      public List<String> receiveTextMessages(int maxSize)
      Receives and returns size Strings from the queue. Presumes that messages in queue are TextMessage.
      Parameters:
      maxSize - maximum number of messages to receive
      Returns:
      size messages from the queue.
    • commit

      public void commit()
      Commits a current transaction. This acknowledges that all messages have been processed. All messages received before this call will be removed from queue and not delivered again.
    • rollback

      public void rollback()
      Rolls back current transaction. All "received" messages will be re-delivered.
    • close

      public void close()
      Closes this resource (calling it is mandatory).
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable