Consumer gets null messages from a producer

Viewed 29

I try to connenct two standalone apps using ActiveMQ. A sender sends message and a reciever should recieve the message from the sender. But the consumer is getting null.

Can it work the way I decribed?

(It's actualy work if I run producer and consumer in the same app, but the idea is to use different independent apps.)

the first app:

thread(new HelloWorldProducer(), false);

public static void thread(Runnable runnable, boolean daemon) {
    Thread brokerThread = new Thread(runnable);
    brokerThread.setDaemon(daemon);
    brokerThread.start();
}

public static class HelloWorldProducer implements Runnable {
public void run() {
    try {
        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = 
             new ActiveMQConnectionFactory("vm://localhost");

        // Create a Connection
        Connection connection = connectionFactory.createConnection();
        connection.start();

        // Create a Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue("TEST.FOO");

        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        // Create a messages
        String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
        TextMessage message = session.createTextMessage(text);

        // Tell the producer to send the message
        System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
        producer.send(message);
        
        Thread.sleep(10000);

        // Clean up
        session.close();
        connection.close();
    }
    catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}

}

the consumer app:

thread(new HelloWorldConsumer(), false);

public static void thread(Runnable runnable, boolean daemon) {
    Thread brokerThread = new Thread(runnable);
    brokerThread.setDaemon(daemon);
    brokerThread.start();
}

public static class HelloWorldConsumer implements Runnable, ExceptionListener {
public void run() {
    try {

        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = 
            new ActiveMQConnectionFactory("vm://localhost");

        // Create a Connection
        Connection connection = connectionFactory.createConnection();
        connection.start();

        connection.setExceptionListener(this);

        // Create a Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue("TEST.FOO");

        // Create a MessageConsumer from the Session to the Topic or Queue
        MessageConsumer consumer = session.createConsumer(destination);

        // Wait for a message
        Message message = consumer.receive(1000);

        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            String text = textMessage.getText();
            System.out.println("Received: " + text);
        } else {
            System.out.println("Received: " + message);
        }
        
        Thread.sleep(10000);

        consumer.close();
        session.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}
1 Answers

Both of your clients are using vm://localhost. As noted in the ActiveMQ documentation:

The first client to use the VM connection will boot an embedded broker. Subsequent connections will attach that the same broker. Once all VM connections to the broker have been closed, the embedded broker will automatically shutdown.

Therefore, if both your producer and consumer are running in the same JVM they will both use the same embedded instance of ActiveMQ.

However, if your producer and consumer are running in different JVMs then they will both need to be configured to connect to a remote, standalone instance of ActiveMQ. Therefore, you will need to start an instance of ActiveMQ somewhere on the network and then provide the producer and consumer with the hostname and port where ActiveMQ is running (e.g. new ActiveMQConnectionFactory("tcp://yourActiveMQHost:61616"))

Related