1 配置信息
1.0 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.2 application-dev.xml
spring:
rabbitmq:
host: localhost
port: 5672
username: admin
password: admin
publisher-confirms: true
publisher-returns: true
listener:
direct:
acknowledge-mode: manual
simple:
acknowledge-mode: manual
2 配置隊列信息
package com.company.web.config;
import org.springframework.context.annotation.Configuration;
// import com.rabbitmq.client.AMQP.Queue;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
/**
* @uthor xindaqi
* @description 創建RabbitMQ消息隊列
* @date 2020-06-29
*/
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queueTest(){
return new Queue("testqueue");
}
}
3 生產者與消費者
3.1 生產者
package com.company.web.service;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
/**
* @author xindaqi
* @description 消息生產者
* @date 2020-06-29
*/
@Component
public class MQSendService {
@Autowired
private AmqpTemplate template;
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(Map params){
System.out.println("開始發送消息");
rabbitTemplate.convertAndSend("testqueue", JSON.toJSONString(params));
}
}
3.2 消費者
package com.company.web.service;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties.Cache.Channel;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
/**
* @author xindaqi
* @description 消息消費者
* @date 2020-06-29
*/
@Component
public class MQReceiveService {
@Autowired
private AmqpTemplate template;
@Autowired
private RabbitTemplate rabbitTempalte;
@RabbitListener(queues="testqueue")
public String receive(String msg){
System.out.println("msg:"+msg);
//TODO 寫入數據庫等其他操作
return msg;
}
}
4 生產者接口
package com.company.web.controller;
import java.util.Map;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.web.service.MQSendService;
/**
* @author xindaqi
* @description 生產者接口,調用接口,生成數據
* @date 2020-06-29
*/
@CrossOrigin(origins="*", maxAge=3600)
@RestController
@RequestMapping("/api/mq")
public class RabbitMQController {
@Autowired
private MQSendService mqSendService;
@RequestMapping(value="/send", method=RequestMethod.POST)
public String sendMsg(@RequestBody Map params){
mqSendService.send(params);
return "OK";
}
}
【參考文獻】
[1]https://my.oschina.net/u/2364788/blog/2875902
[2]https://www.cnblogs.com/kingsonfu/p/10599608.html