Spring AMQP 是基于 Spring 框架的AMQP消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO的消息监听等,简化了我们对于RabbitMQ相关程序的开发。
Spring AMQP包含一些模块,如:spring-amqp, spring-rabbit and spring-Erlang等,每个模块分别由独立的一些Jar包组成。
AbstractAdaptableMessageListener用于在spring容器接收到消息后用于处理消息的抽象基类,如果需要使用自己定义的pojoL类来实现处理消息时,必须要继承该类,并实现onMessage()方法,如下:
public class MessageReceive extends AbstractAdaptableMessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(MessageReceive.class); @编程客栈Override public void onMessage(Message message, Channel channel) throws Exception { LOGGER.info("receive message:{}", new String(message.getBody(), "UTF-8")); } }
实现了MessageListener, ChannelAwareMessageListener接口
MessageListener
@FunctionalInterface public interface MessageListener { void onMessage(Message message); }
@FunctionalInterface注解表示该接口是一php个功能类接口
ChannelAwareMessageListener
@FunctionalInterface public interface ChannelAwareMessageListener { /** * Callback for processing a received Rabbit message. * <p>Implementors are supposed to process the given Message, * typically sending reply messages through the given Session. * @param message the received AMQP message (never <code>null</code>) * @param channel the underlying Rabbit Channel (never <code>null</code>) * @throws Exception Any. */ void onMessage(Message message, Channel channel) throws Exception; }
在AbstractAdaptableMessageListener中实现了MessageListener类的onMessage(Message m编程essage)方法,其实际实现的还是ChannelAwareMessageListener类中的onMessage方法,channel设置为null
@Override public vo编程客栈id onMessage(Message message) { try { onMessage(message, null); } catch (Exception e) { throw new ListenerExecutionFailedException("Listener threw exception", e, message); } }
自定义消息处理的POJO类继承AbstractAdaptabl编程eMessageListener类后,重写onMessage方法,当spring容器接收消息后,会自动交由onMessage进行处理。
到此这篇关于侦听消息队列的Message Listener类的文章就介绍到这了,更多相关侦听消息队列Message Listener类内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论