Ajinkya
Ajinkya

Reputation: 429

JMS topic subscribers do not receive messages when they aren't connected

I have one publisher which is publishing messages on topic, and I have 2 subscribers S1 & S2 which are receiving the messages. When my publisher sends a message and both subscribers are up then they both receive the message. However, when my subscribers are not up and my publisher sends a message then when the subscribers come up they do not receive the message. How can my subscribers receive messages sent when they are not up?

Note: I am using Spring Boot.

MessageProducer.java

@RestController
@RequestMapping("/rest/produce")
public class MessageProducer {
    private static final Logger LOG = LoggerFactory.getLogger(MessageProducer.class);

    @Autowired
    public JmsTemplate jmsTemplate;

    @GetMapping("/{message}")
    public void run(@PathVariable("message") final String message) throws Exception {
        final String messageText = "Hello Blockchain World";
        LOG.info("============= Sending " + message);
        sendMessage(message);
    }

    public void sendMessage(String payload) {
        this.jmsTemplate.convertAndSend("example", payload);
    }
}

application.properties - (MessageProducer)

spring.qpidjms.remoteURL=amqp://127.0.0.1:5672
spring.qpidjms.username=admin
spring.qpidjms.password=admin
activemq.broker-url=tcp://localhost:61616
server.port=8888
spring.jms.pub-sub-domain=true

MessageConsumer.java:

@Component
public class MessageConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(MessageConsumer.class);

    @JmsListener( destination = "example")
    public void processMsg(String message) {
        LOG.info("============= Received: " + message);
    }
}

MessageConsumer main Initiator class (ignore class name)

@SpringBootApplication
@EnableJms
public class QpidJMSSpringBootHelloWorld {

    public static void main(String[] args) {
        SpringApplication.run(QpidJMSSpringBootHelloWorld.class, args);
    }
}

Second consumer is same as first one just port no has been changed in application.properties

application.properties (MessageConsumer-1, S1)

spring.qpidjms.remoteURL=amqp://127.0.0.1:5672
spring.qpidjms.username=admin
spring.qpidjms.password=admin
activemq.broker-url=tcp://localhost:61616
server.port=9999
spring.jms.pub-sub-domain=true

application.properties (S2)

spring.qpidjms.remoteURL=amqp://127.0.0.1:5672
spring.qpidjms.username=admin
spring.qpidjms.password=admin
activemq.broker-url=tcp://localhost:61616
server.port=9990
spring.jms.pub-sub-domain=true

Upvotes: 0

Views: 1963

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 35008

Messages sent to a multicast address (i.e. a JMS topic) are routed to all existing multicast queues (i.e. JMS subscriptions). If no subscriptions exist then the messages are discarded. This is the fundamental semantics of multicast routing (i.e. JMS publish-subscribe).

If you want messages for a subscriber to be stored when the subscriber is not connected then the subscriber must create a durable subscription before any messages which it wants are sent. Once the durable subscription is created the messages sent to the topic will be stored in that subscription even if the subscriber is not connected.

Upvotes: 4

Related Questions