之前的博客和大家分享了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. C 标准库系列之locale.h

    locale.h 区域设置相关,主要针对时间日期.货币格式.字符控制.数字格式等以满足某区域的设置需要. locale设置类别主要包括以下几个宏定义的类别: LC_ALL:设置所有的类别: LC_CO ...

  2. Knockout.js随手记(7)

    数组元素的新增/移除事件 前两篇博客已经很清楚的知道knockout.js通过observableArray()数组元素增减,可以实时的反映在UI上.当然我们想在数组增加或移除元素时加上自定义逻辑就好 ...

  3. Java 之 I/O流

    1.流 a.分类:①字节流:InputStream.OutputStream ②字符流:Reader.Writer b.选择:①判断是 输入 还是 输出 (站在程序的立场上) ②判断是 字节 还是 字 ...

  4. STC系列STC10F芯片解密STC10L单片机破解复制技术

    STC系列STC10F芯片解密STC10L单片机破解 STC10F12XE | STC10F12 | STC10F10XE | STC10F10解密 STC10F08XE | STC10F08 | S ...

  5. trigger() --工作中问题nav样式

    自动执行某元素的某个事件 $("#div").trigger("click");  //让系统自动执行单击事件 适用于nav样式中,下面横线绝对定位于nav.o ...

  6. STL插入删除和查询测试

    博客园的markdown不知道怎么插入C++代码,只好放弃了..本文在我的blog发布:http://jwk000.github.io/2015/09/02/20150902/

  7. tomcat的安全配置(禁用http方法,部署多个应用,启用从安全cookie,指定错误页面和显示信息)

    配置版本:tomcat6 1,虚拟路径,可以配置多个host在一个tomcat中,docbase是web应用目录,此处在server.xml中添加应用配置,要让server.xml配置生效需要重启to ...

  8. 建立jackrabbit内容仓库实例

    jackrabbit需要内容仓库主目录和内容仓库配置文件这两部分的信息才能创建一个运行时内容仓库实例. 1.内容仓库主目录结构 2.Repository.xml的配置文件结构

  9. java.lang.OutOfMemoryError: PermGen space错误解决方法

    1. MyEclipse 中报 PermGen space       window--> preferences-->Myclipse-->Servers-->Tomcat- ...

  10. 通用js函数集锦<来源于网络> 【二】

    通用js函数集锦<来源于网络> [二] 1.数组方法集2.cookie方法集3.url方法集4.正则表达式方法集5.字符串方法集6.加密方法集7.日期方法集8.浏览器检测方法集9.json ...