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