首先 , pom文件需要加入spring集成rabbitMq的依赖:

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

发送端spring的xml配置:

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> <!-- rabbitMQ配置 -->
<bean id="rabbitConnectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg value="192.168.1.11"/>
<property name="username" value="root"/>
<property name="password" value="lee13233"/>
<property name="channelCacheSize" value=""/>
<property name="port" value=""></property>
</bean>
<rabbit:admin connection-factory="rabbitConnectionFactory"/> <!-- autoDelete:是否自动删除 durable:持久化 -->
<rabbit:queue name="test123queue" durable="true"/>
<rabbit:queue name="test321queue" durable="true"/> <!-- topic主题 -->
<rabbit:topic-exchange name="leo.pay.topic.exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true">
<bindings>
<binding queue="test123queue" pattern="*.*.test123" />
<binding queue="test321queue" pattern="test321.#" />
</bindings>
</rabbit:topic-exchange> <!-- 创建rabbitTemplate 消息模板类 -->
<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="rabbitConnectionFactory"></constructor-arg>
</bean> </beans>

发送端的java代码:

@Test
public void testRabbitMq() throws Exception {
RabbitTemplate rabbitTemplate = (RabbitTemplate) LeoContext.getContext().getApplication().getBean("rabbitTemplate");
//第二个参数为路由key(routingKey)的值,当路由可以为test321.hello.test123时,两个消费队列都可以收到消息,当值为test321.hello.aaa时,只有绑定了test321.#的队列才可以收到消息,当值为ta1.hello.test123,只有绑定了*.*.test123的队列才可收到消息
for(int i = ; i <= ; i++) {
String str = "hello" + i;
rabbitTemplate.send("leo.pay.topic.exchange", "test321.hello.test123", new Message(str.getBytes(), new MessageProperties()));
}
}

接收端(我配置的接收端与发送端不在同一个项目)spring的xml配置:

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"
default-autowire="byName"> <!-- rabbitMQ配置 -->
<bean id="rabbitConnectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg value="192.168.1.11"/>
<property name="username" value="leo"/>
<property name="password" value="lee31211"/>
<property name="channelCacheSize" value=""/>
<property name="port" value=""></property>
</bean>
<rabbit:admin connection-factory="rabbitConnectionFactory"/> <rabbit:queue name="test123queue" durable="true" />
<rabbit:queue name="test321queue" durable="true" /> <!-- 该处是指将路由leo.pay.topic.exchange与两个队列绑定在一块,也可以在rabbitMq的控制台上手动绑定,手动绑定之后,该处代码可以省略,其实发送端已经绑定过了,也没必要绑定了,所以该代码可以省略 -->
<!-- <rabbit:topic-exchange name="leo.pay.topic.exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> -->
<!-- <bindings> -->
<!-- <binding queue="test123queue" pattern="test123.*" /> -->
<!-- <binding queue="test321queue" pattern="test321.*" /> -->
<!-- </bindings> -->
<!-- </rabbit:topic-exchange> --> <!-- 启动两个队列对应的监听(消费者) -->
<bean id="detailQueueConsumer" class="com.leo.website.cousumer.DetailQueueConsumer"></bean>
<bean id="testQueueConsumer" class="com.leo.website.cousumer.TestQueueConsumer"></bean> <!-- 将两个队列加入监听容器中,每个队列的监听都对应一个监听器 -->
<rabbit:listener-container connection-factory="rabbitConnectionFactory" concurrency= "">
<rabbit:listener queues="test123queue" ref="detailQueueConsumer" method="onMessage"/>
<rabbit:listener queues="test321queue" ref="testQueueConsumer" method="onMessage"/>
</rabbit:listener-container> </beans>

接收端java代码(只列出一个监听,另外一个类似):

public class DetailQueueConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("DetailQueueConsumer: " + new String(message.getBody()));
}
}

转载至 :http://blog.csdn.net/u012204058/article/details/54292888

基于这个基础,我们需要做一定的修改,当接收端关闭时,rabbitMq中待发送消息无法到达,当接收端启动,listener会在启动过程中便已接收到消息进行业务处理,这样存在风险,我们可以做以下改动进行优化

public class MessageReceiver implements MessageListener{

    private boolean startFlag = true;
@Override
public synchronized void onMessage(Message msg) {
try{
//刚启动系统时,延迟30秒后执行
if(startFlag){
Thread.sleep(30000);
startFlag = false;
}
System.out.println("topic.serviceMessageClientTempQueue : " + new String(msg.getBody(), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
} }
}

spring集成rabbitMq(非springboot)的更多相关文章

  1. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  2. RabbitMQ第四篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  3. spring集成RabbitMQ配置文件详解(生产者和消费者)

    1,首先引入配置文件org.springframework.amqp,如下: <dependency> <groupId>org.springframework.amqp< ...

  4. Spring 集成 RabbitMQ

    pom.xml <dependency> <groupId>org.springframework.amqp</groupId> <artifactId> ...

  5. spring集成rabbitmq

    https://www.cnblogs.com/nizuimeiabc1/p/9608763.html

  6. Spring集成RabbitMQ-使用RabbitMQ更方便

    如果提到Spring,你脑海中对他的印象还停留在SSH三大框架之一,那或许你该好好重新认识这个家伙. 在IT技术日新月异的今天,他还能让你忘不了并与他朝夕相处,他,肯定有自己的绝活.如今他早已经不是孤 ...

  7. Spring Boot实战三:集成RabbitMQ,实现消息确认

    Spring Boot集成RabbitMQ相比于Spring集成RabbitMQ简单很多,有兴趣了解Spring集成RabbitMQ的同学可以看我之前的<RabbitMQ学习笔记>系列的博 ...

  8. Spring集成RabbiMQ-Spring AMQP新特性

    上一篇<Spring集成RabbitMQ-使用RabbitMQ更方便>中,我们只需要添加响应jar的依赖,就可以写一个Spring集成RabbitMQ下非常简单收发消息的程序. 我们使用的 ...

  9. Spring 集成rabbiatmq

    pom 文件 <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artif ...

随机推荐

  1. R语言子集

    取子集方法 x[]:适用于所有r语言 x[[ ]]:适用于list或者data.frame中提取元素 x$:使用元素名做索引,提取list或者data.frame中的某个元素 注意,取出的子集数据类型 ...

  2. [CSS布局]简单的CSS三列布局

    前言 公司终于可以上外网了,近期在搞RN的东西,暂时脑子有点晕,等过段时间再来写点总结.倒是最近有个新学前端的同学经常会问一些基础知识,工作空闲写了小Demo给他看,全是很基础的知识,纯粹是顺便记录在 ...

  3. 六、实现一个小功能 todolist

    1.创建一个新的Compnent 如果是通过 cli 创建的会自动加入,如果是手动创建的,需要自己加入. 2.实现添加效果 3.实现删除按钮 4.优化,把点击 添加 改为 回车 添加 5.优化,分成“ ...

  4. 分布式架构的CAP原理

    CAP 定理的含义   一.分布式系统的三个指标 1998年,加州大学的计算机科学家 Eric Brewer 提出,分布式系统有三个指标. Consistency Availability Parti ...

  5. django 添加分页功能的包

    Django pagination based upon the core pagination module

  6. Centos7安装Nginx1.14.0

    一.官网下载 http://nginx.org/en/download.html 版本说明: Nginx官网提供了三个类型的版本 Mainline version:Mainline 是 Nginx 目 ...

  7. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  8. Linux 虚拟机通过NAT模式访问外网

    1.配置本地VM8地址   2.配置虚拟机NAT网卡:设置VM8网卡地址和Linux主机相同网段地址,网关随便设置   3.编译网卡文件 vim /etc/sysconfig/network-scri ...

  9. <HTTP权威指南>记录 ---- 网络爬虫

    网络爬虫 网络爬虫(web crawler)能够在无需人类干预的情况下自动进行一系列Web事务处理的软件程序.很多爬虫会从一个Web站点逛到另一个Web站点,获取内容,跟踪超链,并对它们找到的数据进行 ...

  10. linux配置防火墙 Centos7下 添加 端口白名单

    最近在阿里云服务器centos7上部署项目 要开启8484端口 , CentOS 7默认使用的是firewall作为防火墙 在firewall下开启端口白名单 1.查看下防火墙的状态:systemct ...