controller层
//注入MQ
@Autowired
private AmqpTemplate amqpTemplate;//rebbitMQ
@GetMapping(“queryRedditMQ”)
@ResponseBody
public void queryRedditMQ(){
// amqpTemplate.convertAndSend(“orderQueue1”,“发起请求”);
for(int i=0;i<100;i++) {
User user = new User();
user.setId(i);
user.setName(i+“哈哈哈”);
String user1 = JSONObject.toJSONString(user);
amqpTemplate.convertAndSend(“orderQueue1”, user1);
}
//homeService.queryRedditMQ();
}
yml中添加
spring
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
导包
org.springframework.boot
spring-boot-starter-amqp
//注生产者消费者都要配yml和pom
生产者controller
//取rabbitMqorderQueue1与消费者配置中的一样
@RabbitListener(queues = “orderQueue1”)
public void test(String mes){
User user = JSONObject.parseObject(mes, User.class);
System.out.println(user);
}
配置文件
package com.jk.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queueMessage(){
return new Queue(“orderQueue1”);
}
}