-1

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();
    }
}
rozerro
  • 5,787
  • 9
  • 46
  • 94
  • Tried "something different than" `vm://localhost`!?;)... [E.g.](https://stackoverflow.com/q/38750843/592355) – xerx593 Sep 24 '22 at 12:16
  • @xerx593 tried to use my `getHostName()` i.e. `new ActiveMQConnectionFactory("tcp://"+myhostname+":61616")` but then I get: Could not connect to broker URL: tcp://...:61616 . Reason: java.net.ConnectException: Connection refused: connect – rozerro Sep 24 '22 at 12:38
  • @xerx593 the same error for the `tcp://0.0.0.0:61616` – rozerro Sep 24 '22 at 12:43
  • I had to start `activemq` on my pc to work as expected, but why it is not required if I use it within the same app? – rozerro Sep 24 '22 at 13:04
  • So, you could make it!? Within same app <-> same "VM"(!)? – xerx593 Sep 24 '22 at 15:42
  • @xerx593 I could make it for differenet apps on the same PC. – rozerro Oct 02 '22 at 06:34

1 Answers1

0

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"))

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