Spring整合rabbitmq实践(二):扩展功能
Spring整合rabbitmq实践(三):源码-@RabbitListener实现过程
spring-rabbit消费过程解析及AcknowledgeMode选择
1. Rabbitmq基本概念

1.1. Rabbitmq中的各个角色
producer:消息生产者;
consumer:消息消费者;
queue:消息队列;
exchange:接收producer发送的消息按照binding规则转发给相应的queue;
binding:exchange与queue之间的关系;
virtualHost:每个virtualHost持有自己的exchange、queue、binding,用户只能在virtualHost粒度控制权限。
1.2. exchange的几种类型
fanout:
群发到所有绑定的queue;
direct:
根据routing key routing到相应的queue,routing不到任何queue的消息扔掉;可以不同的key绑到同一个queue,也可以同一个key绑到不同的queue;


topic:
类似direct,区别是routing key是由一组以“.”分隔的单词组成,可以有通配符,“*”匹配一个单词,“#”匹配0个或多个单词;

headers:
根据arguments来routing。
arguments为一组key-value对,任意设置。
“x-match”是一个特殊的key,值为“all”时必须匹配所有argument,值为“any”时只需匹配任意一个argument,不设置默认为“all”。
2. spring整合rabbitmq
通过以下配置,可以获得最基础的发送消息到queue,以及从queue接收消息的功能。
2.1. maven依赖
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${version}</version>
</dependency>
这个包同时包含了一些其它的包:spring-context、spring-tx、spring-web、spring-messaging、spring-retry、spring-amqp、amqp-client,如果想单纯一点,可以单独引入。
最主要的是以下几个包,
spring-amqp:
Spring AMQP Core.
spring-rabbit:
Spring RabbitMQ Support.
amqp-client:
The RabbitMQ Java client library allows Java applications to interface with RabbitMQ.
个人理解就是,spring-amqp是spring整合的amqp,spring-rabbit是spring整合的rabbitmq(rabbitmq是amqp的一个实现,所以可能spring-rabbit也是类似关系),amqp-client提供操作rabbitmq的java api。
目前最新的是2.0.5.RELEASE版本。如果编译报错,以下信息或许能有所帮助:
(1)
java.lang.ClassNotFoundException: org.springframework.amqp.support.converter.SmartMessageConverter
解决方案:spring-amqp版本改为2.0.5.RELEASE。
(2)
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context...
解决方案:spring-context版本改为5.0.7.RELEASE。
(3)
java.lang.NoSuchMethodError: org.springframework.util.ObjectUtils.unwrapOptional(Ljava/lang/Object;)Ljava/lang/Object
解决方案:spring-core版本改为5.0.7.RELEASE。
(4)
java.lang.NullPointerException: null
at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:60)
at org.springframework.beans.GenericTypeAwarePropertyDescriptor.<init>(GenericTypeAwarePropertyDescriptor.java:70)
at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:366)
at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:302)
解决方案:spring-beans版本改为5.0.7.RELEASE。
(5)
Caused by: java.lang.NoSuchMethodError: org.springframework.aop.framework.AopProxyUtils.getSingletonTarget(Ljava/lang/Object;)Ljava/lang/Object;
解决方案:spring-aop版本改为5.0.7.RELEASE。
总之,需要5.0.7.RELEASE版本的spring,及相匹配版本的amqp-client。
2.2. 配置ConnectionFactory
后面所讲的这些bean配置,spring-amqp中都有默认配置,如果不需要修改默认配置,则不用人为配置这些bean。后面这些配置也没有涉及到所有的属性。
这里的ConnectionFactory指的是spring-rabbit包下面的ConnectionFactory接口,不是amqp-client包下面的ConnectionFactory类。
@Configuration
public class MqProducerConfig {
@Autowired
@Bean
public ConnectionFactory amqpConnectionFactory(ConnectionListener connectionListener,
RecoveryListener recoveryListener,
ChannelListener channelListener) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses("localhost:5672");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/");
connectionFactory.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
connectionFactory.setChannelCacheSize(25);
connectionFactory.setChannelCheckoutTimeout(0);
connectionFactory.setPublisherReturns(false);
connectionFactory.setPublisherConfirms(false);
connectionFactory.addConnectionListener(connectionListener);
connectionFactory.

本文详细介绍了Spring整合rabbitmq的基础概念,包括Rabbitmq的角色、exchange类型。接着深入探讨了Spring整合rabbitmq的配置,如maven依赖、ConnectionFactory、AmqpTemplate、RabbitListenerContainerFactory的配置,以及发送和接收消息的方法。文章还讨论了消息确认机制、事务和错误处理等高级特性。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



