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();
 }
}
}

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

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

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

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

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

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

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

  4. 消息队列RabbitMQ与Spring集成

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

  5. rabbitmq 和Spring 集成 实现(一)

    1.增加pom.xml依赖 <!--rabbitmq消息队列依赖架包--> <dependency> <groupId>org.springframework.am ...

  6. RabbitMQ与Spring集成

    RabbitMQ服务端安装: https://blog.csdn.net/hzw19920329/article/details/53156015 与Spring集成 https://www.cnbl ...

  7. Activiti配置实例以及Spring集成配置

    public class TestDB { public static void main(String[] args) { //1. 创建Activiti配置对象的实例 ProcessEngineC ...

  8. RabbitMQ ——与Spring集成及exchange的direct、topic方式实现和简单队列实现

    程序整体结构 Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  9. ActiveMQ Spring 集成配置

    <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms&l ...

随机推荐

  1. 使用Dockerfile创建ssh服务的镜像02

    使用Dockerfile创建ssh服务的镜像02 1:创建工作目录---一个镜像的所有文件都放这个目录下 ubuntu@ubuntu:~$ mkdir sshd_ubuntu ubuntu@ubunt ...

  2. 小菜鸟之liunx

    目录 第一章:Linux简介 1 Linux特点 1 CentOS 1 第二章:Linux安装 2 Linux目录结构 2 第三章:Linux常用命令 2 Linux命令的分类 3 操作文件或目录常用 ...

  3. 玩linux笔记——持续更新

    说在最前面 centos 是基于redhat linux,所以最好的教程在红帽官网 https://access.redhat.com/documentation/en-us/red_hat_ente ...

  4. Linux下一种高效多定时器实现

    Linux下一种高效多定时器实现 作者:LouisozZ 日期:2018.08.29 运行环境说明 由于在 Linux 系统下一个进程只能设置一个时钟定时器,所以当应用需要有多个定时器来共同管理程序运 ...

  5. DVWA漏洞演练平台 - SQL注入

    SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,具体来说,它是利用现有应用程序将(恶意的)SQL命令注入到后台数据库引擎执 ...

  6. Redission

    https://github.com/redisson/redisson/wiki/6.-%E5%88%86%E5%B8%83%E5%BC%8F%E5%AF%B9%E8%B1%A1#61-%E9%80 ...

  7. 服务器上office不能正常使用?

    (1)确保dll版本和服务器上office版本一致 (2)配置dcom (3)项目配置文件中添加用户模拟语句 <system.web> <identity impersonate=& ...

  8. 检索 COM 类工厂中 CLSID 为 {13C28AD0-F195-4319-B7D7-A1BDAA329FB8} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。

    上午前客户突然来电说换了台电脑重新装的程序不能正常用,发来错误提示如图: 这错误显然不是程序错误,异常写的很清楚 ,COM组件没注册,搜一下CLSID, 原来是GridReport++ ,参考地址:  ...

  9. C#添加带验证的websevice接口

    记录一下,方便下次使用,或者能帮助到别人. 一.添加服务引用,输入WSDL文件地址. 二.代码 public TESTClient TestContext() { var binding = new ...

  10. JS的日期的知识与格式化

    需要知道的JS的日期的知识,都在这了 https://juejin.im/post/5d12b308f265da1b7c612746?utm_source=gold_browser_extension ...