java 如何接收convertAndSend发送的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1373514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to receive what was sent by convertAndSend?
提问by Tadeusz Kopec
I'm reading Spring Framework reference, chapter about JMS integration. There are some examples for sending text messages and asynchronously receiving them (by listeners). And there is also an example for JmsTemplatefunction convertAndSendwhich converts given object to a message. The reference says:
我正在阅读Spring Framework 参考,关于 JMS 集成的章节。有一些发送文本消息和异步接收它们的示例(通过侦听器)。还有一个将给定对象转换为消息的JmsTemplate函数示例convertAndSend。参考文献说:
By using the converter, you and your application code can focus on the business object that is being sent or received via JMS and not be concerned with the details of how it is represented as a JMS message.
通过使用转换器,您和您的应用程序代码可以专注于通过 JMS 发送或接收的业务对象,而不必关心它如何表示为 JMS 消息的细节。
But there is no example for receiving such messages. They mention function receiveAndConvertbut, unfortunately, it receives synchronously.
So how am I to receive it asynchronously? Must I be aware that when I convertAndSenda Map, the resulting message will be a MapMessage, and just check in my listener for this type of message and handle it? But they promised I'm not to be concerned with the details of how it is represented as a JMS message.
So is there a better way?
但是没有接收此类消息的示例。他们提到了函数,receiveAndConvert但不幸的是,它是同步接收的。
那么我如何异步接收它呢?我是否必须知道,当我convertAndSenda 时Map,结果消息将是 a MapMessage,并且只需检查我的侦听器以获取此类消息并处理它?但是他们承诺我不会关心它如何表示为 JMS 消息的细节。
那么有没有更好的办法呢?
采纳答案by skaffman
While JmsTemplateprovides basic synchronous receive methods, asynchronous reception is a whole lot more complicated, and is beyond the scope of JmsTemplate.
虽然JmsTemplate提供了基本的同步接收方法,但异步接收要复杂得多,超出了JmsTemplate.
Asynchronous reception of JMS messages is done in Spring using Message Listener Containers, which asynchronously take messages from the JMS destination and pass them to your application. You can plug a MessageConverterin to your message listener container via a MessageListenerAdapter(plug the converter into the adapter, plug your application's listener into the adapter, then plug the adapter into the listener container).
JMS 消息的异步接收是在 Spring 中使用Message Listener Containers 完成的,它从 JMS 目标异步获取消息并将它们传递给您的应用程序。您可以MessageConverter通过MessageListenerAdapter将 a 插入您的消息侦听器容器(将转换器插入适配器,将应用程序的侦听器插入适配器,然后将适配器插入侦听器容器)。
回答by Owen O Byrne
I know it's been a while since this was asked, but I had the same problem, solved it and wanted to give an explicit code example here.
我知道自从被问到这个问题已经有一段时间了,但我遇到了同样的问题,解决了它并想在这里给出一个明确的代码示例。
Here's my MessageListener. This implements the onMessage(Message)method to intercept messages asynchronously.
这是我的MessageListener. 这实现了onMessage(Message)异步拦截消息的方法。
package com.package.amqp;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.support.converter.JsonMessageConverter;
import com.package.model.User;
public class TestListener implements MessageListener {
public void onMessage(Message message) {
JsonMessageConverter jmc = new JsonMessageConverter();
User u = (User)jmc.fromMessage(message);
System.out.println("received: " + u.getFirstName());
}
}
The messages are then converted using the standard JsonMessageConvertorin my case as this is the messageConvertorI plugged into my rabbitTemplatebean.
然后JsonMessageConvertor在我的情况下使用标准转换消息,因为这是messageConvertor我插入到我的rabbitTemplatebean 中。
<bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="10.10.1.2"/>
<property name="username" value="guest"/>
<property name="password" value="guest"/>
</bean>
<bean class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="queueName" value="queue.helloWorld"/>
<property name="messageListener" ref="someListener"/>
</bean>
<bean id="someListener" class="com.package.amqp.TestListener"></bean>
<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="messageConverter">
<bean class="org.springframework.amqp.support.converter.JsonMessageConverter"/>
</property>
</bean>
Hope this helps someone! Owen
希望这对某人有帮助!欧文

