网站建设资讯

NEWS

网站建设资讯

rabbitmq的direct和fanout模式-创新互联

rabbitmq的direct和fanout模式

流程:rabbitmq的direct和
fanout模式
  1. 请求链接并发送mq
  2. 消费者获取mq并处理

请求链接并发送mq
controller(IndexController):

创新互联主要从事网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务米脂,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
@Autowired
private IndexService indexService;

@ResponseBody
@RequestMapping(value="/direct")
public void direct(){
	log.info("[rabbitmq][direct][start]");
	indexService.sendDirect();
	log.info("[rabbitmq][direct][end]");
}

@ResponseBody
@RequestMapping(value="/fanout")
public void fanout(){
	log.info("[rabbitmq][fanout][start]");
	indexService.sendFanout();
	log.info("[rabbitmq][fanout][end]");
}

service(IndexService):

@Resource(name = "direct-template")
private RabbitTemplate templateDirect;
@Resource(name = "fanout-template")
private RabbitTemplate fanoutDirect;

public void sendDirect(){
	Message message = new Message<>();
	message.setAction("DIRECT_TEST");
	message.setData("DIRECT");
	log.info("Message:"+message.getData());
	templateDirect.convertAndSend(message);
}

public void sendFanout(){
	Message message = new Message<>();
	message.setAction("FANOUT_TEST");
	message.setData("FANOUT");
	log.info("Message:"+message.getData());
	fanoutDirect.convertAndSend(message);
}

消费者获取mq并处理:
1.direct 方式



	
		
	

	

		
	

	

DirectListener:

@Override
public void accept(String messageContent) throws Throwable {
	log.info("direct listener");
}

2.fanout方式




	
		
		
	

	
	

		
	

	
		

FanoutListener1:

@Override
public void accept(String messageContent) throws Throwable {
	log.info("fanout listener11111111111111");
}

FanoutListener2:

@Override
public void accept(String messageContent) throws Throwable {
	log.info("fanout listener222222222222222");
}

运行代码,访问链接,查看结果。
访问/direct

[com.controller.IndexController]-[INFO] [rabbitmq][direct][start]
[com.service.IndexService]-[INFO] Message:DIRECT
[com.controller.IndexController]-[INFO] [rabbitmq][direct][end]
[com.listener.DirectListener]-[INFO] direct listener

访问/fanout

[com.controller.IndexController]-[INFO] [rabbitmq][fanout][start]
[com.service.IndexService]-[INFO] Message:FANOUT
[com.controller.IndexController]-[INFO] [rabbitmq][fanout][end]
[com.listener.FanoutListener2]-[INFO] fanout listener222222222222222
[com.listener.FanoutListener1]-[INFO] fanout listener11111111111111

网页名称:rabbitmq的direct和fanout模式-创新互联
文章网址:http://njwzjz.com/article/dhhioc.html