http://blog.csdn.net/zhu_tianwei/article/details/40890543

以下实现使用Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。

1.所需jar包依赖

  1. <properties>
  2. <rabbitmq.version>3.0.4</rabbitmq.version>
  3. <spring.amqp.version>1.1.1.RELEASE</spring.amqp.version>
  4. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  5. <spring.version>3.1.2.RELEASE</spring.version>
  6. </properties>
  7. <dependencies>
  8. <dependency>
  9. <groupId>com.rabbitmq</groupId>
  10. <artifactId>amqp-client</artifactId>
  11. <version>${rabbitmq.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.amqp</groupId>
  15. <artifactId>spring-rabbit</artifactId>
  16. <version>${spring.amqp.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-core</artifactId>
  21. <version>${spring.version}</version>
  22. <exclusions>
  23. <!-- Exclude Commons Logging in favor of SLF4j -->
  24. <exclusion>
  25. <groupId>commons-logging</groupId>
  26. <artifactId>commons-logging</artifactId>
  27. </exclusion>
  28. </exclusions>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-context</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-test</artifactId>
  38. <version>${spring.version}</version>
  39. <scope>test</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>junit</groupId>
  43. <artifactId>junit</artifactId>
  44. <version>4.8.1</version>
  45. <scope>test</scope>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.amqp</groupId>
  49. <artifactId>spring-amqp</artifactId>
  50. <version>${spring.amqp.version}</version>
  51. <classifier>sources</classifier>
  52. <scope>compile</scope>
  53. </dependency>
  54. <dependency>
  55. <groupId>commons-lang</groupId>
  56. <artifactId>commons-lang</artifactId>
  57. <version>2.6</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.slf4j</groupId>
  61. <artifactId>slf4j-api</artifactId>
  62. <version>1.5.10</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.slf4j</groupId>
  66. <artifactId>jcl-over-slf4j</artifactId>
  67. <version>1.5.10</version>
  68. <scope>runtime</scope>
  69. </dependency>
  70. <dependency>
  71. <groupId>org.slf4j</groupId>
  72. <artifactId>slf4j-log4j12</artifactId>
  73. <version>1.5.10</version>
  74. <scope>runtime</scope>
  75. </dependency>
  76. <dependency>
  77. <groupId>log4j</groupId>
  78. <artifactId>log4j</artifactId>
  79. <version>1.2.14</version>
  80. <scope>runtime</scope>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.aspectj</groupId>
  84. <artifactId>aspectjweaver</artifactId>
  85. <version>1.6.9</version>
  86. </dependency>
  87. </dependencies>

2.application.properties配置

  1. #============== rabbitmq config ====================
  2. rabbit.hosts=192.168.101.210
  3. rabbit.username=admin
  4. rabbit.password=admin
  5. rabbit.virtualHost=/
  6. rabbit.queue=spring.queue.sync
  7. rabbit.routingKey=spring.queue.sync

3.applicationContext.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:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd"
  11. default-lazy-init="false">
  12. <description>Spring公共配置 </description>
  13. <context:component-scan base-package="cn.slimsmart.rabbitmq.demo.spring" />
  14. <aop:aspectj-autoproxy />
  15. <!-- 定义受环境影响易变的变量 -->
  16. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  17. <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
  18. <property name="ignoreResourceNotFound" value="true" />
  19. <property name="locations">
  20. <list>
  21. <!-- 标准配置 -->
  22. <value>classpath*:/application.properties</value>
  23. </list>
  24. </property>
  25. </bean>
  26. <import resource="applicationContext-rabbitmq-sync.xml"/>
  27. </beans>

4.applicationContext-rabbitmq-sync.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <!-- 创建connectionFactory -->
  9. <bean id="rabbitConnectionFactory"
  10. class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
  11. <constructor-arg value="${rabbit.hosts}"/>
  12. <property name="username" value="${rabbit.username}"/>
  13. <property name="password" value="${rabbit.password}"/>
  14. <property name="virtualHost" value="${rabbit.virtualHost}"/>
  15. <property name="channelCacheSize" value="5"/>
  16. </bean>
  17. <!-- 创建rabbitAdmin 代理类 -->
  18. <!--     <bean id="rabbitAdmin" -->
  19. <!--         class="org.springframework.amqp.rabbit.core.RabbitAdmin"> -->
  20. <!--         <constructor-arg ref="rabbitConnectionFactory" /> -->
  21. <!--     </bean> -->
  22. <!-- 创建rabbitTemplate 消息模板类 -->
  23. <bean id="rabbitTemplate"
  24. class="org.springframework.amqp.rabbit.core.RabbitTemplate">
  25. <constructor-arg ref="rabbitConnectionFactory"></constructor-arg>
  26. <property name="queue" value="${rabbit.queue}"></property>
  27. <property name="routingKey" value="${rabbit.routingKey}"></property>
  28. </bean>
  29. <!-- 声明Queue并设定Queue的名称 -->
  30. <!--     <bean id="helloWorldQueue"   -->
  31. <!--         class="org.springframework.amqp.core.Queue">   -->
  32. <!--         <constructor-arg value="${rabbit.queue}"></constructor-arg>   -->
  33. <!--     </bean>   -->
  34. </beans>

5.生产者Producer.java

  1. import org.springframework.amqp.core.AmqpTemplate;
  2. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. //生产者
  6. public class Producer {
  7. public static void main(String[] args) {
  8. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  9. AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
  10. amqpTemplate.convertAndSend("test spring sync");
  11. }
  12. }

6.消费者Consumer.java

  1. import org.springframework.amqp.core.AmqpTemplate;
  2. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. //消费者
  6. public class Consumer {
  7. public static void main(String[] args) {
  8. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  9. AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
  10. System.out.println("Received: " + amqpTemplate.receiveAndConvert());
  11. }
  12. }

运行生产者向队列中发送一条消息,再运行消费者消费消息。

(转)RabbitMQ学习之spring整合发送同步消息的更多相关文章

  1. (转) RabbitMQ学习之spring整合发送同步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...

  2. (转) RabbitMQ学习之spring整合发送异步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...

  3. (转)RabbitMQ学习之spring整合发送异步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...

  4. 【RocketMQ源码学习】- 3. Client 发送同步消息

    本文较长,代码后面给了方法简图,希望给你帮助 发送的方式 同步发送 异步发送 消息的类型 普通消息 顺序消息 事务消息 发送同步消息的时序图 为了防止读者朋友嫌烦,可以看下时序图,后面我也会给出方法的 ...

  5. ActiveMQ学习总结------Spring整合ActiveMQ 04

    通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...

  6. RabbitMQ学习笔记之五种模式及消息确认机制

    本文详细介绍简单模式Simple.工作模式Work.发布订阅模式Publish/Subscribe.Topic.Routing. Maven依赖引用 <dependencies> < ...

  7. Spring整合ActiveMQ实现消息延迟投递和定时投递

    linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...

  8. RabbitMQ走过的坑,发送的消息是乱码

    发送的消息在可视化界面中是乱码,如图: 看见这个content_tpye没有,是不是很奇怪,就是这个坑,设置下就行,看代码: @Bean Jackson2JsonMessageConverter me ...

  9. RabbitMQ学习之spring配置文件rabbit标签的使用

    下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...

随机推荐

  1. VUE常见问题解决

    1.vue模板加载顺序 computed:例如分页的配置: created:dom加载前一般用来生成dom mounted:dom加载后用来覆盖渲染或者基于dom的操作 2.关于this指向的问题 通 ...

  2. 移动端rem.js使用方法

    下面的代码一是我根据rem的使用经验,自己写的一个rem.js,发现很好用,能适用所有移动端h5页面的自适应需求: 代码一: ``` window.onload = function(){ /*720 ...

  3. 【微软2017年预科生计划在线编程笔试第二场 B】Diligent Robots

    [题目链接]:http://hihocoder.com/problemset/problem/1498 [题意] 一开始你有1个机器人; 你有n个工作; 每个工作都需要一个机器人花1小时完成; 然后每 ...

  4. foj 2173 floyd+矩阵快速幂

     Problem 2173 Nostop Accept: 52    Submit: 210 Time Limit: 3000 mSec    Memory Limit : 32768 KB  Pro ...

  5. redhat超级用户密码破解

    1. 开机在出现grub画面,按e键 2. 用上下键选中第二项(类似于kernel /boot/vmlinuz-2.4.18-14 ro root=LABEL=/) 然后按e键编辑 3. 空格sing ...

  6. 洛谷——P1616 疯狂的采药

    https://www.luogu.org/problem/show?pid=1616#sub 题目背景 此题为NOIP2005普及组第三题的疯狂版. 题目描述 LiYuxiang是个天资聪颖的孩子, ...

  7. sass基础教程

    1. 使用变量; $highlight-color: #F90; .selected { border: 1px solid $highlight-color; } //编译后 .selected { ...

  8. 例说linux内核与应用数据通信(三):读写内核设备驱动文件

    [版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet.文章仅供学习交流.请勿用于商业用途]         读写设备文件也就是调用系统调用read()和write(),系 ...

  9. Unable to read the project file &#39;client.csproj&#39;. Could not load file or assembly &#39;Microsoft.Build.En

    错误具体信息: Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft. ...

  10. SPOJ 10628 Count on a tree (lca+主席树)

    题意:给定一棵有n个结点的树,每一个点有一个权值.共同拥有m个询问.对于每一个询问(u,v,k),回答结点u至v之间第k小的点的权值. 思路:主席树+lca.首先指定一个根结点dfs一次并在此过程中建 ...