rauch
rauch

Reputation: 1835

How can one use activemq not locally?

I can't understand how to use ActiveMQ not locally.
Suppose I have 2 machines, which need to exchange some messages.
On the on machine I start ActiveMQ broker:

> ~/bin/activemq

and use something like:

    javax.naming.Context ctx = new InitialContext();

    TopicConnectionFactory factory = (TopicConnectionFactory)ctx.lookup("connectionFactory");
    conn = factory.createTopicConnection();

    TopicSession session = conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
    Topic topic = null;
    try{
        topic = (Topic)ctx.lookup("MyTopic");
        System.out.println("MyTopic was found");
    }catch(NameNotFoundException nnfe){
        topic = session.createTopic("MyTopic");
        System.out.println("MyTopic was created");
    }
    TextMessage textMessage = session.createTextMessage();
    TopicPublisher publisher = session.createPublisher(topic);
    conn.start();

    textMessage.setText("My topic message number");
    publisher.publish(textMessage);
    System.out.println("sendMessage2topic");

where in jndi.properties I have:

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616

But what should I create on the other machine to subscribe on this topic? Shoul I create second local ActiveMQ broker ont the 2-nd machine, and how to subscribe on the remote topic that is on the first machine???

Upvotes: 10

Views: 14186

Answers (3)

Mohammed Rafeeq
Mohammed Rafeeq

Reputation: 2694

You need to use something like below. substitute the ipaddress with the target ip you want to use

java.naming.provider.url = tcp://172.16.202.168:61616

Upvotes: 1

Henryk Konsek
Henryk Konsek

Reputation: 9168

This line...

java.naming.provider.url = tcp://localhost:61616

...tells your connectionFactory to connect with the loopback interface. You can specify here address of the remote broker.

In such case your snippet will send message to the remote broker. Now it is up to broker to distribute the message over the registered subscribers (both local and remote ones).

In this scenario no broker is created (neither locally or remotely). You just connect to the existing broker. Of course, you can also create a local broker and configure it to route messages to the remote one (for example, you can do it via static/dynamic network transport or peer network transport protocol). ActiveMQ provides you a lot of integration topologies and patterns - but at first you must define what actually you want to achieve.

Upvotes: 6

Elister
Elister

Reputation: 1586

localhost:61616 will make activeMQ listen on loopback(127.0.0.1) interface only. Use the IP of the machine or 0.0.0.0 instead.

Upvotes: 7

Related Questions