spring boot整合RabbitMQ实例详解(Fanout模式)
程序员文章站
2024-02-26 08:52:10
1.fanout exchange介绍
fanout exchange 消息广播的模式,不管路由键或者是路由模式,会把消息发给绑定给它的全部队列,如果配置了routing...
1.fanout exchange介绍
fanout exchange 消息广播的模式,不管路由键或者是路由模式,会把消息发给绑定给它的全部队列,如果配置了routing_key会被忽略。
如上图所示,即当使用fanout交换器时,他会将消息广播到与该交换器绑定的所有队列上,这有利于你对单条消息做不同的反应。
例如存在以下场景:一个web服务要在用户完善信息时,获得积分奖励,这样你就可以创建两个对列,一个用来处理用户信息的请求,另一个对列获取这条消息是来完成积分奖励的任务。
2.代码示例
1).queue配置类
fanoutrabbitconfig.java类:
package com.example.rabbitmqfanout; 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 public class fanoutrabbitconfig { //创建队列 @bean public queue amessage() { return new queue("fanout.a"); } //创建队列 @bean public queue bmessage() { return new queue("fanout.b"); } //创建队列 @bean public queue cmessage() { return new queue("fanout.c"); } //创建fanout交换器 @bean fanoutexchange fanoutexchange() { return new fanoutexchange("fanoutexchange"); } //将对列绑定到fanout交换器 @bean binding bindingexchangea(queue amessage,fanoutexchange fanoutexchange) { return bindingbuilder.bind(amessage).to(fanoutexchange); } //将对列绑定到fanout交换器 @bean binding bindingexchangeb(queue bmessage, fanoutexchange fanoutexchange) { return bindingbuilder.bind(bmessage).to(fanoutexchange); } //将对列绑定到fanout交换器 @bean binding bindingexchangec(queue cmessage, fanoutexchange fanoutexchange) { return bindingbuilder.bind(cmessage).to(fanoutexchange); } }
2).消息生产者
fanoutsender.java类:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.core.amqptemplate; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; @component public class fanoutsender { @autowired private amqptemplate rabbittemplate; public void send() { string context = "hi, fanout msg "; system.out.println("sender : " + context); this.rabbittemplate.convertandsend("fanoutexchange","", context); } }
3).消息消费者
fanoutreceivera.java类:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.rabbit.annotation.rabbithandler; import org.springframework.amqp.rabbit.annotation.rabbitlistener; import org.springframework.stereotype.component; @component @rabbitlistener(queues = "fanout.a") public class fanoutreceivera { @rabbithandler public void process(string message) { system.out.println("fanout receiver a : " + message); } }
fanoutreceiverb.java类:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.rabbit.annotation.rabbithandler; import org.springframework.amqp.rabbit.annotation.rabbitlistener; import org.springframework.stereotype.component; @component @rabbitlistener(queues = "fanout.b") public class fanoutreceiverb { @rabbithandler public void process(string message) { system.out.println("fanout receiver b: " + message); } }
fanoutreceiverc.java类:
package com.example.rabbitmqfanout.rabbitmq; import org.springframework.amqp.rabbit.annotation.rabbithandler; import org.springframework.amqp.rabbit.annotation.rabbitlistener; import org.springframework.stereotype.component; @component @rabbitlistener(queues = "fanout.c") public class fanoutreceiverc { @rabbithandler public void process(string message) { system.out.println("fanout receiver c: " + message); } }
4).测试
fanouttest.java类:
package com.example.rabbitmqfanout.rabbitmq; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; @runwith(springrunner.class) @springboottest public class fanouttest { @autowired private fanoutsender sender; @test public void fanoutsender() throws exception { sender.send(); } }
以上所述是小编给大家介绍的spring boot整合rabbitmq(fanout模式),希望对大家有所帮助
上一篇: Java微信公众平台开发(15) 微信JSSDK的使用
下一篇: Java输出打印工具类封装的实例