Spring boot使用Rabbitmq注解

三个注解:

@EnableRabbit
@RabbitListener
@RabbitHandler

@EnableRabbit

@EnableRabbit和@Configuration一起使用,可以加在类或者方法上,这个注解开启了容器对注册的bean的@RabbitListener检查。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(RabbitBootstrapConfiguration.class)
public @interface EnableRabbit {
}

从源代码上可以看出这个注解提供了很简单的功能就是引入了另一个配置类:RabbitBootstrapConfiguration。该类注册了两个bean:一个BeanPostProcessor一个ListenerEndpointRegistry。

注册的BeanPostProcessor则会在bean初始化之后扫描@RabbitListener和@RabbitHandler注解。

@Configuration
public class RabbitBootstrapConfiguration {

    @Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public RabbitListenerAnnotationBeanPostProcessor rabbitListenerAnnotationProcessor() {
        return new RabbitListenerAnnotationBeanPostProcessor();
    }

    @Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME)
    public RabbitListenerEndpointRegistry defaultRabbitListenerEndpointRegistry() {
        return new RabbitListenerEndpointRegistry();
    }

}

@RabbitListener

@RabbitListener用于注册Listener时使用的信息:如queue,exchange,key、ListenerContainerFactory和RabbitAdmin的bean name。

@RabbitListener(containerFactory = "rabbitListenerContainerFactory", bindings = @QueueBinding(
        value = @Queue(value = "${mq.config.queue}", durable = "true"),
        exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
        key = "${mq.config.key}"), admin = "rabbitAdmin")

扫描到bean带有该注解后,首先会将注解的内容封装到Endpoint对象中并和ListenerContainerFactory的实例一起添加到上面的RabbitListenerEndpointRegistry实例中。添加的时候会创建相应的ListenerContainer实例并添加Listener对象。

RabbitListenerAnnotationBeanPostProcessor通过RabbitListenerEndpointRegistrar间接持有RabbitListenerEndpointRegistry实例。

@RabbitHandler

@RabbitListener 和 @RabbitHandler结合使用,不同类型的消息使用不同的方法来处理。

public class CommandListener{

    @RabbitHandler
    public void handler1(ApiMessage msg){
        System.out.println(msg);
    }

    @RabbitHandler
    public void handler2(Map msg){
        System.out.println(msg);
    }
}

可能遇到的问题

AMQP错误:ACCESS_REFUSED

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - operation not permitted on the default exchange, class-id=50, method-id=20)

出现这个问题的原因是因为ListenContainer的auto-declare默认为true,container会使用RabbitAdmin去重新声明Queue、Exchange、Binding等对象。如果没有配置RabbitAdmin则会报ACCESS_REFUSED的错误,看官方解释:

Starting with version 1.4, SimpleMessageListenerContainer has this new property.
When set to true (default), the container will use a RabbitAdmin to redeclare all AMQP objects (Queues, Exchanges, Bindings), if it detects that at least one of its queues is missing during startup, perhaps because it’s an auto-delete or an expired queue, but the redeclaration will proceed if the queue is missing for any reason. To disable this behavior, set this property to false. Note that the container will fail to start if all of its queues are missing.

在spring-boot中,使用@EnableRabbit,需要在context添加ListenerContainerFactory。但是ListenerContainerFactory没有提供设置container的autoDeclare属性的接口,也没办法为ListenerContainer实例注入RabbitAdmin实例。

解决方案是为@RabbitListener指定RabbitAdmin实例的bean name

@RabbitListener(containerFactory = "rabbitListenerContainerFactory", bindings = @QueueBinding(
        value = @Queue(value = "${mq.config.queue}", durable = "true"),
        exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
        key = "${mq.config.key}"), admin = "rabbitAdmin")


  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot提供了RabbitMQ的集成,可以方便地使用RabbitMQ来实现消息队列的功能。 以下是使用Spring BootRabbitMQ实现消息队列的基本步骤: 1. 添加RabbitMQ依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置RabbitMQ连接信息 在application.properties文件中添加以下配置: ``` spring.rabbitmq.host=your_host spring.rabbitmq.port=your_port spring.rabbitmq.username=your_username spring.rabbitmq.password=your_password ``` 3. 创建消息发送者 创建一个消息发送者类,通过注入RabbitTemplate来实现消息发送: ``` @Service public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void send(String message) { rabbitTemplate.convertAndSend("exchange_name", "routing_key", message); } } ``` 4. 创建消息接受者 创建一个消息接受者类,通过注入@RabbitListener注解来实现消息接收: ``` @Service public class MessageReceiver { @RabbitListener(queues = "queue_name") public void receive(String message) { System.out.println("Received message: " + message); } } ``` 5. 发送消息 在需要发送消息的地方,注入MessageSender并调用send方法: ``` @Autowired private MessageSender messageSender; public void sendMessage(String message) { messageSender.send(message); } ``` 6. 启动应用程序 启动应用程序后,消息发送者发送消息,消息接受者监听队列并接收消息。 以上就是使用Spring BootRabbitMQ实现消息队列的基本步骤。需要注意的是,RabbitMQ是一个分布式消息队列系统,需要正确配置连接信息和队列信息才能保证消息的正确传递。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值