1

I recently work with Topic in jms and I have a problem. My TopicSubscriber didn't receive message from publisher and I don't understand why.

Here is my TopicPublisher:

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();
    }
}

My TopicSubscriber:

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();
    }
}

In my subscriber I've got a NullPointer on message.getText() What is that problem? What am I doing wrong and how it can be fixed?

Mefisto_Fell
  • 876
  • 1
  • 10
  • 30
  • Are you sure the `NullPointerException` is on `topicSubscriber.receiveNoWait()`? That would indicate `topicSubscriber` is `null`. It seems more likely that the `NullPointerException` is coming from `message.getText()` because `message` is `null`. Please confirm. – Justin Bertram Apr 19 '19 at 15:31
  • Yes, I mean message.getText() is null. But its because subscriber didn't receive a message. And I don't understand why – Mefisto_Fell Apr 19 '19 at 16:05

1 Answers1

1

It appears that you're sending the message before you create the subscription. JMS topics use publish-subscribe semantics where any message published goes to all subscriptions. If there are no subscriptions then the message is discarded.

Also, since you're using receiveNoWait() you're severely reducing the chance that your client will ever get a message. In order for your client to actually receive a message the message would have to be sent in between the time you call createSubscriber(topic) and the time you call receiveNoWait(). Since those 2 calls happen very close together the window of time is very small.

If you really want your subscriber to get a message then run Subscriber first and use receive() rather than receiveNoWait() and then run Publisher. This will ensure the subscription exists when the message is sent and that the client waits to receive the message before exiting.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43