之前的博客和大家分享了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. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备二)

    接下来,我们继续分析UIlib.h文件中余下的文件,当然部分文件可能顺序错开分析,这样便于从简单到复杂的整个过程的里面,而避免一开始就出现各种不理解的地方. 1. UIManager.h:UI管理器, ...

  2. 提取postgresql数据库中jsonb列的数据

    ; SELECT t.errmsg,sms_records.* FROM sms_records, jsonb_to_record(result_json) AS t(errmsg text,sid ...

  3. winform快速开发平台 -> 基础组件之分页控件

    一个项目控件主要由及部分的常用组件,当然本次介绍的是通用分页控件. 处理思想:我们在处理分页过程中主要是针对数据库操作. 一般情况主要是传递一些开始位置,当前页数,和数据总页数以及相关关联的业务逻辑. ...

  4. JQuery的无缝滚动

    图片无缝向左滚动的代码如下:   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  5. Beginning Scala study note(3) Object Orientation in Scala

    1. The three principles of OOP are encapsulation(封装性), inheritance(继承性) and polymorphism(多态性). examp ...

  6. 打印自己的C代码

    #include "stdio.h" ,,,,p,,);;}

  7. 诡异的localhost无法连接

    上午试了localhost发现提示无法连接,ping了下localhost,能够ping通. 重启了Apache,还是无法解决. 试着停止了Apache服务,然后再连接localhost,发现浏览器提 ...

  8. map<虽然一直不喜欢map>但突然觉得挺好用的

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...

  9. Python3.5 Day1作业:实现用户密码登录,输错三次锁定。

    作业需求: 1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 实现思路: 1.判断用户是否在黑名单,如果在黑名单提示账号锁定. 2.判断用户是否存在,如果不存在提示账号不存在. 3. ...

  10. 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。

    PostgreSQL添加新的服务器连接时,报错: