I have two Java applications: AppOne and AppTwo. In AppOne, I used a JMS sender to publish a Topic. In both AppOne and AppTwo, I used a JMS MessageListener to subscribe to the message published by AppOne. I used ActiveMQ as my JMS broker and Spring JMS. However, I can only see the echoed message received by AppOne message listener. But I can't see the echoed message received by AppTwo listener. AppOne message listener is in the same application/project as the message publisher. But AppTwo message listener is in a different application/project.
The AppOne listener class is:
public class CustomerStatusListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println("Subscriber 1 got you! The message is: " + ((TextMessage) message).getText());
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
} else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
}
It is invoked by a test calss JmsTest in AppOne:
public class JmsTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("message-bean.xml");
CustomerStatusSender messageSender = (CustomerStatusSender) context.getBean("customerMessageSender");
messageSender.simpleSend();
context.close();
}
}
The AppTwo listener class is:
public class CustomerStatusMessageListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
System.out.println("Subscriber 2 got you! The message is: "
+ ((TextMessage) message).getText());
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
} else {
throw new IllegalArgumentException(
"Message must be of type TextMessage");
}
}
}
The bean definition file for AppTwo where the Subscriber 2 lives in is:
<bean id="connectionFactoryBean" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!-- this is the Message Driven POJO (MDP) -->
<bean id="customerStatusListener" class="com.mydomain.jms.CustomerStatusMessageListener" />
<!-- and this is the message listener container -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactoryBean" />
<property name="destination" ref="topicBean" />
<property name="messageListener" ref="customerStatusListener" />
</bean>
The bean id topicBean is the bean that is associated with the publisher.
If both listener received the message sent from AppOne, I would have seen two echoed messages:
Subscriber 1 got you! The message is: hello world
Subscriber 2 got you! The message is: hello world
But right now I only see the first line which means only the listener in AppOne got the message. So how to let the listener in AppTwo get the message? The first listener is in the same application as the sender, so it is easy to understand that it can get the message. But how about the second listener which is in a different application? What is the correct way to subscribe to a JMS Topic published in another application?