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"));
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:
Output:
Hopefully it helps someone else out!