目录
RabbitMq实操Fanout方式
项目准备和配置相关见系列二
一、RabbitMq的config代码实现:
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class RabbitConfigFanout {
public static final String queue_A = "fanout_A";
public static final String queue_B = "fanout_B";
public static final String queue_C = "fanout_C";
public static final String exchange_fanout = "exchange_fanout";
@Bean
public Queue queueFanoutA(){
return new Queue(RabbitConfigFanout.queue_A);
}
@Bean
public Queue queueFanoutB(){
return new Queue(RabbitConfigFanout.queue_B);
}
@Bean
public Queue queueFanoutC(){
return new Queue(RabbitConfigFanout.queue_C);
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange(RabbitConfigFanout.exchange_fanout);
}
@Bean
Binding bindingExchangeA(Queue queueFanoutA, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueFanoutA).to(fanoutExchange);
}
@Bean
Binding bindingExchangeB(Queue queueFanoutB, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueFanoutB).to(fanoutExchange);
}
@Bean
Binding bindingExchangeC(Queue queueFanoutC, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueFanoutC).to(fanoutExchange);
}
}
二、生产者代码实现
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class MsgProducerFanout {
@Autowired
public RabbitTemplate rabbitTemplate;
public void sendMsg(String msg){
log.info(msg);
rabbitTemplate.convertAndSend(RabbitConfigFanout.exchange_fanout, "", msg);
}
}
三、消费者代码实现
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = RabbitConfigFanout.queue_A)
public class MsgReceiverFanoutQueueA {
@RabbitHandler
public void process(String message){
log.info("成功接收queueA的消息:"+ message);
}
}
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = RabbitConfigFanout.queue_B)
public class MsgReceiverFanoutQueueB {
@RabbitHandler
public void process(String message){
log.info("成功接收queueB的消息:"+ message);
}
}
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
@RabbitListener(queues = RabbitConfigFanout.queue_C)
public class MsgReceiverFanoutQueueC {
@RabbitHandler
public void process(String message){
log.info("成功接收queueC的消息:"+ message);
}
}
四、测试代码实现
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.maggiezhang.springbootdemo.CaseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
import java.util.Date;
@Slf4j
public class TestMsgProducerFanout extends CaseBase {
@Autowired
MsgProducerFanout msgProducerFanout;
@Test
public void testSendMsg() {
msgProducerFanout.sendMsg("这是fanout发出来的消息:" + new Date());
log.info("test:成功发送fanout消息");
}
}