2

I use Apache NMS (in c#) to receive messages from ActiveMQ. I want to be able to acknowledge every message I received, or roll back a message in case I had an error.

I solved the first part by using the CreateSession(AcknowledgementMode.IndividualAcknowledge), and then for every received message I use message.Acknowledge().

The problem is that in this mode there is no Rollback option. if the message is not acknowledged - I can never receive it again for another trial. It can only be sent to another consumer, but there isn't another consumer so it is just stucked in queue.

So I tried to use AcknowledgementMode.Transactional instead, but here there is another problem: I can only use session.Commit() or session.Rollback(), but there is no way to know which specific message I commit or role back.

What is the correct way to do this?

Doron Saar
  • 21
  • 3

2 Answers2

0

Stay with INDIVIDUAL_ACKNOWLEDGE and then try session.recover() and session.close(). Both of those should signal to the broker that the messages are not going to be acknowledged.

Matt Pavlovich
  • 4,087
  • 1
  • 9
  • 17
0

My solution to this was to throw an exception if (for any reason (exception from db savechanges event for example)) I did not want to acknowledge the message with message.Acknowledge().

When you throw an exception inside your extended method of IMessageConsumer Listener then the message will be sent again to your consumer for about 5 times (it will then moved to default DLQ queue for investigation).

However you can change this using the RedeliveryPolicy in connection object. Example of Redelivery

Policy redeliveryPolicy = new RedeliveryPolicy
{
InitialRedeliveryDelay = 5000, // every 5 secs
MaximumRedeliveries = 10,      // the message will be redelivered 10 times
UseCollisionAvoidance = true,  // use this to random calculate the 5 secs
CollisionAvoidancePercent = 50,// used along with above option
UseExponentialBackOff = false
};

If message fails again (after 10 times) then it will be moved to a default DLQ queue. (this queue will be created automatically) You can use this queue to investigate the messages that have not been acknowledged using an other consumer.

Nsio
  • 1
  • 1