Search code examples
javajmsspring-jms

Get the value of currentAttempts in ExceptionListener class


I am using a JMS queue.

Sometime my broker goes down. As per behavior of Spring JMS it continuously makes call and checks if the connection can be refreshed, e.g.:

DefaultMessageListenerContainer - Could not refresh JMS Connection for destination '<mydestination>' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=1}
...
DefaultMessageListenerContainer - Could not refresh JMS Connection for destination '<mydestination>' - retrying using FixedBackOff{interval=5000, currentAttempts=n, maxAttempts=1}

After 5 seconds it checks availability. I have passed my custom ExceptionListener so I am getting an exception in my class as well.

Based on some number attempts (e.g. 50) I need to send an email saying this is down. For this I need to get the value of currentAttempts in my ExceptionListener class. Can somebody help me here?


Solution

  • Count yourself: You could add a bean which stores a counter of the connection error and a timestamp of last occurrence. Your ExceptionListener uses a method of this bean class to add the exception to the counter. Based on the timestamp the bean decides to either increase the counter or to start from 1. If the threshold is exceeded, it sends the mail.