您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家介绍RabbitMQ与spring集成以及配置完整的生产者和消费者,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/frankyou/p/5283539.html
下面是rabbitMQ和spring集成的配置,我配置了二种ExCahange: topicExchange和directExChange
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <description>Spring公共配置 </description> <!--配置connection-factory,指定连接rabbit server参数 --> <rabbit:connection-factory id="connectionFactory" host="${rabbit.connect.host}" port="${rabbit.connect.port}" username="${rabbit.connect.username}" password="${rabbit.connect.password}" channel-cache-size="${rabbit.connect.channelCacheSize}" publisher-returns="true" publisher-confirms="true" /> <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 --> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" /> <!--定义queue 说明:durable:是否持久化 exclusive: 仅创建者可以使用的私有队列,断开后自动删除 auto_delete: 当所有消费客户端连接断开后,是否自动删除队列--> <rabbit:queue name="mq.asdf" durable="true" auto-delete="false" exclusive="false" /> <rabbit:queue name="mq.asdf2" durable="true" auto-delete="false" exclusive="false" /> <rabbit:queue name="mq.qwer" durable="true" auto-delete="false" exclusive="false" /> <!--定义topic-exchange --> <rabbit:topic-exchange name="mq.asdfExChange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding queue="mq.asdf" pattern="mq.asdf.send"></rabbit:binding> <rabbit:binding queue="mq.asdf2" pattern="mq.asdf2.send"></rabbit:binding> <rabbit:binding queue="mq.asdf2" pattern="mq.asdf.send"></rabbit:binding> </rabbit:bindings> </rabbit:topic-exchange> <!--定义direct-exchange --> <rabbit:direct-exchange name="mq.qwerExChange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding queue="mq.qwer" key="mq.qwer.send" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 消息接收者 --> <bean id="asdfConsumer" class="com.demo.action.AsdfConsumer"></bean> <bean id="asdfConsumer2" class="com.demo.action.AsdfConsumer2"></bean> <bean id="qwerConsumer" class="com.demo.action.QwerConsumer"></bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 --> <rabbit:listener-container connection-factory="connectionFactory" > <rabbit:listener queues="mq.asdf" ref="asdfConsumer"/> </rabbit:listener-container> <rabbit:listener-container connection-factory="connectionFactory" > <rabbit:listener queues="mq.asdf2" ref="asdfConsumer2"/> </rabbit:listener-container> <rabbit:listener-container connection-factory="connectionFactory" > <rabbit:listener queues="mq.qwer" ref="qwerConsumer"/> </rabbit:listener-container> </beans>
生成者demo例子 :
@Controller @RequestMapping("/amqpTest") public class AmqpTestController { @Autowired private AmqpTemplate amqpTemplate; @RequestMapping("/sendMsg") @ResponseBody public String sendAmqbMsg(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){ if(model!=null&&!"".equals(msg)){ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", msg); }else{ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", "hello world"); } return "success"; } @RequestMapping("/sendMsg2") @ResponseBody public String sendAmqbMsg2(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){ if(model!=null&&!"".equals(msg)){ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "这个世界很奇妙!!!"); }else{ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "这个世界很奇妙"); } return "success"; } @RequestMapping("/sendMsg3") @ResponseBody public String sendAmqbMsg3(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){ if(model!=null&&!"".equals(msg)){ amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界!!!"); }else{ amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界"); } return "success"; } }
消费者:
这里我按照配置列出来一个Demo
<bean id="asdfConsumer" class="com.demo.action.AsdfConsumer"> </bean>
消费者
路径匹配上就没有什么问题.public class AsdfConsumer implements MessageListener{
static{ System.out.println("已经依赖成功 "); } public void onMessage(Message message) { MessageProperties m=message.getMessageProperties(); // System.out.println(m); String msg= new String (message.getBody()); System.out.println("消费掉了:"+msg+"------->>>>>"); } }
关于RabbitMQ与spring集成以及配置完整的生产者和消费者就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。