1

I'm new to JMS message passing. I want to publish a message to a topic and subscriber should receive it. Here is my spring bean definition.

<bean id="mySampleListener" class="com.my.sample.jms.MySampleListener" />

<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${broker.url}" />
</bean>

<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
</bean>

<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
    <constructor-arg index="0" value="sampleTopic"/>
</bean>

<bean id="sampleMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destinationName" value="sampleTopic" />
    <property name="messageListener" ref="mySampleListener" />
    <property name="pubSubDomain" value="true" />
    <property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
</bean>

<bean id="sampleJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="defaultDestination" ref="topicDestination" />
</bean>

I publish message in the constructor of JmsPublisher.class

<bean class="com.my.sample.jms.JmsPublisher">
    <constructor-arg index="0" ref="sampleJmsTemplate" />
</bean>

but my listener won't get executed.

public class MySampleListener implements MessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(MySampleListener.class);

    @Override
    public void onMessage(final Message message) {
        LOGGER.info("Message received. " + message);
    }
}

public class JmsPublisher {
    private static final Logger LOGGER = LoggerFactory.getLogger(JmsPublisher.class);

    public JmsPublisher(final JmsTemplate jmsTemplate) {
        LOGGER.info("Trying to publish JMS message...");

        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(final Session session) throws JMSException {
                final Message message = session.createTextMessage("test123");

                return message;
            }
        });

        LOGGER.info("Published JMS message.");
    }
}
Darshana
  • 2,462
  • 6
  • 28
  • 54
  • do you see the message/count in AMQ topic? –  Jul 27 '15 at 06:29
  • yes. enqueueCount is 1 – Darshana Jul 27 '15 at 06:32
  • can you copy all the code? Have you tried using a queue to see if you dequeue a message from there? For the topic, are you sure your listener is setup to listen before the message gets published? Perhaps try programmatic config and see if that works & then move to the spring config route to help troubleshoot the issue –  Jul 27 '15 at 07:20
  • this is whole code. I didn't use queue. I need topic. – Darshana Jul 27 '15 at 07:33
  • @user3813256 I tried with a Queue and it is working. But Topic is not. Is there any other configurations I'm missing here? – Darshana Jul 29 '15 at 08:37
  • I would look at whether my subscriber has a subscription registered with the right topic in AMQ before I send the message - perhaps run the subscription logic first, put in a wait state and then publish message to topic (you can also send multiple messages to the topic as well). You need to ensure that the subscription is in place before the message hits the topic - otherwise the message will not get sent to a subscriber. –  Jul 29 '15 at 10:36

0 Answers0