Search code examples
springjmsactivemq-classicspring-jmsjmstemplate

Using topics but not all the consumers take the message


I want to create a sender to generate message and the send it to all consumers. I am using topic, but something is wrong, if for example I have 3 consumers, only one takes the message in a random way. I don´t know what is wrog. Here is my server configuration

<amq:broker brokerName="granicaBroker" id="broker"
        persistent="false" deleteAllMessagesOnStartup="true" enableStatistics="false"
        useLoggingForShutdownErrors="true">
        <amq:networkConnectors>
            <amq:networkConnector name="linkToBrokerB"
                uri="static:(tcp://xxx.xx.xxx.xx:61617)" networkTTL="3" duplex="true" />
        </amq:networkConnectors>
        <amq:transportConnectors>
            <amq:transportConnector
                uri="nio://xxx.xx.xxx.xx:61616?jms.useAsyncSend=true?jms.useCompression=true"
                disableAsyncDispatch="false" />
        </amq:transportConnectors>
    </amq:broker>


    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="JMS.TOPIC.NOTIFICATION" />
    </bean>

    <bean id="producerTemplate" class="org.springframework.jms.core.JmsTemplate"
        p:connectionFactory-ref="connectionFactory"
        p:defaultDestination-ref="destination" />

    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
        p:brokerURL="nio://xxx.xx.xxx.xx:61616" />

And my producer class(just the part to send the message)

    @Autowired
    protected JmsTemplate jmsTemplate;

    final String text = applicationEvent.getMsg();

        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage(text);
                    return message;
            }
        });

My client context configuration:

    p:brokerURL="nio://xxx.xx.xxx.xx:61616" />

<bean id="simpleMessageListener" class="notifications.NotifierControllerImpl"/>
    <jms:listener-container container-type="default"
        connection-factory="connectionFactory" acknowledge="auto">
        <jms:listener destination="JMS.TOPIC.NOTIFICATION" ref="simpleMessageListener"
            method="onMessage" />
    </jms:listener-container>

And the java client class

public class NotifierControllerImpl implements MessageListener{
    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage tm = (TextMessage)message;
                System.out.println(tm.getText());
            }
        } catch (JMSException e) {
            System.out.println(e.toString());
        }
    }
}

Solution

  • The destination needs to be a topic not a queue; use ActiveMQTopic not ActiveMQQueue.