学习持久订阅。我调整了这里给出的例子。我写了两个程序。在一个中,我创建主题并使用 PERSISTENCE 模式发送消息。在其他程序中,我为同一主题创建持久订阅者并尝试接收。
我运行第一个程序。它成功完成。然后我运行第二个(消费者)程序。但是那里没有收到任何消息。receive() 消息仍然被阻止。
我错过了什么?
生产者计划的主要部分是
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();
}
消费者计划的主要部分是
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();
}