Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS queue receive message?

Tags:

java

jms

In the JMS API doc, it said:

public Message receive() throws JMSException

Receives the next message produced for this message consumer. This call blocks indefinitely until a message is produced or until this message consumer is closed.

If this receive is done within a transaction, the consumer retains the message until the transaction commits.

Here I have three questions: 1. in the code, do we need while-loop to receive message ? like:

while(true){
    Message msg = queue.receive();
    ....
}
  1. what is the transaction setting ? how to commit a transaction ? like this:

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

Thanks

like image 568
user595234 Avatar asked Dec 13 '11 16:12

user595234


Video Answer


1 Answers

  1. If you are going to use receive then you will need some sort of loop to keep receiving messages after the first one is received. Remember that you can also setup a messagelistener and get the received messages async via a callback method and not have to block.

  2. The transaction is generally set to AUTO_ACKNOWLEDGE by default which means that as soon as the message is taken from the queue it is gone and cannot be rolled back. If you want to setup a transaction you need to set the session to transacted and the method to SESSION_TRANSACTED. When you call commit() on the session the messages will be acknowledged on the queue.

  3. receiveNoWait() can have transaction support if you setup the acknowledgement mode correctly and you use commit() and rollback() on the session.

If I were you I would create a MessageListener and not have to worry about spinning a thread to poll the receive methods. Keep in mind that an implicit transaction is started once the 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()
        }
    }
}
like image 176
gregwhitaker Avatar answered Oct 13 '22 20:10

gregwhitaker