上一篇,用springboot整合了rabbitmq,并且成功实现了发送者发送消息,消费者消费消息,这一篇实现工作队列的功能
轮循分发
一对多(一个发送者,两个消费者)
RabbitConfig
@Configuration
public class RabbitConfig {
@Bean
public Queue queue(){
return new Queue("hello");
}
@Bean
public Queue queue2(){
return new Queue("hello2");
}
}
基于上一篇的rabbitconfig,新增一个hello2的队列
发送者
package com.fandy.rabbitmq.component;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @Author: Fandy
* @Date: 2020/7/16 13:54
*/
@Component
public class Sender {
@Autowired
@SuppressWarnings("all")
private AmqpTemplate amqpTemplate;
//一对一
public void send(){
String msg = "hello";
System.out.println("sender:"+msg);
amqpTemplate.convertAndSend("hello",msg);
}
//一对多
public void send2(int i){
String msg = "第"+i+"个消息";
System.out.println(msg);
amqpTemplate.