Message listener adapter adapter that delegates the handling of messages to target listener methods via refection, with flexible message tyep conversion. Allows listener methods to operate on message content types, completely independent from the Rabbit API.
By default, the content of incoming Rabbit messges gets extracted before being passed into the target listener method, to let the target method operate on message content tyeps such as String or byte array instead of the raw 'Message'. Message type conversion is delegated to a Spring AMQ 'MessageConverter'. Be default, a 'SimpleMessageConverter' will be used. (If you do not want such automatic message conversijon taking palce, then be sure to set the 'setMessageConverter MessageConverter' to 'null'.)
If a target listener method returns a non-null object (typically of a message content type such as String or byte array), it will get wrapped in a Rabbit 'Message' and sent to the exchange of the incoming message with the routingKey that comes from the Rabbit ReplyTo property or via 'setResponseRoutingKey(String) spicified routingKye').
Note: The sending of response messges is only available when using the 'ChannelAwareMessageListener' entry point (typically through a Spring message listener container). Usage as 'MessageListener' does 'not' support the generation of response messages.
Find below some examples of method signaures compliant with this adapter class. This first example handles all 'Message' types and gets passed the contents of each 'Message' type as an argument. No 'Message' will be sent back as all of these methods return 'void'.
public interface MessageContentsDelegate {
void handleMessage(String text);
void handleMessage(Map map);
void handleMessage(byte[] bytes);
void handleMessage(Serializable obj);
}
This next example handle a 'Message' delegate that just consumes the 'String' contents of 'Message Messages'. Notice also how the name of the 'Message' handling method is different from the 'ORIGINAL_DEFAULT_LISTENER_METHOD original' (this will have to be configured in the attandant bean definition). Again, no 'Message' will be sent back as the method returns 'void'.
public interface TextMessageContentDelegate {
void onMessage(String text);
}
This final example illustrates a 'Message' delegate that just consumes the 'String' contents of 'Message Messages'. Notice how the return type of this method is 'String': This will result in the configured 'MessageListenerAdapter' sending a 'Message' in response.
public interface ResponsiveTextMessageContentDelegate {
String handleMessage(String text);
}
For further examples and discussion please do refer to the Spring reference documentation which describes. this class (and its attendant XML configuration) in detail.
开始解释坑爹的地方,众所周知,MessageListener接口为
public interface MessageListener {
void onMessage(Message message);
}
上面的the next example中也使用了onMessage方法,于是我以为可以直接写一个带有这个方法的pojo类让MessageListenerAdapter适配,结果很显然悲剧。注释中的“Notice also how the name of the 'Message' handling method is different from the 'ORIGINAL_DEFAULT_LISTENER_METHOD original' (this will have to be configured in the attandant bean definition).”开始没怎么明白,后来才知道“this will have to be configured in the attandant bean definition”告诉我们要使用onMessage方法,请修改Bean的配置,将ORIGINAL_DEFAULT_LISTENER_METHOD设置成'onMessage'!哎,为什么用onMessage这个容易浑淆的方法名作例子呢?
/**
* Out-of-the-box value for the default listener method: "handleMessage".
*/
public static final String ORIGINAL_DEFAULT_LISTENER_METHOD = "handleMessage";
2011.3.14 12:47 更新
如果采用java-based的配置:
package asynchronous.consumer;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ExampleAmqpConfiguration {
@Autowired
private ConnectionFactory connectionFactory;
@Bean
public SimpleMessageListenerContainer messageListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames("myqueue");
container.setMessageListener(exampleListener());
return container;
}
@Bean
public ExampleListener plainExampleListener() {
ExampleListener exampleListener = new ExampleListener();
return exampleListener;
}
@Bean
public MessageListener exampleListener() {
MessageListener listener = new MessageListenerAdapter(plainExampleListener());
return listener;
}
}
将一个简单的POJO通过MessageListenerAdapter代理成MessageListener,在注入到SimpleMessageListenerContainer。
那么XML配置又是怎样的呢?截取一个配置片段:
<bean id="textMessageContentListener"
class="stephansun.github.samples.amqp.spring.listeneradapter.TextMessageContentListener"/>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="textMessageContentListener" queues="myqueue" method="handleMessage"/>
</rabbit:listener-container>
可以看出XML配置文件不需要我们知道MessageListenerAdapter类,关键点在<rabbit:listener/>Element的method Node上,看注释:
Attribute : method
The name of the listener method to invoke. If not
specified, the target bean is supposed to implement
the MessageListener or ChannelAwareMessageListener
interface.
Data Type : string
如果没有写method,Spring会认为你ref的Bean是一个实现了MessageListener和ChanelAwareMessage接口的类,这两个接口分别有onMessage(Message message)和onMessage(Message message, Channel channel)方法。只有写了method,Spring才会调用MessageListenerAdapter来适配你的POJO类,并默认将method对应的方法变成监听消息的处理方法。默认handleMessage,所以想使用XML配置文件,即使自己写的POJO也是MessageListenerAdapter中默认的适配的handleMessage方法,也是必须在在XML中写上method="handleMessage"。
分享到:
相关推荐
这个库也包含了一些配置辅助类,帮助开发者更容易地在Spring应用上下文中配置RabbitMQ的相关bean。 再者,`amqp-client-3.5.1.jar`是RabbitMQ官方提供的Java客户端库,用于与RabbitMQ服务器进行通信。这个库包含了...
你会使用Spring AMQP的RabbitTemplate内置应用系统来发布消息和使用一个MessageListenerAdapter POJO来订阅消息 git克隆 gradle bootRun 需要 大约几十分钟 一款文本编辑器或者IDE 你也可以从这个项目中入门代码...
在"jms包下的类",可能是项目中自定义的一些消息处理类,这些类可能继承了Spring的`MessageListenerAdapter`,从而简化了消息监听器的编写。它们通常包含特定业务逻辑的方法,这些方法会在接收到消息时被调用。 ...
Spring AMQP通过`MessageListenerAdapter`和自定义监听器提供了一种优雅的方式来处理手动确认。 接下来,我们讨论外部事务。在分布式系统中,有时需要跨多个操作保持数据的一致性,这就需要用到事务。RabbitMQ本身...
这个实例可能是对原始CSDN博客文章"spring整合rabbit项目实例"的一个修正版,因为原作者提到在按照该方法整合时遇到了一些问题。我们将详细解释整合过程中的关键步骤和可能遇到的问题。 首先,确保你已经安装了...
在本文中,我们将深入探讨如何将RabbitMQ与Spring框架整合,以便在Java应用程序中实现高效的消息传递。RabbitMQ是一个流行的开源消息代理,而Spring是Java开发中的一个全面框架,两者结合可以创建灵活且可扩展的...
在IT行业中,Spring Boot是一个非常流行的微服务开发框架,它简化了Spring应用...在`demo-rabbit-spring`这个压缩包中,很可能是包含了这样一个示例项目的源代码,你可以参考其中的结构和代码来理解和实践上述知识点。
Spring框架提供了`spring-rabbit`模块,使得在Spring应用中配置和使用RabbitMQ变得非常方便。通过定义`RabbitTemplate`和`AmqpAdmin`等bean,可以轻松地发送和接收消息,管理交换器(Exchanges)、队列(Queues)和...
在Spring Boot应用中整合RabbitMQ,以确保消息100%投递,是一个关键的实践,特别是对于那些需要高可靠性和数据一致性的系统。RabbitMQ是一个流行的开源消息代理,它遵循Advanced Message Queuing Protocol (AMQP)...