请求链接并发送mq
controller(IndexController):
@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);
}
消费者获取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");
}
运行代码,访问链接,查看结果。
访问/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模式-创新互联
转载注明:https://www.cdcxhl.com/article2/dhhioc.html
成都网站建设公司_创新互联,为您提供电子商务、ChatGPT、网站设计、App设计、自适应网站、虚拟主机
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联