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

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。

简介

集成示例基本目录结构

一:引入相关依赖

引入Spring核心的依赖和spring-rabbit依赖,注意spring-rabbit依赖不要使用最新的版本,这里使用的1.7.5.RELEASE,使用2.0.0的会报错

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.lin</groupId>
  5. <artifactId>rabbit_c2</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <properties>
  8. <spring.version>4.2.3.RELEASE</spring.version>
  9. </properties>
  10. <dependencies>
  11. <!-- 添加Spring依赖 -->
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-core</artifactId>
  15. <version>${spring.version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-webmvc</artifactId>
  20. <version>${spring.version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-context</artifactId>
  25. <version>${spring.version}</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-context-support</artifactId>
  30. <version>${spring.version}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-aop</artifactId>
  35. <version>${spring.version}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-aspects</artifactId>
  40. <version>${spring.version}</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-tx</artifactId>
  45. <version>${spring.version}</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-jdbc</artifactId>
  50. <version>${spring.version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-web</artifactId>
  55. <version>${spring.version}</version>
  56. </dependency>
  57. <!--单元测试依赖 -->
  58. <dependency>
  59. <groupId>junit</groupId>
  60. <artifactId>junit</artifactId>
  61. <version>4.12</version>
  62. <scope>test</scope>
  63. </dependency>
  64. <!--spring单元测试依赖 -->
  65. <dependency>
  66. <groupId>org.springframework</groupId>
  67. <artifactId>spring-test</artifactId>
  68. <version>${spring.version}</version>
  69. <scope>test</scope>
  70. </dependency>
  71. <!--rabbitmq依赖 1.7.5.RELEASE, 不要使用2.x.x版本会报错-->
  72. <dependency>
  73. <groupId>org.springframework.amqp</groupId>
  74. <artifactId>spring-rabbit</artifactId>
  75. <version>1.7.5.RELEASE</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>javax.validation</groupId>
  79. <artifactId>validation-api</artifactId>
  80. <version>1.1.0.Final</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.hibernate</groupId>
  84. <artifactId>hibernate-validator</artifactId>
  85. <version>5.0.1.Final</version>
  86. </dependency>
  87. </dependencies>
  88. </project>
  89.  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

二:配置Rabbit连接参数application.properties

  1. rabbit.host=127.0.0.1
  2. rabbit.port=5672
  3. rabbit.username=guest
  4. rabbit.password=guest
  5. rabbit.vhost=/
  6.  
    • 1
    • 2
    • 3
    • 4
    • 5

三:配置Spring的配置文件

applicationContext.xml Spring总的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd"
  12. default-lazy-init="false">
  13. <description>Spring总配置文件</description>
  14. <context:component-scan base-package="com.mengday.rabbitmq" />
  15. <context:annotation-config />
  16. <context:spring-configured />
  17. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  18. <property name="ignoreResourceNotFound" value="true" />
  19. <property name="locations">
  20. <list>
  21. <value>classpath*:/application.properties</value>
  22. </list>
  23. </property>
  24. </bean>
  25. <import resource="applicationContext-rabbit.xml"/>
  26. </beans>
  27.  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

Spring对应的rabbit配置文件 applicationContext-rabbit.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/rabbit
  9. http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
  10. <!--配置connection-factory,指定连接rabbit server参数 -->
  11. <rabbit:connection-factory id="connectionFactory"
  12. host="${rabbit.host}"
  13. port="${rabbit.port}"
  14. username="${rabbit.username}"
  15. password="${rabbit.password}"
  16. virtual-host="${rabbit.vhost}"/>
  17. <!--定义rabbit template用于数据的接收和发送 -->
  18. <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="exchangeTest"/>
  19. <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
  20. <rabbit:admin connection-factory="connectionFactory" />
  21. <!--定义queue -->
  22. <rabbit:queue name="queueTest" durable="true" auto-delete="false" exclusive="false" />
  23. <!-- 定义direct exchange,绑定queueTest -->
  24. <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false">
  25. <rabbit:bindings>
  26. <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
  27. </rabbit:bindings>
  28. </rabbit:direct-exchange>
  29. <!-- 消息接收者 -->
  30. <bean id="messageReceiver" class="com.mengday.rabbitmq.MessageConsumer"></bean>
  31. <!-- queue litener 自动监听队列,当有消息到达时会通知监听在对应的队列上的监听对象-->
  32. <rabbit:listener-container connection-factory="connectionFactory">
  33. <rabbit:listener queues="queueTest" ref="messageReceiver"/>
  34. </rabbit:listener-container>
  35. </beans>
  36.  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

四:消息监听

MessageConsumer.java

  1. package com.mengday.rabbitmq;
  2. public class MessageConsumer implements MessageListener {
  3. public void onMessage(Message message) {
  4. System.out.println("Received:" + message);
  5. }
  6. }
  7.  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

五:发送消息

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { "classpath:conf/spring/applicationContext.xml" })
  3. public class SpringRabbitTest extends AbstractJUnit4SpringContextTests {
  4. @Autowired
  5. private AmqpTemplate amqpTemplate;
  6. @Test
  7. public void sendMessage(){
  8. Map<String, Object> log = new HashMap<String, Object>();
  9. log.put("level", "info");
  10. log.put("timestamp", new Date());
  11. log.put("operateId", 666);
  12. log.put("msg", "修改密码,修改前密码:123456,修改后密码:111111");
  13. amqpTemplate.convertAndSend("queueTestKey", log);
  14. }
  15. }
  16.  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

六:运行结果

示例源码下载

http://download.csdn.net/download/vbirdbest/10158927


分享一个朋友的人工智能教程。比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看。

我的微信公众号:

RabbitMQ入门教程(十六):RabbitMQ与Spring集成的更多相关文章

  1. RabbitMQ入门教程(十五):普通集群和镜像集群

    原文:RabbitMQ入门教程(十五):普通集群和镜像集群 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.c ...

  2. RabbitMQ入门教程(十四):RabbitMQ单机集群搭建

    原文:RabbitMQ入门教程(十四):RabbitMQ单机集群搭建 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...

  3. RabbitMQ入门教程(十二):消息确认Ack

    原文:RabbitMQ入门教程(十二):消息确认Ack 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csd ...

  4. RabbitMQ入门教程(十):队列声明queueDeclare

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

  5. 无废话ExtJs 入门教程十六[页面布局:Layout]

    无废话ExtJs 入门教程十六[页面布局:Layout] extjs技术交流,欢迎加群(201926085) 首先解释什么是布局: 来自百度词典的官方解释:◎ 布局 bùjú: [distributi ...

  6. RabbitMQ入门教程(十):队列声明queueDeclare(转载)

    原文转载至:https://blog.csdn.net/vbirdbest/article/details/78670550 简介本节主要讨论队列声明的各个参数 queueDeclare(String ...

  7. SpringBoot入门教程(十六)@Autowired、@Inject、@Resource

    @Resource,@Autowired,@Inject 这3种都是用来注入bean的,它们属于不同的程序中.详情参见下表: v区别 ANNOTATION PACKAGE SOURCE 作用域 实现方 ...

  8. RabbitMQ入门教程(六):路由选择Routing

    原文:RabbitMQ入门教程(六):路由选择Routing 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...

  9. RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较

    原文:RabbitMQ入门教程(十七):消息队列的应用场景和常见的消息队列之间的比较 分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 这是网上的一篇教程写的很好,不知原作 ...

随机推荐

  1. PX4学习之-uORB msg 自动生成模板解读

    最后更新日期 2019-06-22 一.前言 在 PX4学习之-uORB简单体验 中指出, 使用 uORB 进行通信的第一步是新建 msg.在实际编译过程中,新建的 msg 会转换成对应的 .h..c ...

  2. 「CEOI2008」order

    题目链接 戳我 \(Solution\) 首先看看没有租条件的怎么弄.这很显然,就是普通最小割的套路 \(s\)向每个工作连一条流量\(x\)的边,\(x\)为工作收益 每个工作向每个机器连流量为\( ...

  3. echarts之bootstrap选项卡不能显示其他标签echarts图表

    在echarts跟bootstrap选项卡整合的时候,默认第一个选中选项卡可以正常加载echarts图表,但是切换其他选项的时候不能渲染出其他选项卡echarts图表. 解决方法: 在js中添加代码: ...

  4. spring-boot:run启动时,如何带设置环境参数dev,test.

    这边在linux 启动springboot的jar包时候,多次报错 最终使用 java -jar -Dspring.profiles.active=test demo-0.0.1-SNAPSHOT.j ...

  5. SpringBoot&Dubbo&Zookeeper远程调用项目搭建

    序言 Dubbo一款分布式服务框架,作为阿里巴巴SOA服务化治理方案的核心框架,通过高性能和透明化的RPC实现服务的远程调用,对服务的负载均衡以及项目的耦合性提供很强的解决方式;具体Dubbo的介绍和 ...

  6. SpringMvc中@PathVariable注解简单的用法

    @PathVariable /** * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中. * @param id * @return */ jsp页面请求 <a h ...

  7. flutter 快速生成Widget

    快速生成对象 List.generate(20, (i){ return Text("$i"); }), 快速生成Widget ListView.builder( itemCoun ...

  8. PHP结合Ueditor并修改图片上传路径 微信小程序 拼接域名显示图片

    前言 在使用UEditor编辑器时,一般我们都是需要修改默认的图片上传路径的,下面是我整理好的修改位置和方法供大家参考. 操作 Ueditor PHP版本本身自带了一套上传程序,我们可以在此基础中,找 ...

  9. Elasticsearch 6.2.3版本 同一个index新增type报错 Rejecting mapping update to [website] as the final mapping would have more than 1 type: [blog2, blog]

    在website的index下已经存在一个名为blog的type.想在website下,新增一个名为blog2的type. 执行语句如下: PUT /website/blog2/1 { "t ...

  10. 精通CSS:高级Web标准解决方案(第二版) 不明白的地方

    P47 在图3-14中,当把框1向左浮动时,它脱离文档流并且向左移动,直到它的左边缘碰到包含框的左边缘.因为它不在处于文档流中,所以它不占据空间,实际上覆盖住了框2,使框2从视图中消失. 我的疑问是, ...