接下来要学习的五种队列
一、springboot项目mq配置:
@Configuration public class SimpleMqConfig { //简单队列配置开始 private final static String workQunne = "helloWorld"; @Bean public Queue helloWorld() { return new Queue(workQunne); } }
二、生产者
如下同时有传递string及一个对象到队列
@Controller @RequestMapping("simpleMq") public class Producer { private Logger logger= LoggerFactory.getLogger(Producer.class); @Autowired private AmqpTemplate rabbitTemplate; @RequestMapping("/sendMessage") public void send() { String context = "hello " + new Date(); logger.info("Sender : " + context); //发送字符串 this.rabbitTemplate.convertAndSend("helloWorld", context); //发送json对象 So so=new So(); so.setOrderNum("1111"); so.setCount(10); so.setStatus(1); logger.info("Sender----so : " + so); this.rabbitTemplate.convertAndSend("helloWorld", so); } }
三、消费者
@Component public class Customer { private Logger logger= LoggerFactory.getLogger(Customer.class); @RabbitListener(queues="helloWorld") public void processA(String msg) { logger.info("ReceiveA:"+msg); } @RabbitListener(queues="helloWorld") public void processA(So so) { logger.info("ReceiveB:"+so); } }
测试:
利用postman去请求来发送消息,运行结果
总结:
在上面的一个队列中,发送了两种数据结果,一种是string,一种是对象,消费者中的两个同名方法(参数不一样)分别对其进行了处理
参考博客:https://blog.youkuaiyun.com/hellozpc/article/details/81436980#55_500
源码地址:
链接:https://pan.baidu.com/s/1NOUG32bGEQyUPgsV-LJ1Uw
提取码:ur0v