spring jms error handler access to message

2019-07-15 03:49发布

问题:

We are using spring-jms to receive message off the queue. But sometimes we got a connection issue (downstream JDBC on the listener thread) where we don't have enough connection in the pool. So we want to retry that message again and if we still dont have connection then we will reject the message

We are trying this in error handler and that error handler instance is inject to spring default message listener container but I am not sure how to access the message object as it only provides Throwable object.

Does anyone know how to access actual message object?

回答1:

The only way to pass the message to the error handler is to catch the exception in your listener and add the message as a property to a new exception (probably wrapping the original exception in the cause).

You may find it simpler to just handle the exception in your user code.

If you are using message-driven POJOs instead of a MessageListener; you would have to subclass the MessageListenerAdapter and call super.onMessage() in a try/catch block.

EDIT:

In any case, the ErrorHandler is invoked after the rollback, so it can't change that behavior.



标签: spring-jms