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) {
source share