问题
My Goal + Progress
I'm interested in using ActiveMQ to publish a message to a topic and have it bridge to multiple queues. I have managed to achieve this with the command-line broker by providing an xml-config containing a composite topic:
<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>
Using this start-up command: activemq start xbean:amq.xml
. In this case, amq.xml
is my active MQ XML configuration file. There is more on xml configuration here: http://activemq.apache.org/xml-configuration.html.
My Problem
If I publish a message to the topic above using the web console, it shows up in both queues as expected. But now I want to switch to using an embedded broker.
In the following code, I can tell that it is using my amq.xml
file (as when I change it I get relevant errors), but when I publish to the topic, the receive on the queue blocks forever. If I publish and receive to the same topic or queue though, everything works fine. Why is my composite topic not working here?
//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"));
回答1:
I found an example on a website which demonstrated how to produce a message in a slightly different way. Unfortunately, I closed the tab and can't find the link to reference it.
Either way, I combined that message-production style with my xbean configuration and now everything is working. Here's the working code:
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"));
Output:
Posting message to topic:
Q1: Hello World!
Q2: Hello World!
Q3: Hello World!
Q4: Hello World!
Ending application.
Hopefully it helps someone else out!
来源:https://stackoverflow.com/questions/26063603/activemq-embedded-broker-topic-to-queue-bridge-with-xml-config