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. (C/C++学习)3.C++中cin的成员函数(cin.get();cin.getine()……)

    说明:流输入运算符,在一定程度上为C++程序的开发提供了很多便利,我们可以避免C语言那种繁琐的输入格式,比如在输入一个数值时,还需指定其格式,而cin以及cout则不需要.但是cin也有一些缺陷,比如 ...

  2. Linux运维工程师学习大纲

    linux运维课程大纲: Linux运维: Linux系统管理: Linux服务及安全管理: httpd,lamp,lnmp cache:memcached,varnish DB:mysql(mari ...

  3. php实现网站访客数量统计的方法(简单实现,不能防刷新)

    方法一: <?php function Counter()//定义函数 { $five = "00000";//声明变量,$five,$four等变量表示零的个数,放在数字前 ...

  4. nodejs获取post请求发送的formData数据

    前端post请求发送formData的类型数据时,需要服务端引入中间件body-parser,主要原因是post请求发送的数据,是在http的body里面,所以需要进行解析,否则获取不到数据(数据为空 ...

  5. Linux文字分段裁剪命令cut(转)

    Linux cut命令用于显示每行从开头算起num1到num2的文字. 语法 cut [-bn] [file] cut [-c] [file] cut [-df] [file] 使用说明: cut命令 ...

  6. MySQL备份 博客---MYSQLDBA 黄杉

    http://blog.csdn.net/mchdba/article/category/1598781

  7. 【JMeter连接SQLServer】採用window鉴权方式连接(原创)

    大家都知道Jmeter能够连接各种数据库.这方面我也不多说了,假设你还不知道怎么连接的话.能够參看我看的另外一篇博文.这边有具体的介绍 http://blog.csdn.net/lzqinfen/ar ...

  8. 【JEECG技术博文】Local storage &amp; easyui extensions

    1. Local storage背景 cookie弊端:同域内http请求都会带cookie,添加带宽和流量:有个数和限制大小(约4K). 在HTML5中,本地存储是一个window的属性.包含loc ...

  9. A - Red and Black(3.2.1)(搜索)

    A - Red and Black(3.2.1) Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  10. 日积(Running)月累(ZSSURE):Task之Cancel、OpenAccess之Delete及fo-dicom之DeepCopy、

    题记: 最近看了一篇关于架构方面的良心长文你的架构是怎样一步步腐化的?,文中字字句句道出了诸多从业者.初创企业,以及BAT都会遇到的问题,细细品读后认为工作生活中的诸多情况皆如此,都会有一个体量由小到 ...