tags:

views:

284

answers:

2

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???

+1  A: 

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.

Elister
ok, on the on machine I will use the IP of first machine, BUT problem is same as before: on the second machine what should I use?
rauch
@rauch: What I meant is that in ActiveMQ **Broker** configuration file, in transportConnector element use tcp. URI attribute of transportConnector should use IP instead of localhost
Elister
+1  A: 

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.

Henryk Konsek
Isn't that the URL that does the JNDI connection though? Wouldn't the URL to connect to the broker be embedded in the ConnectionFactory object?
T.Rob