RabbitMQ的基本使用
添加依赖
org.springframework.boot
spring-boot-starter-amqp
Direct模式
配置
@Configuration
public class RabbitDirectConfig {
public final static String DIRECT_NAME = "spoon-direct";
@Bean
Queue queue(){
return new Queue("myQueue");
}
@Bean
DirectExchange directExchange(){
return new DirectExchange(DIRECT_NAME, true, false);
}
@Bean
Binding binding(){
return BindingBuilder
.bind(queue())
.to(directExchange())
.with("it");
}
}
接收
@Component
public class DirectReceiver {
@RabbitListener(queues = "myQueue")
public void handler1(String message) {
System.out.println("handler1>>> " + message);
}
}
发送
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void sendDirect() {
rabbitTemplate.convertAndSend("myQueue", " hello");
}
Fanout模式
配置
@Configuration
public class RabbitFanoutConfig {
public final static String FANOUT_NAME = "spoon-fanout";
@Bean
Queue queueOne() {
return new Queue("queue-one");
}
@Bean
Queue queueTwo() {
return new Queue("queue-two");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(FANOUT_NAME, true, false);
}
@Bean
Binding bindingOne() {
return BindingBuilder
.bind(queueOne())
.to(fanoutExchange());
}
@Bean
Binding bindingTwo() {
return BindingBuilder
.bind(queueTwo())
.to(fanoutExchange());
}
}
接收
@Component
public class FanoutReceiver {
@RabbitListener(queues = "queue-one")
void handler1(String message){
System.out.println("handler1>>> " + message);
}
@RabbitListener(queues = "queue-two")
void handler2(String message){
System.out.println("handler2>>> " + message);
}
}
发送
@Test
void sendFanout() {
rabbitTemplate.convertAndSend(RabbitFanoutConfig.FANOUT_NAME, null," hello");
}
Topic模式
配置
@Configuration
public class RabbitTopicConfig {
public static final String TOPIC_NAME = "spoon-topic";
@Bean
TopicExchange topicExchange() {
return new TopicExchange(TOPIC_NAME, true, false);
}
@Bean
Queue xiaomi(){
return new Queue("xiaomi");
}
@Bean
Queue huawei(){
return new Queue("huawei");
}
@Bean
Queue phone(){
return new Queue("phone");
}
@Bean
Binding xiaomiBinding(){
return BindingBuilder.bind(xiaomi()).to(topicExchange()).with("xiaomi.#");
}
@Bean
Binding huaweiBinding(){
return BindingBuilder.bind(huawei()).to(topicExchange()).with("huawei.#");
}
@Bean
Binding phoneeBinding(){
return BindingBuilder.bind(phone()).to(topicExchange()).with("#.phone.#");
}
}
接收
@Component
public class TopicReceiver {
@RabbitListener(queues = "xiaomi")
void handler1(String message){
System.out.println("handler xiaomi>>> " + message);
}
@RabbitListener(queues = "huawei")
void handler2(String message){
System.out.println("handler huawei>>> " + message);
}
@RabbitListener(queues = "phone")
void handler3(String message){
System.out.println("handler phone>>> " + message);
}
}
发送
@Test
void sendTopic(){
rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPIC_NAME,"xiaomi.news","小米新闻");
rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPIC_NAME,"vivo.phone","Vivo手机");
rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPIC_NAME,"huawei.phone","华为手机");
}
Header模式
配置
@Configuration
public class RabbitHeaderConfig {
public static final String HEADER_NAME = "spoon-header";
@Bean
HeadersExchange headersExchange() {
return new HeadersExchange(HEADER_NAME, true, false);
}
@Bean
Queue queueName() {
return new Queue("name-queue");
}
@Bean
Queue queueAge() {
return new Queue("age-queue");
}
@Bean
Binding bindingName(){
Map<String, Object> map = new HashMap<>();
map.put("name","zhangxin");
return BindingBuilder.bind(queueName()).to(headersExchange()).whereAny(map).match();
}
@Bean
Binding bindingAge(){
Map<String, Object> map = new HashMap<>();
map.put("age","18");
return BindingBuilder.bind(queueAge()).to(headersExchange()).whereAny(map).match();
}
}
接收
@Component
public class HeaderReceiver {
@RabbitListener(queues = "name-queue")
public void handler1(byte[] msg){
System.out.println("HeaderReceiver name " + new String(msg));
}
@RabbitListener(queues = "age-queue")
public void handler2(byte[] msg){
System.out.println("HeaderReceiver age " + new String(msg));
}
}
发送
@Test
void sendHeader(){
Message nameMsg = MessageBuilder
.withBody("ni hao a sai li ya".getBytes())
.setHeader("name", "zhangxin").build();
Message ageMsg = MessageBuilder
.withBody("ni hao a 18 sui".getBytes())
.setHeader("age", "20").build();
rabbitTemplate.convertAndSend(RabbitHeaderConfig.HEADER_NAME, null, nameMsg);
rabbitTemplate.convertAndSend(RabbitHeaderConfig.HEADER_NAME, null, ageMsg);
}