SpringMVC集成RabbitMQ
来源:互联网 发布:软件测试 英文翻译 编辑:程序博客网 时间:2024/06/15 14:01
RabbitMQ在集成到SpringMVC的过程中,可能会遇到很多坑,网上很多配置都有问题,要么无法发消息,要么无法接收消息,LZ特将能够使用的配置发出来,分享给大家。
首先添加Maven依赖:
<dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.3.6.RELEASE</version> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.5.1</version> </dependency>创建配置文件,application-mq.xml
添加代码:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" > <description>rabbitmq 连接服务配置</description> <!-- 连接配置 --> <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <property name="username" value="lynn"></property> <property name="password" value="lynn"></property> <property name="virtualHost" value="/"></property> <property name="addresses" value="192.168.1.38:5672"></property> <property name="publisherConfirms" value="true"></property> </bean> <!-- spring template声明--> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg name="connectionFactory" ref="connectionFactory"/> <!--消息确认回调 --> <property name="confirmCallback" ref="producer"/></bean> <bean id="queue" class="org.springframework.amqp.core.Queue"> <constructor-arg name="name" value="spring-boot-queue"></constructor-arg> <constructor-arg name="durable" value="true"></constructor-arg> </bean> <bean id="exchange" class="org.springframework.amqp.core.DirectExchange"> <constructor-arg name="name" value="spring-boot-exchange"></constructor-arg> </bean> <bean id="binding" class="cn.sunsharp.bigdata.mq.MyBinding" init-method="createBinding"> <constructor-arg index="0" ref="queue"></constructor-arg> <constructor-arg index="1" ref="exchange"></constructor-arg> <constructor-arg index="2" value="spring-boot-routingKey"></constructor-arg> </bean> <bean name="queueListenter" class="cn.sunsharp.bigdata.mq.QueueListenter"></bean> <bean name="producer" class="cn.sunsharp.bigdata.mq.RmqProducer"></bean> <bean id="messageContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"> <property name="queues" ref="queue"></property> <property name="exposeListenerChannel" value="true"></property> <property name="maxConcurrentConsumers" value="10"></property> <property name="concurrentConsumers" value="2"></property> <property name="connectionFactory" ref="connectionFactory"></property> <property name="acknowledgeMode" value="MANUAL"></property> <property name="messageListener" ref="queueListenter"></property> </bean> </beans>
添加Java代码:
生产者:
package cn.sunsharp.bigdata.mq;import java.util.UUID;import org.springframework.amqp.rabbit.connection.ConnectionFactory;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;import org.springframework.amqp.rabbit.support.CorrelationData;import org.springframework.beans.factory.annotation.Autowired;public class RmqProducer implements ConfirmCallback{private RabbitTemplate template;@Autowiredpublic RmqProducer(RabbitTemplate template){this.template = template;} /* (non-Javadoc) * @see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue(java.lang.String, java.lang.Object) */ public void sendMessage(String content) { try { CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString()); template.convertAndSend("spring-boot-exchange", "spring-boot-routingKey",content,correlationId); } catch (Exception e) { e.printStackTrace(); } }@Overridepublic void confirm(CorrelationData correlationData, boolean ack) {System.out.println(" 回调id:" + correlationData+"ack:"+ack); }}
消费者:
package cn.sunsharp.bigdata.mq;import org.springframework.amqp.core.Message;import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;import com.rabbitmq.client.Channel;public class QueueListenter implements ChannelAwareMessageListener{@Override public void onMessage(Message message, Channel channel) throws Exception { byte[] body = message.getBody(); System.out.println("receive msg : " + new String(body)); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //确认消息成功消费 }}
//必须要添加如下这个类,否者无法接收消息。
package cn.sunsharp.bigdata.mq;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.core.Queue;public class MyBinding {private Queue queue;private DirectExchange exchange;private String routingkey;private MyBinding(Queue queue,DirectExchange exchange,String routingkey){this.queue = queue;this.exchange = exchange;this.routingkey = routingkey;}public Binding createBinding(){return BindingBuilder.bind(queue).to(exchange).with(routingkey);}}
下面测试代码:
@Resource(name="producer") private RmqProducer producer; public voidtest(){try {//发送消息 producer.sendMessage("test"); } catch (Exception e) {e.printStackTrace();}}
控制台即可打印接收到的消息。
2 0
- SpringMVC集成RabbitMQ
- SpringMVC和rabbitmq集成使用
- springmvc 集成rabbitmq,实现RPC通信方式
- RabbitMQ与SpringMVC集成并实现发送消息和接收消息(持久化)方案一
- RabbitMQ与SpringMVC集成并实现发送消息和接收消息(持久化)方案二
- RabbitMQ与SpringMVC集成并实现发送消息和接收消息(持久化)方案二
- RabbitMQ与SpringMVC集成并实现发送消息和接收消息(持久化)方案一
- spring集成rabbitmq
- Spring集成rabbitMQ
- Spring集成RabbitMQ
- spring-boot 集成 rabbitmq
- Spring boot集成RabbitMQ
- spring集成rabbitmq
- springboot10:集成RabbitMQ
- spring boot集成rabbitmq
- Srpingboot集成RabbitMq
- SpringBoot集成RabbitMQ
- Springboot集成RabbitMq
- centos mariadb(mysql)远程连接
- 使用xtrbackup/mysqldump全量备份+binlog备份恢复到最近一个时间点
- mac系统安装jdk和配置环境变量
- 当指针遇上const关键字,奇妙的化学变化~
- maven项目jsp中不显示表达式值解决
- SpringMVC集成RabbitMQ
- 【LeetCode】50. Pow(x, n)
- 数据库优化
- 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.
- 用户及文件权限管理
- 页面加载完成,输入框获得焦点失败
- 回滚机制——《亿级流量》
- Linux命令之cat命令的使用
- 棋盘覆盖问题算法分析与实现(递归)