1

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());
        }
    }
}
Rys
  • 4,934
  • 8
  • 21
  • 37

2 Answers2

3

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

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I changed for a ActiveMQTopic becouse it made sense, but now the clients are not rereiving messages – Rys Jan 30 '15 at 14:37
  • Are you starting the consumers first, before publishing the message? With a topic, messages are only sent to active consumers at the time of publication, unless the subscriptions are durable (which is not the default). – Gary Russell Jan 30 '15 at 14:50
  • I am using a broker inside a tomcat and it runs first, then, I start the client – Rys Jan 30 '15 at 16:01
  • You should be able to debug this using the activemq web console, where you can see the consumers. – Gary Russell Jan 30 '15 at 16:11
  • But I am not using the binary distribution of activemq, is a war project with embedded broker. – Rys Jan 30 '15 at 16:25
  • Well, try debugging with the stand-alone version then switch to embedded. That's what I usually do. – Gary Russell Jan 30 '15 at 16:29
0

I change jms:listener-container part

Here is the code:

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="destination"/>
        <property name="messageListener" ref="simpleMessageListener" />
    </bean>

And it works!

Rys
  • 4,934
  • 8
  • 21
  • 37