RabbitMQ与spring集成,配置完整的生产者和消费者
2019-08-16 11:59:39来源:博客园 阅读 ()
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+"------->>>>>");
}
}
原文链接:https://www.cnblogs.com/alterem/p/11306971.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash