Is this because the main thread has been terminated and the listener is not live anymore? I thought with setMessageListener, the above code should internally create a thread that should always be running.
setMessageListener
does not create a new thread. When you create a new Session
from the Connection
, a new thread is created. So yes even if the main thread terminates, the Session thread should continue running.
Your code looks correct to me. Are you sure that messages exist on that topic? Also only messages published to the topic after the consumer comes live will be received.