Problem: Spring JMS MessageListener Stuck / Not Receiving Messages
Scenario
A Spring Boot application using ActiveMQ with @JmsListener
suddenly stops receiving messages after running for a while. No errors in logs, and the queue keeps growing, but the consumers seem idle.
Setup
-
ActiveMQConnectionFactory
was used. -
The queue (
myQueue
) was filling up. -
Restarting the app temporarily fixed the issue.
Investigation
-
Checked ActiveMQ Monitoring (Web Console)
-
Messages were enqueued but not dequeued.
-
Consumers were still active, but not processing.
-
-
Thread Dump Analysis
-
Found that listener threads were stuck in a waiting state.
-
The problem only occurred under high load.
-
-
Checked JMS Acknowledgment Mode
-
Default
AUTO_ACKNOWLEDGE
was used. -
Suspected an issue with message acknowledgment.
-
-
Enabled Debug Logging
-
Added:
-
Found repeated logs like:
-
This hinted at connection issues.
-
-
Tested with a Different Message Broker
-
Using Artemis JMS instead of ActiveMQ resolved the issue.
-
Indicated that it was broker-specific.
-
Root Cause
ActiveMQ’s TCP connection was silently dropped, but the JMS client did not detect it.
-
When the connection is lost,
DefaultMessageListenerContainer
doesn’t always recover properly. -
ActiveMQ does not always notify clients of broken connections.
-
No exceptions were thrown because the connection was technically “alive” but non-functional.
Fix
-
Enabled
keepAlive
in ActiveMQ connection -
Forced Reconnection with Exception Listener
-
Implemented:
-
This ensured that if a connection was dropped, the listener restarted.
-
-
Switched to
DefaultJmsListenerContainerFactory
withDMLC
-
SimpleMessageListenerContainer
was less reliable in handling reconnections. -
New Configuration:
-
Final Outcome
✅ After applying these fixes, the issue never reoccurred.
🚀 The app remained stable even under high load.
Key Takeaways
-
Silent disconnections in ActiveMQ can cause message listeners to hang.
-
Enable
keepAlive
andoptimizeAcknowledge
for reliable connections. -
Use
DefaultJmsListenerContainerFactory
withDMLC
instead ofSMLC
. -
Implement an
ExceptionListener
to restart the JMS connection if necessary.