依赖包

        <dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.6.8.RELEASE</version>
</dependency>

spring-xxx.xml 配置文件

    <bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="${mq.user}" />
<property name="password" value="${mq.pwd}" />
<property name="host" value="${mq.address}" />
<property name="port" value="${mq.port}" />
<property name="virtualHost" value="${mq.vhost}" />
</bean> <bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean> <bean id="serializerMessageConverter"
class="org.springframework.amqp.support.converter.SimpleMessageConverter" /> <!-- 创建rabbitTemplate 消息模板类 -->
<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory" />
<property name="exchange" value="${mq.exchange}" />
<property name="routingKey" value="${mq.routingKey}" />
<property name="queue" value="${mq.queue}" />
<property name="messageConverter" ref="serializerMessageConverter" />
</bean> <bean id="queue" class="org.springframework.amqp.core.Queue">
<constructor-arg index="0" value="${mq.queue}"></constructor-arg>
<constructor-arg index="1" value="true"></constructor-arg>
<constructor-arg index="2" value="false"></constructor-arg>
<constructor-arg index="3" value="false"></constructor-arg>
</bean>
<!--
<bean id="directExchange" class="org.springframework.amqp.core.DirectExchange">
<constructor-arg index="0" value="${mq.routingKey}"></constructor-arg>
<constructor-arg index="1" value="true"></constructor-arg>
<constructor-arg index="2" value="false"></constructor-arg>
</bean>

<util:map id="arguments">
</util:map>
<bean id="binding" class="org.springframework.amqp.core.Binding">
<constructor-arg index="0" value="${mq.queue}"></constructor-arg>
<constructor-arg index="1" value="QUEUE"></constructor-arg>
<constructor-arg index="2" value="${mq.exchange}"></constructor-arg>
<constructor-arg index="3" value="${mq.routingKey}"></constructor-arg>
<constructor-arg index="4" value="#{arguments}"></constructor-arg>
</bean>

-->
<bean id="rmqProducer" class="com.xxxx.RmqProducer">
<property name="rabbitTemplate" ref="rabbitTemplate" />
</bean>
<bean id="rmqConsumer" class="com.xxxx.RmqConsumer" />
<bean id="messageListenerAdapter"
class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="rmqConsumer" />
<property name="defaultListenerMethod" value="rmqConsumeMessage"></property>
<property name="messageConverter" ref="serializerMessageConverter"></property>
</bean> <bean id="listenerContainer"
class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queues" ref="queue"></property>
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListener" ref="messageListenerAdapter"></property>
</bean>

profile.xml 中相关配置

mq.address=xxxx
mq.exchange=xxxxx
mq.routingKey=xxx
mq.queue=xxx
mq.port=5672
mq.user=xxxx
mq.pwd=xxxx
mq.timeout=5000
mq.vhost=lms

相关类文件

public class RmqProducer {
private static final Logger LOGGER = LoggerFactory.getLogger(RmqConsumer.class); private RabbitTemplate rabbitTemplate; /**
* 发送信息
*/
public void sendMessage(RabbitMessage msg) {
try {
// 发送信息
rabbitTemplate.convertAndSend(msg);
} catch (Exception e) {
LOGGER.error("rmq消费者任务处理出现异常", e);
}
} public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
}
public class RmqConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(RmqConsumer.class); public void rmqConsumeMessage(Object obj) {
LOGGER.info("rmq 消费者任务:{}", JSON.toJSONString(obj));
// TODO 具体的消费策略
}
}

使用

    • 使用时只需要注入相应的bean即可使用
    • 如果有多个队列,注意以下Bean的定义
    1. rmqProducer
    2. rmqConsumer
    3. messageListenerAdapter
    4. LisetenerContainer

备注

  • 如果已经在rabbit的图形化界面bind相关的exchange 和 quene,橙色斜体加粗子部分可以省略;

Spring MVC 接入 rabbitMQ的更多相关文章

  1. 【RabbitMQ系列】 Spring mvc整合RabbitMQ

    一.linux下安装rabbitmq 1.安装erlang环境 wget http://erlang.org/download/otp_src_18.2.1.tar.gz tar xvfz otp_s ...

  2. spring mvc 接入cas登录

    费劲千辛万苦开发出来的系统要接入sso 让我头大还好有大佬帮忙 首先在配置文件中写入启动的ip地址 HOST=http://127.0.0.1:8080/ 地址写你的地址和端口 然后在pom文件中导入 ...

  3. 实战 Spring MVC接入支付宝即时到账 (部分代码)

    下面就拿我项目中的部分代码来实践一下. 支付请求 首先,是提交表单 fund.jsp(这里我表单只需要用户填交易金额,其他的订单号之类的全部后台生成) <form id="deposi ...

  4. 彻底解决Spring mvc中时间的转换和序列化等问题

    痛点 在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8的新特性java.time包下的具体类型参数来直接接收. 我们使用含有java.time封装类型的 ...

  5. 彻底解决Spring mvc中时间类型的转换和序列化问题

    在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8时间包下的具体类型参数来直接接收.同时还有一系列的序列化 .反序列化问题,在返回前端带时间类型的同样会出 ...

  6. Spring MVC 的 XML 配置方式

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/ ...

  7. Spring MVC 文件上传 & 文件下载

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...

  8. Spring MVC 的 Java Config ( 非 XML ) 配置方式

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml web/pom.xml web.xml WebInitializer.java ...

  9. 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】

    这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...

随机推荐

  1. 让VS2010也支持html5和css3语法验证

    让VS2010也支持html5和css3语法验证 步骤: 首先打开VS2010或者可自行下载均可,我这里是利用VS的扩展器 弹出如下画面,然后选在,联机库,在右上角输入css3,即可看到下面,然后选中 ...

  2. hdu 3484 Interviewe RMQ+二分

    #include <cstdio> #include <iostream> #include <algorithm> using namespace std; + ...

  3. 最小公倍数的最小和(Minimum Sum LCM )

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u ...

  4. Centos 6.x 搭建 Zabbix Agent 客户端

    如需搭建zabbix server端,请参考:Zabbix-Server配置 环境: Zabbix-Server: Centos 6.8   IP:192.168.126.129    #Zabix- ...

  5. Selenium | 基础入门 | 截屏并保存于本地

    可先参考   Selenium | 基础入门 | 利用Xpath寻找用户框 核心代码: //截屏操作 File srcFile = ((TakesScreenshot)driver).getScree ...

  6. nginx命令介绍

    [root@webmaster nginx]# ./sbin/nginx -h nginx version: nginx/1.12.2 Usage: nginx [-?hvVtTq] [-s sign ...

  7. JAVA学习笔记(一)配置环境

    java语言的两种机制: Java 的虚拟机机制(JVM):虚拟机机制保证Java程序的跨平台特性. Java 的垃圾回收机制:垃圾回收机制保证Java程序更安全.更高效. 环境搭配:安装JDK和JR ...

  8. Codeforces Round #544 (Div. 3) D. Zero Quantity Maximization

    链接:https://codeforces.com/contest/1133/problem/D 题意: 给两个数组a,b. 同时ci = ai * d + bi. 找到一个d使c数组中的0最多. 求 ...

  9. Random Query CodeForces - 846F

    题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...

  10. uva 6910 - Cutting Tree 并查集的删边操作,逆序

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...