2

Learning durable subscription. I tweaked the example given here. I have written two programs. In one I create topic and send message with PERSISTENCE mode. In other program I create durable subscribers for same topic and try to receive.

I run first program. It finishes successfully. Then I run second(consumer) program. But there it does not get any message. receive() message remains blocked.

What am I missing?

Main part of producer program is

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
try{
    con = connectionFactory.createConnection();
    con.setClientID("DurabilityTest");
    con.start();
    session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic  myFirstTopic =  session.createTopic("myFirstTopic");
    MessageProducer producer = session.createProducer(myFirstTopic);
    // Registering consumers. But not calling receive on them
    MessageConsumer  consumer1 = session.createDurableSubscriber(myFirstDurableTopic, "consumer1", "", false);
    MessageConsumer  consumer2 = session.createDurableSubscriber(myFirstDurableTopic,"consumer2", "", false);
    TextMessage txtMsg = session.createTextMessage("Namaskar mitranno2!!");
    producer.send(myFirstTopic, txtMsg, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE+10000);
}catch(JMSException e){
    e.printStackTrace();
}

Main part of consumer program is

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
try{
    con = connectionFactory.createConnection();
    con.setClientID("DurabilityTest");
    con.start();
    session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic  myFirstTopic =  session.createTopic("myFirstTopic");
    MessageConsumer  consumer1 = session.createDurableSubscriber(myFirstTopic, "consumer1", "", false);
    processMessage(consumer1,consumer1.receive());
    MessageConsumer  consumer2 = session.createDurableSubscriber(myFirstTopic,"consumer2", "", false);
    processMessage(consumer2,consumer2.receive());
}catch(JMSException e){
    e.printStackTrace();
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kaushik Lele
  • 6,439
  • 13
  • 50
  • 76

1 Answers1

1

A durable Topic subscription only starts to retain messages after it has been created. So if you send messages to the Topic and there's no durable topic subscriptions registered on the broker those message are discarded regardless of the persistence mode of the message.

If you redo your testing and first create a durable topic subscription and then close the consumer you can send persistent messages to the Topic and then later when you run the durable consumer test again it will get the message sent while it was offline.

It's tricky to know what exactly is going on in your application but the use of a VM broker could be at play here, there's no certainty given the code given that each of these two bits producer / consumer aren't creating their own instance of a VM broker, are they being run in the same VM?

Why don't you stand up a broker and connect to it using your client programs?

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • Update your question to show the code, otherwise it didn't happen – Tim Bish Jun 07 '17 at 15:51
  • Updated, still unclear what you are doing but you are obviously doing something incorrectly. – Tim Bish Jun 08 '17 at 15:52
  • I run first program which creates 2 consumers and 1 producer. Producer sends on message. Thne session and connection are closed. Program finishes. Then I run second program where two consumers with same ID are created and they try to receive message. – Kaushik Lele Jun 09 '17 at 04:52