RabbitMQ与Spring集成配置

1.引入相关jar包


//RabbitMQ

compile group: ‘org.springframework.amqp‘, name: ‘spring-rabbit‘, version: ‘1.6.6.RELEASE‘
compile group: ‘org.springframework.integration‘, name: ‘spring-integration-amqp‘, version: ‘4.3.5.RELEAS

生产者配置

2.实现一个消息处理器,继承自org.springframework.amqp.core.MessageListener

public class AmqpMsgListener implements MessageListener {

@Override public void onMessage(Message message)    {        System.out.println(message.toString());   }}

3.rabbit-producer.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:p="http://www.springframework.org/schema/p" xmlns:int="http://www.springframework.org/schema/integration" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration   http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd   http://www.springframework.org/schema/integration/amqp   http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd"> <!--连接工厂--> <rabbit:connection-factory id="connectionFactory" host="{ip地址}" port="{端口}" username="{用户名}" password="{密码}" publisher-confirms="true"/> <!--创建队列--> <rabbit:queue name="queue.test" durable="true" exclusive="false" auto-delete="false" /> <!--创建分发交换器--> <rabbit:direct-exchange name="exchange.directTest" durable="true"> <rabbit:bindings> <rabbit:binding key="foo.bar" queue="queue.test"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="exchange.directTest" routing-key="foo.bar" message-converter="jsonMessageConverter" confirm-callback=""/> <rabbit:admin connection-factory="connectionFactory" id="adminId"/> <!-- 配置exchange,不同的exchange会影响消息分发策略 --> <!-- 消息对象json转换类 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

</beans>

消费者配置

4.rabbit-customer.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:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!-- rabbitmq连接配置 --> <rabbit:connection-factory id="connectionFactory1" host="{ip地址}" port="{端口}" username="{用户名}" password="{密码}" publisher-confirms="true"/>

<rabbit:admin connection-factory="connectionFactory1"/>
 <!--按项目需求配置 --> <rabbit:listener-container connection-factory="connectionFactory1" acknowledge="auto"> <rabbit:listener ref="amqpMsgListener" queues="queue.test" /> </rabbit:listener-container> <bean id="amqpMsgListener" class="com.nxin.farm.test.AmqpMsgListener"/></beans>

5.创建测试类

public class MqTest extends BaseJunit {  @Autowired private RabbitTemplate amqpTemplate;

 @Test public void testSend()    {        try {            for(int i=0;i<100;i++){                amqpTemplate.convertAndSend("nx.farm.exchange.directTest", "foo.bar", "Hello, world! send by xxxxx"); }            Thread.sleep(1000*1000); } catch (AmqpException e) {            e.printStackTrace(); } catch (InterruptedException e) {            e.printStackTrace(); }    }}
时间: 2024-08-29 02:34:02

RabbitMQ与Spring集成配置的相关文章

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

一.RabbitMQ与Spring集成  准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文件里面引入如下依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati

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

原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbirdbest/article/details/78805591 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 简介 集成示例基本目录结构 一:引入相关依赖 引入Spring核心的依赖和spring-rabbit依赖,注意sprin

apache-cxf-2.6.3 spring集成配置

apache-cxf-2.6.3 <?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:jaxws="http://cxf.apache.o

MQ原理、使用场景、IBM WebSphere MQ介绍及spring集成配置

一.MQ简介及特点 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术.排队指的是应用程序通过队列来通信.队列的使用除去了接收和发送应用程序同时执行的要求.其中较为成熟的MQ产品有IBM WebSphere MQ.RabbitMQ .ZeroMQ

RabbitMQ及Spring集成

部分转载自https://blog.csdn.net/whoamiyang/article/details/54954780 1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现. 2.应用场景 2.1异步处理 场景说明:用户注册后,需要发注册邮件和注册短信,传统的做法有两种1.串行的方式;2.并行的方式 (1)串行方式:将注册信息写入数据库后,发送注册邮件,再发送注册短信,以上三个任务全部完成后才返回给客户端. 这有一个问题是,

Redis与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:p="http://www.springframework.org/schema/p&

ibatis 开发中的经验 (三)ibatis与spring2集成配置

ibatis项目中用到了一些基本配置,需要和spring集成,看了看这些配置大部分同hibernate中是一样的,也比较好理解,只是需要他们的配置中每个类的含义,还有其中的一些细节还是需要我们了解的,知识不在多,而在不断吸收和重复,在使用和练习中加深对各种问题的理解. 读取属性文件配置 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceho

重构Mybatis与Spring集成的SqlSessionFactoryBean(上)

一般来说,修改框架的源代码是极其有风险的,除非万不得已,否则不要去修改.但是今天却小心翼翼的重构了Mybatis官方提供的与Spring集成的SqlSessionFactoryBean类,一来是抱着试错的心态,二来也的确是有现实需要. 先说明两点: 通常来讲,重构是指不改变功能的情况下优化代码,但本文所说的重构也包括了添加功能 本文使用的主要jar包(版本):spring-*-4.3.3.RELEASE.jar.mybatis-3.4.1.jar.mybatis-spring-1.3.0.jar

RabbitMQ安装和使用(和Spring集成)

一.安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 Eralng OTP For Windows (vR16B03) 运行安装 Rabbit MQ Server Windows Installer (v3.2.3) 具体操作步骤参考:在 Windows 上安装Rabbit MQ 指南 本人遇到的问题 当安装RabbitMQ后,使用rabbitmqctl