1.RabbitMQ简介

RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。
官网:http://www.rabbitmq.com/

2.maven配置

        <!--rabbit -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>

3.配置文件

rabbitmq.properties

mq.host=172.17.22.187
mq.username=remote_user
mq.password=123456

4.Spring配置

<?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:rabbit="http://www.springframework.org/schema/rabbit"
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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.3.xsd"> <context:property-placeholder location="classpath:rabbitmq.properties" /> <!--配置connection-factory,指定连接rabbit server参数-->
<rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" /> <!--通过指定下面的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" /> <!-- 接受 -->
<bean id="messageReceiver" class="com.ucs.mq.QueueListenter"></bean>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener queues="com.mj.test" ref="messageReceiver"/>
</rabbit:listener-container> </beans>

5.发送消息Producer

package com.ucs.mq;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.json.JSONSerializer; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional; import com.ucs.base.BaseTest; @Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
@ContextConfiguration(locations={"classpath:application-mq.xml"})
public class TestMQ { @Autowired
private AmqpTemplate amqpTemplate; @Test
public void send() throws Exception{ List<String> submobileList=new ArrayList<String>();
submobileList.add("1");
submobileList.add("2");
submobileList.add("3");
Map<String, Object> bodyMap = new HashMap<String, Object>();
bodyMap.put("batchNo", "递四方速递");
bodyMap.put("item", submobileList);
String jsonStr=JSONSerializer.toJSON(bodyMap).toString();
amqpTemplate.convertAndSend("hello", jsonStr);
}
}

6.异步接收消息Consumer

package com.ucs.mq;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener; public class QueueListenter implements MessageListener { @Override
public void onMessage(Message msg) {
try{
System.out.println(new String(msg.getBody(),"UTF-8"));
String str=new String(msg.getBody(),"UTF-8");
JSONObject json=JSONObject.fromObject(str); System.out.println(json.get("batchNo").toString());
JSONArray jSONArray=JSONArray.fromObject(json.get("item"));
System.out.println(jSONArray.toString());
}catch(Exception e){
e.printStackTrace();
}
}
}

7.运行

{"batchNo":"递四方速递","item":["1","2","3"]}
递四方速递
["1","2","3"]

消息队列RabbitMQ与Spring集成的更多相关文章

  1. 消息队列 RabbitMQ 与 Spring 整合使用

    一.什么是 RabbitMQ RabbitMQ 是实现 AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消 ...

  2. 消息队列RabbitMQ与Spring

    1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...

  3. .NET 开源工作流: Slickflow流程引擎高级开发(七)--消息队列(RabbitMQ)的集成使用

    前言:工作流流程过程中,除了正常的人工审批类型的节点外,事件类型的节点处理也尤为重要.比如比较常见的事件类型的节点有:Timer/Message/Signal等.本文重点阐述消息类型的节点处理,以及实 ...

  4. 消息中间件系列四:RabbitMQ与Spring集成

    一.RabbitMQ与Spring集成  准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...

  5. RabbitMQ与spring集成,配置完整的生产者和消费者

    RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/frankyou/p/5283539.html 下面是rabbitMQ和spring集成的配置,我配置了二 ...

  6. RabbitMQ入门教程(十六):RabbitMQ与Spring集成

    原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  7. C#中使用消息队列RabbitMQ

    在C#中使用消息队列RabbitMQ 2014-10-27 14:41 by qy1141, 745 阅读, 2 评论, 收藏, 编辑 1.什么是RabbitMQ.详见 http://www.rabb ...

  8. node使用消息队列RabbitMQ一

    基础发布和订阅 消息队列RabbitMQ使用 1 安装RabbitMQ服务器 安装erlang服务 下载地址 http://www.erlang.org/downloads 安装RabbitMQ 下载 ...

  9. 消息队列--RabbitMQ(一)

    1.消息队列概述 可以理解为保存消息的一个媒介/或者是个容器,与之相关有两个概念(即生产者(Publish)与消费者(Consumer)).所谓生产者,就是生产创造消息的一方,那么,消费者便是从队列中 ...

随机推荐

  1. hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)

    题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...

  2. Xcode的Architectures、Valid Architectures和Build Active Architecture Only属性(原创)

    最近xcode升级了5.1版本,升级之后程序报关于要适配arm64机器的错.之前对xcode的参数配置,一直不是很了解,但实现先面对问题了,就调查了一下并解决它. 一个一个来吧. Architectu ...

  3. SpringMVC基础学习(二)—开发Handler

    一.Handler开发         Handler的开发方式在springmvc中有多种,下面我们主要讲解三种实现方式:实现Controller接口.实现HttpRequestHandler接口. ...

  4. sql解析xml

    我们有时候需要在sql中解析xml,xml解析sql实例如下:  DECLARE @params xml  DECLARE @customparams xml = null  -- 0.解析输入参数 ...

  5. DateTime.Now的一些用法

    System.DateTime.Now.ToString("D");   //Tuesday, December 13, 2016 System.DateTime.Now.ToSt ...

  6. 0-创建scott示例数据

    CREATE TABLE dept (  deptno INT PRIMARY KEY,  dname VARCHAR(14),  loc VARCHAR(13) );   INSERT INTO d ...

  7. 京东笔试---通过考试(DP)

    题目描述      小明同学要参加一场考试,考试一共有n道题目,小明必须作对至少60%的题目才能通过考试.考试结束后,小明估算出每题作对的概率,p1,p2,...,pn,你能帮他算出他通过考试的概率吗 ...

  8. stl_config.h基本宏

    四.宏: (其实呢, 我们所有的宏都包含在了 "stl_config.h"头文件中.) //这些宏是怎么判断是否需要定义:是否有指定的宏,还有一些特定的编译器也可能支持. 4.1. ...

  9. 【国家集训队2012】tree(伍一鸣)

    Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一:  + u v c:将u到v的路径上的点的权值都加上自然数c:  - u1 v1 u2 ...

  10. nginx与apache配合反向代理技术2

    注意,上次我们只是简单的在同一台服务器模拟搭建了一个新的http服务器(启用了8080端口),使用的是apache,从而模拟了多台服务器实现的Nginx反向代理,通过Nginx向上游代理服务器发送请求 ...