2
我有一個擁有持久訂閱者的主題。我可以發佈和使用這些消息,但是我發現在閱讀主題消息時會有一些延遲。從JMS消費消息時的延遲主題
我無法在一次調用中讀取消息。我需要多次調用該方法來讀取消息。我錯過了什麼?
private void publishMessage() {
TopicConnection topicConnection = null;
TopicSession topicSession = null;
TopicPublisher topicPublisher = null;
try {
topicConnection = connectionFactory.createTopicConnection();
topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
Topic topicName= topicSession.createTopic(topicName);
topicPublisher = topicSession.createPublisher(topicName);
ObjectMessage message = topicSession.createObjectMessage(customObject)
message.setStringProperty("user", userProperty);
topicPublisher.publish(message, DeliveryMode.PERSISTENT, Message.DEFAULT_PRIORITY, timeToLive);
} catch (JMSException e) {
throw new RuntimeException("Error Sending UMessage", e);
} finally {
closeConnections(null, topicPublisher, topicSession, topicConnection);
}
}
public void consumeMessages(String userId, int maxResults) {
TopicConnection topicConnection = null;
TopicSession topicSession = null;
TopicSubscriber topicSubscriber = null;
try {
topicConnection = connectionFactory.createTopicConnection("guest","guest");
topicConnection.setClientID("topic");
topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
Topic topicName= topicSession.createTopic(topicName);
topicSubscriber = topicSession.createDurableSubscriber(topicName, "subscriptionname", String.format("user = '%s'", userName), false);
topicConnection.start();
Message msg = null;
do {
msg = topicSubscriber.receiveNoWait();
if (msg instanceof ObjectMessage) {
ObjectMessage om = (ObjectMessage) msg;
else {
log.error(String.format(" %s", om.getObject().getClass().getSimpleName()));
}
} else if (msg != null) {
log.error(String.format("e %s", msg.getClass().getSimpleName()));
}
} while (msg != null && out.size() <= maxResults);
} catch (JMSException e) {
throw new RuntimeException("Error retrieving User Messages", e);
} finally {
closeConnections(topicSubscriber, null, topicSession, topicConnection);
}
return out;
}
是,我想檢索訂戶的所有可用消息。這就是爲指定數量的消息循環的原因。它正在閱讀所有信息,但不是在發佈後立即發佈,需要5-6分鐘(有時甚至更多)。我觀察到一個奇怪的行爲,如果我在調試模式下運行我的jboss,並且如果我有一些斷點,它會立即讀取消息。但是,如果在正常模式下運行,則需要一些時間來閱讀。 – John 2011-12-20 22:44:29
既然你提到過JBoss,我假設你在應用服務器內部使用JBoss Messaging作爲你的JMS提供者? – gregwhitaker 2011-12-20 22:49:45
是的,你是對的。我正在使用JBOSS提供的JMS服務器。我是否缺少配置? – John 2011-12-20 22:57:50