-->

ActiveMQ的嵌入式代理主题对队列桥XML配置(ActiveMQ embedded broker

2019-10-21 01:20发布

我的目标+进展

我感兴趣的使用ActiveMQ的发布消息到一个主题,并把它缩小到多个队列。 我已设法通过提供包含复合主题的XML的配置来实现这一与命令行经纪人:

<destinationInterceptors>
    <virtualDestinationInterceptor>
        <virtualDestinations>
            <compositeTopic name="LOCAL.EC.T">
                <forwardTo>
                    <queue physicalName="LOCAL.EC.Q.1" />
                    <queue physicalName="LOCAL.EC.Q.2" />                       
                </forwardTo>
            </compositeTopic>
        </virtualDestinations>
    </virtualDestinationInterceptor>
</destinationInterceptors>

使用此启动命令: activemq start xbean:amq.xml 。 在这种情况下, amq.xml是我主动MQ XML配置文件。 还有更多关于XML配置这里: http://activemq.apache.org/xml-configuration.html 。

我的问题

如果我发布一条消息,上面使用Web控制台的话题,它在两个队列显示为预期。 但现在我想切换到使用嵌入式经纪人。


在下面的代码,我可以告诉大家,它是用我的amq.xml文件(当我改变它,我获得相关的错误),但是当我发布到的话题,队列块接收到永远。 如果我发布和接收同一主题或队列不过,一切工作正常。 为什么不工作我复合的话题


//Create the broker using the xbean configuration and start it.
brokerService = BrokerFactory.createBroker(new URI("xbean:amq.xml"));
brokerService.setBrokerName("localhost");
brokerService.setPersistent(false);
brokerService.setDeleteAllMessagesOnStartup(true);
brokerService.setUseJmx(false);
brokerService.start();

//Create the connection factory and JMS template.
connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("vm://localhost?create=false&jms.redeliveryPolicy.maximumRedeliveries=-1");

//Send the message to the topic.
template = new JmsTemplate(connectionFactory);
ActiveMQMapMessage message = new ActiveMQMapMessage();
message.setString("batch", "Hello World!");
template.convertAndSend("LOCAL.EC.T",message);

//Read the message from the queues.
final Message receive = template.receive("LOCAL.EC.Q.1");
MapMessage received = (MapMessage)receive;
System.out.println(received.getString("batch"));  

Answer 1:

我发现了一个网站,演示了如何在一个稍微不同的方式产生的消息上的例子。 不幸的是,我关闭了标签,并不能找到引用它的链接。

无论哪种方式,我结合我xbean配置该消息,制作风格,现在一切工作。 这里是工作的代码:

brokerService = BrokerFactory.createBroker(new URI("xbean:amq.xml"));
brokerService.setPersistent(false);
brokerService.setDeleteAllMessagesOnStartup(true);
brokerService.setUseJmx(false);
brokerService.start();

connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
template = new JmsTemplate(connectionFactory);

//Create a connection.
Connection connection = connectionFactory.createConnection();
connection.start();

//Create a session.
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

//Create a topic and publish to it - it should be linked to 4 queues by the configuration.
System.out.println("Posting message to topic:");
Destination destination = session.createTopic("LOCAL.EC.T");
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
ActiveMQMapMessage message = new ActiveMQMapMessage();
message.setString("batch", "Hello World!");
producer.send(message);

//Read the message from the queues.
System.out.println("Q1: " + ((MapMessage)receive("LOCAL.EC.Q.1")).getString("batch"));
System.out.println("Q2: " + ((MapMessage)receive("LOCAL.EC.Q.2")).getString("batch"));
System.out.println("Q3: " + ((MapMessage)receive("LOCAL.EC.Q.3")).getString("batch"));
System.out.println("Q4: " + ((MapMessage)receive("LOCAL.EC.Q.4")).getString("batch"));

输出:

发布消息主题:

Q1:你好世界!

Q2:世界您好!

Q3:世界您好!

Q4:世界您好!

结束应用程序。

希望这可以帮助别人了!



文章来源: ActiveMQ embedded broker topic-to-queue bridge with XML config