How can one use activemq not locally?

activemq-classic, java, jms

Solution

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.

Problem

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

Original source