2011-12-20 65 views
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; 
} 

回答

0

要调用receiveNoWait(),这将同时检索一个消息。根据您的JMS提供程序,通常会发生的情况是JMS客户端将一次检索多条消息并将其缓存在客户端以减少网络延迟。当你调用接收时,它会从这个缓存中获取消息并提供给你。

如果您看到分钟很长的延迟,那么在将这些消息放入主题时如何出错或者在处理每封消息时阻止了您的消息接收。如果您不想在处理时考虑实现MessageListener接口而不是使用receive方法,或者您可以从receive方法获取消息并异步地在线程池中处理它们,您不希望阻止收到消息。

当您创建消费者可以添加监听器,如下:

MessageListener listener = new MyListener(); 
consumer.setMessageListener(listener); 

然后创建一个类来处理消息或实现该接口在现有的消费类:

public class MyListener implements MessageListener { 
    public void onMessage(Message message) 
    { 
    TextMessage text = (TextMessage) message; 

    System.out.println("Message: " + text.getText()); 
    } 
} 
+0

是,我想检索订户的所有可用消息。这就是为指定数量的消息循环的原因。它正在阅读所有信息,但不是在发布后立即发布,需要5-6分钟(有时甚至更多)。我观察到一个奇怪的行为,如果我在调试模式下运行我的jboss,并且如果我有一些断点,它会立即读取消息。但是,如果在正常模式下运行,则需要一些时间来阅读。 – John 2011-12-20 22:44:29

+0

既然你提到过JBoss,我假设你在应用服务器内部使用JBoss Messaging作为你的JMS提供者? – gregwhitaker 2011-12-20 22:49:45

+0

是的,你是对的。我正在使用JBOSS提供的JMS服务器。我是否缺少配置? – John 2011-12-20 22:57:50