两个嵌入式代理之间的网络连接,以共享主题消息

时间:2016-10-07 12:43:47

标签: jms activemq

我正在尝试创建一个嵌入式代理网络,我试图实现的拓扑结构如下:

enter image description here

所以我想运行Broker 1,它将成为所有消息的初始接收者,并持有一个主题%X%。然后我想将Broker 2和Broker 3连接到Broker 1并让它们通过网络连接监听Broker 1。 最终,我希望允许消费者从连接到Broker 2和Broker 3的%X%主题接收消息。

到目前为止,我已经编写了以下代码:

经纪人1:

BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61617");
broker.addNetworkConnector("static:(tcp://localhost:61616)");
broker.start();

经纪人2:

BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61618");
broker.addNetworkConnector("static:(tcp://localhost:61616)");
broker.start();

经纪人3:

public class Producer {

    private Connection connection;

    public Producer() throws JMSException {
        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL("tcp://localhost:61616");

        connection = connectionFactory.createConnection();
        connection.start();
        ....

    }

    public void produceMessage(int x) {
        try {
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic("Testtopic");
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            String text = "Hello world " + x + "! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
            TextMessage message = session.createTextMessage(text);
            System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());

            producer.send(message);
            session.close();
        }
         ......
    }
}

制片:

public class Consumer {
    public Consumer() throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL("tcp://localhost:61618"); // BROKER 3
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(session.createTopic("Testtopic"));
        consumer.setMessageListener(new HelloMessageListener());

    }

    private static class HelloMessageListener implements MessageListener {
        public void onMessage(Message message) {
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println("Consumer " + Thread.currentThread().getName() + " received message: " + textMessage.getText());
              .......
        }

    }
}

消费者:

{{1}}

但是,当我连接到消费者的 tcp:// localhost:61618 Broker 3 )时,我无法接收任何内容信息。在此期间如果直接连接到 tcp:// localhost:61616 (初始接收者,经纪人1 ),消费者接收消息,一切顺利。我想我错过了连接器配置中的一些东西。你能帮帮我吗?

谢谢,

干杯

1 个答案:

答案 0 :(得分:0)

networkConnectors看起来在uri中有错误的端口。

应该是:

在broker1上:   .addNetworkConnector(TCP://本地主机:61617)   .addNetworkConnector(TCP://本地主机:61618)

您不需要从2,3回到1的网络连接器。

我还建议配置完整对象并添加一些参数来支持配置... duplex =" false",networkTTL = 1等。