Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:07:02+00:00 2026-05-27T11:07:02+00:00

In the JMS API doc, it said: public Message receive() throws JMSException Receives the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:07:02+00:00Added an answer on May 27, 2026 at 11:07 am
    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()
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to receive a JMS message in an EJB2 (legacy sucks ;-) stateless
Today if we build an application using the JMS API (using MDBs as message
I use JMS (Java Message Service API) in my java application to work with
Flat-file JMS authentication is easy to set up on Glassfish (see http://docs.sun.com/app/docs/doc/821-0027/aeofg?a=view ). The
What is common and useful uses of JMS and Message Driven Beans?
I am trying to use JMS Message Selector with ActiveMQ. I read the documentation
Currently I am working on a JMS application. But I use plain JMS API
I need to get details of Message Receivers which are connected to JMS Queue,
How does async JMS work? I've below sample code: public class JmsAdapter implements MessageListener,
I am new JMS arena, I have created multiple message listeners but one listener

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.