【问题标题】:Topic subscriber didn't receive a message主题订阅者未收到消息
【发布时间】:2019-09-09 17:23:48
【问题描述】:

我最近在 jms 中使用 Topic,但遇到了问题。我的 TopicSubscriber 没有收到发布者的消息,我不明白为什么。

这是我的主题发布者:

public class Publisher
{
    private static final String CONNECTION_URL = "tcp://localhost:61616";

    public static void main(String[] args) throws Exception
    {
        BrokerService service = BrokerFactory.createBroker(new URI("broker:(" + CONNECTION_URL + ")"));
        service.start();
        TopicConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTION_URL);

        // create a topic connection
        TopicConnection topicConn = connectionFactory.createTopicConnection();

        // create a topic session
        TopicSession topicSession = topicConn.createTopicSession(false,
                Session.AUTO_ACKNOWLEDGE);

        // lookup the topic object
        Topic topic = topicSession.createTopic("test");

        // create a topic publisher
        TopicPublisher topicPublisher = topicSession.createPublisher(topic);
        topicPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        // create the "Hello World" message
        TextMessage message = topicSession.createTextMessage();
        message.setText("Hello World");

        // publish the messages
        topicPublisher.publish(message);

        // print what we did
        System.out.println("Message published: " + message.getText());

        // close the topic connection
        topicConn.close();
    }
}

我的主题订阅者:

public class Subscriber
{
    private static final String CONNECTION_URL = "tcp://localhost:61616";

    public static void main(String[] args) throws Exception
    {
        TopicConnectionFactory connectionFactory = new ActiveMQConnectionFactory(CONNECTION_URL);

        // create a topic connection
        TopicConnection topicConn = connectionFactory.createTopicConnection();

        // create a topic session
        TopicSession topicSession = topicConn.createTopicSession(false,
                Session.AUTO_ACKNOWLEDGE);


        Topic topic = topicSession.createTopic("test");

        // create a topic subscriber
        TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);

        // start the connection
        topicConn.start();

        // receive the message
        TextMessage message = (TextMessage) topicSubscriber.receiveNoWait();

        // print the message
        System.out.println("Message received: " + message.getText());

        // close the topic connection
        topicConn.close();
    }
}

在我的订阅者中,我在 message.getText() 上有一个 NullPointer 那是什么问题?我做错了什么以及如何解决?

【问题讨论】:

  • 你确定NullPointerExceptiontopicSubscriber.receiveNoWait() 上吗?这表明topicSubscribernullNullPointerException 似乎更有可能来自message.getText(),因为messagenull。请确认。
  • 是的,我的意思是 message.getText() 为空。但这是因为订阅者没有收到消息。我不明白为什么

标签: java jms activemq jms-topic


【解决方案1】:

您似乎是在创建订阅之前发送消息。 JMS 主题使用发布-订阅语义,其中发布的任何消息都会发送到所有订阅。如果没有订阅,则丢弃该消息。

此外,由于您使用的是receiveNoWait(),因此大大降低了您的客户收到消息的机会。为了让您的客户真正收到消息,消息必须在您拨打createSubscriber(topic) 和您拨打receiveNoWait() 之间发送。由于这 2 个调用非常靠近,因此时间窗口非常小。

如果您真的希望您的订阅者收到消息,请先运行Subscriber,然后使用receive() 而不是receiveNoWait(),然后运行Publisher。这将确保在发送消息时订阅存在,并且客户端在退出之前等待接收消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多