简体   繁体   中英

Consume Messages in ActiveMQ with a service or a listener

I have written a small test where I send a message to an existing ActiveMQ FORWARD queue. Unfortunately the message is sent to the queue but not received. Below you will find my two attempts to receive this message: through a MyMessageListener and through a MessageService. Both methods fail. Here is my test:

 Map<Parameter, String> params = new HashMap<Parameter, String>();

    params.put(key1, "601");
    params.put(key2, "3000");

    Map<String,String> headers = Collections.singletonMap("method-name","prepareHotDrink");
    Message<?>  msg = MessageBuilder.withPayload(params)
    .copyHeaders(headers)
    .build();

    boolean i = inputChannel.send(msg);

This is my configuration file:

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
 <property name="brokerURL" value="${jms.primary.server}"/>
 </bean>


<!-- spring integration beans -->

<int-jms:channel id="inputChannel" queue-name="FORWARD"
    connection-factory="connectionFactory" auto-startup="true">
</int-jms:channel>

<!-- Consumers -->    
<int-jms:message-driven-channel-adapter
    id="jmsIn"
    container="messageListenerContainer"
    channel="inputChannel"
    extract-payload="true" />


<int:service-activator input-channel="inputChannel"
                   ref="messageService"
                   method="processMessage"/>


<bean id="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">     
    <property name="connectionFactory" ref="connectionFactory"/>     
    <property name="destination" ref="requestQueue"/>
    <property name="exposeListenerSession" value="false"/>
    <property name="messageListener" ref="messageListener" />
</bean> 

<bean id="messageListener" class="com.ucware.ucpo.forward.mess.MyMessageListener"/>
<bean id="messageService" class="com.ucware.ucpo.forward.jms.MessageService"/>

<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">

<constructor-arg name="name" value="FORWARD"/>
</bean>

It turned out ActiveMQ does not support channels. So I had to define a <jms:lintener-container> and <jms:listener> to be able to consume the messages. In addition I created a message producer that sends messages to the existing ActiveMQ queue.

    <!-- RECEIVER -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/>

<bean id="messageListener" class="com.ucware.ucpo.forward.jms.ProductMessageListener"/>

<jms:listener-container connection-factory="connectionFactory" concurrency="2" acknowledge="auto">
    <jms:listener destination="FORWARD" ref="messageListener" method="onMessage"/>
</jms:listener-container>

 <!-- SENDER -->

<!-- A cached connection to wrap the ActiveMQ connection --> 

<bean id="cachedConnectionFactory" 
     class="org.springframework.jms.connection.CachingConnectionFactory" 
     p:targetConnectionFactory-ref="connectionFactory"      
     p:sessionCacheSize="10" />

<!-- A destination in ActiveMQ --> 

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

<!-- A JmsTemplate instance that uses the cached connection and destination --> 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM