Search code examples
javajmsmessagingreceiversubscriber

JMS message lost when receiver is not available


I am using JMS for message passing. And when both sender and receiver available I successfully received the message. But when I down the receiver and start again queued messages are not received to receiver again. Is there any configuration I need to do? I have increase the time to live as below to avoid expiring the session.

This is how my sender code looks like.

Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic(topic);
MessageProducer producer = session.createProducer(destination);
producer.setTimeToLive(18000000L);
TextMessage message = session.createTextMessage(customMessage.getContent());
producer.send(message);

Solution

  • What you're observing is the expected behavior. Since you're sending your message to a JMS topic you will be subject to publish/subscribe semantics. Publish/subscribe semantics dictate that messages are only placed in valid subscriptions. When your subscriber is offline it has no valid subscription on the broker to receive the messages therefore it will miss the messages sent to it when it is offline.

    You can use a durable subscriber to receive messages when the subscriber is offline or you can use a traditional JMS queue.