简体   繁体   中英

Messages not getting removed from JMS Queue while using XASession

I am trying to connect to solace queues and simply read messages from them. However, I am able to read the messages but the messages are not getting removed from the queue. Below is the code which I tried:

public void clearMessages() throws Exception {

        // Programmatically create the connection factory using default settings
        // Create connection to the Solace router

        SolXAConnectionFactoryImpl connectionFactory = returnConnFactory();


        XAConnection connection = connectionFactory.createXAConnection();

        XASession session = connection.createXASession();

        Queue queue = session.createQueue(QUEUE_NAME);
        connection.start();
        MessageConsumer messageConsumer = session.createConsumer(queue);
        //session.b

        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if(message instanceof SolTextMessage)  {
                    SolTextMessage solTextMessage =(SolTextMessage)message;
                    try {
                        System.out.println("Message cleared is : "+solTextMessage.getText());
                    } catch (JMSException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else {
                    System.out.println("Message Content: %s"+ SolJmsUtility.dumpMessage(message));
                }
                    latch.countDown(); 
            }
        });
        latch.await(120,TimeUnit.SECONDS);
        connection.stop();

        messageConsumer.close();
        session.close();
        connection.close();
    }

Here latch is the object of CountDownLatch which is initialized as:

CountDownLatch latch = new CountDownLatch(2);

You need to commit the XA transaction in order for messages to be consumed. In JMS, the function call is XAResource.commit(xid, true)

Also, is there a reason to use the CountDownLatch? If you would like to consume messages synchronously, you can choose not to set a message listener and call MessageConsumer.receive()

Solace does provide a basic sample showing how to make use of XA transactions. Refer to XATransactions.java in the samples directory of the API.

Note that the sample code is manually managing the XA transaction by calling the relevant XAResource methods such as XAResource.commit(). XA transactions are usually used within Java EE application servers that contain a transaction manager to manage the lifecycle of XA transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM