Seb
Seb

Reputation: 3912

How to not consume JMS message when an error happen

I'm creating a topic with multiple consumer, each of them identified by a clientId. The behaviour I'm seeing is :

Is there a way to stop the consumption after 3 try for instance ?

Upvotes: 1

Views: 448

Answers (1)

Axel Podehl
Axel Podehl

Reputation: 4323

You could create a transacted JMS Session:

// create JMS Session from JMS Connection
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

and use the Session.rollback() method to indicate that you need to see that message again:

  public void onMessage(Message message)
  {
      msgsReceived++;
      System.err.println("received: " + message);
      if( msgsReceived<3 ) { // simulating an error case

        session.rollback();
      } else {
        session.commit();
      }

you should then see this message 3 times until you commit it the last time.

Upvotes: 2

Related Questions