使用服务或侦听器在ActiveMQ中使用消息

时间:2013-07-11 14:35:46

标签: java spring asynchronous spring-integration

我编写了一个小测试,我将消息发送到现有的ActiveMQ FORWARD队列。不幸的是,消息被发送到队列但没有收到。您将在下面找到我尝试接收此消息的两次尝试:通过MyMessageListener和MessageService。两种方法都失败了 这是我的测试:

 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);

这是我的配置文件:

<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>

1 个答案:

答案 0 :(得分:0)

原来,ActiveMQ不支持频道。所以我必须定义一个<jms:lintener-container><jms:listener>才能使用这些消息。另外,我创建了一个消息生成器,它将消息发送到现有的ActiveMQ队列。

    <!-- 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>