Receive JMS Queue Message?

In a JMS API document, he said:

public Message receive() throws JMSException 

Gets the following message for this message consumer. This call is blocked indefinitely until a message appears or until this message consumer is closed.

If this receipt is performed inside a transaction, the consumer saves the message until the transaction is completed.

Here I have three questions: 1. In the code, do I need a while loop to receive the message? as:

 while(true){ Message msg = queue.receive(); .... } 
  1. What is transaction setup? how to make a transaction? eg:

     boolean transacted = false; session = connection.createQueueSession(transacted, Session.AUTO_ACKNOWLEDGE); 
  2. do receiveNoWait () have transaction support? how to use it?

thanks

+6
source share
1 answer
  • If you intend to use receive, you will need some kind of loop to receive messages after receiving the first. Remember that you can also configure the messagelistener and receive received async messages using the callback method and not block.

  • By default, a transaction is usually set to AUTO_ACKNOWLEDGE, which means that once a message is sent from the queue, it will disappear and cannot be canceled. If you want to set up a transaction, you need to set up a transaction session, and the method to SESSION_TRANSACTED. When you call commit () in a session, messages will be queued.

  • receiveNoWait () can support the transaction if you have correctly configured the confirmation mode and you use commit () and rollback () in the session.

If I were you, I would create a MessageListener and should not worry about scrolling through the thread to poll the receiving methods. Keep in mind that an implicit transaction is triggered after a session is created.

 public class JmsAdapter implements MessageListener, ExceptionListener { private ConnectionFactory connFactory = null; private Connection conn = null; private Session session = null; public void receiveMessages() { try { this.session = this.conn.createSession(true, Session.SESSION_TRANSACTED); this.conn.setExceptionListener(this); Destination destination = this.session.createQueue("SOME_QUEUE_NAME"); this.consumer = this.session.createConsumer(destination); this.consumer.setMessageListener(this); this.conn.start(); } catch (JMSException e) { //Handle JMS Exceptions Here } } @Override public void onMessage(Message message) { try { //Do Message Processing Here //Message sucessfully processed... Go ahead and commit the transaction. this.session.commit(); } catch(SomeApplicationException e) { //Message processing failed. //Do whatever you need to do here for the exception. //NOTE: You may need to check the redelivery count of this message first //and just commit it after it fails a predefined number of times (Make sure you //store it somewhere if you don't want to lose it). This way you're process isn't //handling the same failed message over and over again. this.session.rollback() } } } 
+3
source

Source: https://habr.com/ru/post/903698/


All Articles