我使用 WSO2 MB 2.0.1,我尝试将教程中提供的 JMS Java 订阅者扩展为持久订阅者。然后停止 jms 客户端并向主题发送一些消息。但是当我启动 jms 客户端时,它没有收到消息。
有人可以让我知道如何创建持久订阅者。我的要求是当 jms 订阅者上线时接收消息。
我的代码:public void subscribe(String topicName) {
Properties initialContextProperties = new Properties();
initialContextProperties.put("java.naming.factory.initial",
"org.wso2.andes.jndi.PropertiesFileInitialContextFactory");
String connectionString = "amqp://admin:admin@clientID/carbon?brokerlist='tcp://localhost:5672'";
initialContextProperties.put("connectionfactory.qpidConnectionfactory", connectionString);
initialContextProperties.put("topic.myWarning", "myWarning");
try {
InitialContext initialContext = new InitialContext(initialContextProperties);
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory) initialContext.lookup("qpidConnectionfactory");
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
//topicConnection.setClientID("clientID");
topicConnection.start();
TopicSession topicSession =topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//Topic topic = topicSession.createTopic(topicName);
Topic topic =(Topic) initialContext.lookup(topicName);
TopicSubscriber topicSubscriber =
topicSession.createDurableSubscriber(topic, "tom");
TextMessage receivedMessage = (TextMessage)topicSubscriber.receive();
System.out.println(receivedMessage);
// topicSubscriber.setMessageListener(new JCOMessageListener(
// topicConnection, topicSession, topicSubscriber));
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}