1. 程式人生 > >SpringBoot框架搭建系列(六):整合RabbitMQ

SpringBoot框架搭建系列(六):整合RabbitMQ

本次我們整合RabbitMQ

1、引入依賴

<!--mq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2、在配置檔案中加入

# mq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=beck

3、訊息的生產者

@Autowired
private AmqpTemplate amqpTemplate;


/**
* 測試ma
* @return
* @throws Exception
*/
@RequestMapping("mq")
public String mqTest() throws Exception {
  amqpTemplate.convertAndSend("sendSMS", "manager", "StreamYearMq內容");
  return "ok";
}

4、訊息的消費者

package com.streamyear.course.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "eventQueue")
public class EventQueue {

    @RabbitHandler
    public void process(String manager){
        System.out.println("eventQueue收到的訊息內容: " + manager);
    }
}

備註: