中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

rabbitmq的direct和fanout模式-創(chuàng)新互聯(lián)

rabbitmq的direct和fanout模式

流程:rabbitmq的direct和
fanout模式
  1. 請(qǐng)求鏈接并發(fā)送mq
  2. 消費(fèi)者獲取mq并處理

請(qǐng)求鏈接并發(fā)送mq
controller(IndexController):

創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)米脂,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):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<String> message = new Message<>();
	message.setAction("DIRECT_TEST");
	message.setData("DIRECT");
	log.info("Message:"+message.getData());
	templateDirect.convertAndSend(message);
}

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

消費(fèi)者獲取mq并處理:
1.direct 方式

<bean id="directListener" class="com.calron.rabbitmq.listener.DirectListener" />
<rabbit:direct-exchange name="directExchange"
	auto-declare="true" auto-delete="false" durable="true" declared-by="mqAdmin">
	<rabbit:bindings>
		<rabbit:binding queue="directQueue" key="directRoutekey" />
	</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:queue name="directQueue"
	auto-declare="true" auto-delete="false"
	durable="true" declared-by="mqAdmin" />	

<rabbit:template id="direct-template"
	connection-factory="mqConnectionFactory"
	queue="directQueue"
	exchange="directExchange"
	routing-key="directRoutekey"
	message-converter="mqMessageConvertor" />		
	
<rabbit:listener-container connection-factory="mqConnectionFactory" message-converter="mqMessageConvertor">
	<rabbit:listener ref="directListener" method="accept"
		response-exchange="directExchange"
		response-routing-key="directRoutekey"
		queue-names="directQueue" />
</rabbit:listener-container>

DirectListener:

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

2.fanout方式

<bean id="fanoutListener1" class="com.calron.rabbitmq.listener.FanoutListener1" />
<bean id="fanoutListener2" class="com.calron.rabbitmq.listener.FanoutListener2" />
<rabbit:fanout-exchange name="fanoutExchange"
	auto-declare="true" auto-delete="false" durable="true" declared-by="mqAdmin">
	<rabbit:bindings>
		<rabbit:binding queue="fanoutQueue1"/>
		<rabbit:binding queue="fanoutQueue2"/>
	</rabbit:bindings>
</rabbit:fanout-exchange>
<rabbit:queue name="fanoutQueue1"
	auto-declare="true" auto-delete="false"
	durable="true" declared-by="mqAdmin" />	
<rabbit:queue name="fanoutQueue2"
	auto-declare="true" auto-delete="false"
	durable="true" declared-by="mqAdmin" />	

<rabbit:template id="fanout-template"
	connection-factory="mqConnectionFactory"
	queue="fanoutQueue"
	exchange="fanoutExchange"
	routing-key="fanoutRoutekey"
	message-converter="mqMessageConvertor" />		
	
<rabbit:listener-container connection-factory="mqConnectionFactory" message-converter="mqMessageConvertor">
	<rabbit:listener ref="fanoutListener1" method="accept"
		queue-names="fanoutQueue1" />
	<rabbit:listener ref="fanoutListener2" method="accept"
		queue-names="fanoutQueue2" />	
</rabbit:listener-container>

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");
}

運(yùn)行代碼,訪問鏈接,查看結(jié)果。
訪問/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

網(wǎng)頁標(biāo)題:rabbitmq的direct和fanout模式-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.rwnh.cn/article2/dhhioc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、ChatGPT、網(wǎng)站設(shè)計(jì)App設(shè)計(jì)、自適應(yīng)網(wǎng)站、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
曲阜市| 烟台市| 久治县| 运城市| 大同市| 青海省| 南宁市| 茂名市| 遂川县| 休宁县| 浦城县| 灌南县| 六枝特区| 保德县| 灌云县| 泌阳县| 乐至县| 柳江县| 囊谦县| 邹平县| 达州市| 辽宁省| 大宁县| 离岛区| 华安县| 莱州市| 乐亭县| 承德市| 宜州市| 万安县| 乐业县| 丘北县| 澜沧| 宁河县| 海安县| 九江县| 钟祥市| 梁平县| 定南县| 德格县| 新泰市|