之前的博客和大家分享了Rabbitmq的基本框架,及其工作原理,网址为 < http://www.cnblogs.com/jun-ma/p/4840869.html >。今天呢,想和大家一起分享一下如何把rabbitmq应用到我们的Spring工程项目中。

Rabbitmq Server Install & Config

使用Rabbitmq需要我们安装rabbitmq服务器,服务器下载地址 < http://www.rabbitmq.com/download.html >。笔者使用的是Mac OS standalone 3.5.4版本,下载完成之后,解压文件即可。

进入sbin目录,后台启动rabbitmq server

./rabbitmq-server –detached

使用ps -A| grep rabbit可以查看到当前运行的rabbitmq进程。

然后,使用rabbitmq-plugin命令使能web管理:

./rabbitmq-plugins enable rabbitmq_management

然后,我们就可以使用web界面管理我们的Rabbitmq Server,网址为:

http://localhost:15672/

用户名和密码默认为guest/guest,用户登录之后可以根据需要选择新增用户名和密码,并设置管理权限。

Spring Rabbitmq使用实例

Message Producer

生产者的Rabbitmq.xml配置,这里我们使用rabbit标签来配置rabbitmq客户端。

<?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: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/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"> <!--配置connection-factory,指定连接rabbit server参数-->
<rabbit:connection-factory id="connectionFactory" username="guest" password="guest"
host="localhost"
port="5672"
virtual-host="/"/> <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成-->
<rabbit:admin connection-factory="connectionFactory"/> <!--定义queue-->
<rabbit:queue id="com.mj.test" name="com.mj.test" durable="true" auto-delete="false" exclusive="false"/> <!-- 定义direct exchange,绑定com.mj.test queue -->
<rabbit:direct-exchange name="myChange" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="com.mj.test" key="hello"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange> <!--定义rabbit template用于数据的接收和发送-->
<rabbit:template id="myAmqpTemplate" connection-factory="connectionFactory" exchange="myChange"/>
</beans>

ApplicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 使能AOP-->
<aop:aspectj-autoproxy/>
<!-- 自动装载bean使能-->
<context:component-scan base-package="com.mj.amq"/>
<context:annotation-config/> <!-- 声明一个事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 启用annotation的事务支持 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <import resource="Rabbitmq.xml"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3307/student?useUnicode=true&amp;characoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>

Producer发送数据Java代码

@Service
public class MessageSender { @Resource(name="myAmqpTemplate")
AmqpTemplate amqpTemplate; public void sendMessage(Object message){
amqpTemplate.convertAndSend("hello",message);
}
}

Message Consumer

Rabbitmq.xml配置

Rabbitmq接收端需要配置Connection-Factory实例,监听的队列,以及Listener-container。同时,我们需要创建监听队列的java bean。这里我们使用的是spring rabbitmq提供的异步MessageListener接口,consumer的业务逻辑在onMessage中实现。

<?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: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/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"> <rabbit:connection-factory id="connectionFactory" username="guest" password="guest"
host="localhost"
port="5672"
virtual-host="/"/> <rabbit:queue id="com.mj.test" name="com.mj.test" durable="true" auto-delete="false" exclusive="false"/> <bean id="messageReceiver" class="com.mj.amq.MessageReceiver"></bean> <rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener queues="com.mj.test" ref="messageReceiver"/>
</rabbit:listener-container> </beans>

ApplicationContext.xml和producer的一致

Consumer侦听mq消息代码

public class MessageReceiver implements MessageListener {

    public void onMessage(Message message) {
System.out.println(message);
} public static void main(String[]args){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
}
}

测试

启动MessageReceiver的main函数,运行producer的单元测试,会在MessageReceiver的concole端看到接收都到的数据。同时登陆rabbitmq客户端可以看到我们配置的Exchange和queue信息。


public class MessageSenderTest { private ApplicationContext context = null; @Before
public void setUp() throws Exception {
context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
} @Test
public void should_send_a_amq_message() throws Exception {
MessageSender messageSender = (MessageSender) context.getBean("messageSender");
messageSender.sendMessage("Hello, I am amq sender");
}
}

Conclusion

本文和大家分享了一个Spring rabbitmq代码实例,文中给出了完整的xml配置和java代码,希望能够给大家带来一些帮助。

Spring Rabbitmq HelloWorld实例的更多相关文章

  1. 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例

    第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...

  2. JMS【三】--ActiveMQ简单的HelloWorld实例

    第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...

  3. SpringMVC HelloWorld实例开发及部署

    SpringMVC HelloWorld实例开发及部署 2017-01-24 目录 1 Tomcat及Eclipse Tomcat插件安装配置  1.1 Tomcat的安装  1.2 Eclipse ...

  4. SpringMVC之HelloWorld实例

    1.1 Helloworld实例的操作步骤  1. 加入jar包 2. 配置dispatcherServlet 3. 加入Spring配置文件 4. 编写请求处理器 并表示为处理器 5. 编写视图 1 ...

  5. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

  6. 【转】深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例

    这篇博文,我们使用ActiveMQ为大家实现一种点对点的消息模型.如果你对点对点模型的认识较浅,可以看一下第一篇博文的介绍. JMS其实并没有想象的那么高大上,看完这篇博文之后,你就知道什么叫简单,下 ...

  7. Spring学习--HelloWorld

    Spring: Spring 是一个开源框架. Spring 是为简化企业级应用开发而生,使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. Spring 是一 ...

  8. 转载:Spring+EhCache缓存实例

    转载来自:http://www.cnblogs.com/mxmbk/articles/5162813.html 一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干 ...

  9. Spring+EhCache缓存实例

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

随机推荐

  1. ExtJS基础知识总结:自定义日历和ComboBox控件(二)

    概述 1.ExtJS 5不支持日期选择框中只选择年月,为了满足ExtJs5可以实现选择年月的功能,查询网上资料,整理出来了相应的处理方式,最终实现的效果如下图: 2.ExtJS 控件丰富,如果需要实现 ...

  2. C++ 系列:socket 资料收集

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  3. 基于ZooKeeper的分布式锁和队列

    在分布式系统中,往往需要一些分布式同步原语来做一些协同工作,上一篇文章介绍了Zookeeper的基本原理,本文介绍下基于Zookeeper的Lock和Queue的实现,主要代码都来自Zookeeper ...

  4. css sprite css雪碧图生成工具

    最新地址:http://www.cnblogs.com/wang4517/p/4476758.html

  5. vmware 中ubuntu客户机 安装vmware tool vmhgfs 共享文件夹失败处理

    vmware版本:10.0.0 build-1295980 ubuntu版本:3.13.0-62-generic 先安装的vmware workstation自带光盘中vmare tools包,安装完 ...

  6. WebGL入门教程(四)-webgl颜色

    前面文章: WebGL入门教程(一)-初识webgl WebGL入门教程(二)-webgl绘制三角形 WebGL入门教程(三)-webgl动画 颜色效果图: 操作步骤: 1.创建HTML5 canva ...

  7. Linux学习笔记(16)-多线程

    什么是多线程?或者说,什么是线程? 按照书本上的描述,所谓线程,便是包含在进程内部的顺序执行流,是进程中实际运作的单位,也是操作系统能够进行调度的最小单位. 一个进程中可以并发多条线程,每条线程并行执 ...

  8. startssl申请配置免费https证书

    之前给业务配置都是在沃通上申请免费证书,而后通过反向代理层的Nginx进行https认证. 今天来了个新需求,要求域名直接解析至阿里云SLB.https配置需要通过阿里云的控制台部署这倒无所谓,只是在 ...

  9. ACM ICPC Vietnam National Second Round

    A. Stock Market 枚举哪一天买入,哪一天卖出即可. #include<cstdio> #include<algorithm> using namespace st ...

  10. Euler猜想

    这是从http://duodaa.com/blog/index.php/archives/538/截得图,以下是代码 package math; import java.math.BigDecimal ...