java 如何不在本地使用activemq?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:35:43  来源:igfitidea点击:

How can one use activemq not locally?

javajmsactivemq

提问by rauch

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:

我无法理解如何不在本地使用 ActiveMQ。
假设我有 2 台机器,它们需要交换一些消息。
在机器上我启动 ActiveMQ 代理:

> ~/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:

我在 jndi.properties 中的位置:

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

但是我应该在另一台机器上创建什么来订阅这个主题?我应该在第二台机器上创建第二个本地 ActiveMQ 代理,以及如何订阅第一台机器上的远程主题???

采纳答案by Henryk Konsek

This line...

这条线...

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

...tells your connectionFactoryto connect with the loopback interface. You can specify here address of the remotebroker.

...告诉您connectionFactory连接回环接口。您可以在此处指定远程代理的地址。

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 lotof integration topologies and patterns - but at first you must define what actually you want to achieve.

在这种情况下,没有创建代理(本地或远程)。您只需连接到现有代理即可。当然,您也可以创建一个本地代理并将其配置为将消息路由到远程代理(例如,您可以通过静态/动态网络传输或对等网络传输协议来实现)。ActiveMQ 为您提供了许多集成拓扑和模式 - 但首先您必须定义您实际想要实现的目标。

回答by Elister

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.

localhost:61616 将使 activeMQ 仅在环回(127.0.0.1)接口上侦听。请改用机器的 IP 或 0.0.0.0。

回答by Mohammed Rafeeq

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

您需要使用如下所示的内容。将 ipaddress 替换为您要使用的目标 ip

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