简体   繁体   English

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

[英]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. 我编写了一个小测试,将消息发送到现有的ActiveMQ FORWARD队列。 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. 在下面,您将找到我两次尝试接收此消息的尝试:通过MyMessageListener和通过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. 原来ActiveMQ不支持频道。 So I had to define a <jms:lintener-container> and <jms:listener> to be able to consume the messages. 因此,我必须定义<jms:lintener-container><jms:listener>才能使用消息。 In addition I created a message producer that sends messages to the existing ActiveMQ queue. 另外,我创建了一个消息生成器,该消息生成器将消息发送到现有的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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