rabbitMQ基本使用方法

  1. 导入rabbitMQ依赖
<!--rabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2.连接MQ

  rabbitmq:
    host: ip地址
    port: 5672
    username: guest
    password: guest
  1. 配置消息队列
@Configuration
public class DirectConfig {

    public static final String QUEUE = "notify.conversion";

    @Bean
    public Queue conversionNotifyQueue() {
        return new Queue(QUEUE,true);
    }
}

4.依赖注入

@Autowired
    private AmqpTemplate rabbitTemplate;

5.消息发送方发送消息

/*发送message到RabbitMQ*/
                rabbitTemplate.convertAndSend(DirectConfig.QUEUE, conversionTaskQueue);

6.消息接收方监听消息队列获取队列数据

@Component
public class TransformNotifyReceive {

    @Autowired
    private FileConversionService fileConversionService;
    @RabbitListener(queues = DirectConfig.QUEUE)
    public void receive(ConversionTaskQueue ConversionTaskQueue) throws Exception {
        fileConversionService.conversionFileUpload(ConversionTaskQueue);
    }
}

版权声明:本文为qq_41550306原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。