(转)RabbitMQ学习之spring整合发送同步消息
http://blog.csdn.net/zhu_tianwei/article/details/40890543
以下实现使用Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。
1.所需jar包依赖
- <properties>
- <rabbitmq.version>3.0.4</rabbitmq.version>
- <spring.amqp.version>1.1.1.RELEASE</spring.amqp.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.2.RELEASE</spring.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.rabbitmq</groupId>
- <artifactId>amqp-client</artifactId>
- <version>${rabbitmq.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.amqp</groupId>
- <artifactId>spring-rabbit</artifactId>
- <version>${spring.amqp.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- <exclusions>
- <!-- Exclude Commons Logging in favor of SLF4j -->
- <exclusion>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.amqp</groupId>
- <artifactId>spring-amqp</artifactId>
- <version>${spring.amqp.version}</version>
- <classifier>sources</classifier>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.5.10</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- <version>1.5.10</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.5.10</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.14</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.6.9</version>
- </dependency>
- </dependencies>
2.application.properties配置
- #============== rabbitmq config ====================
- rabbit.hosts=192.168.101.210
- rabbit.username=admin
- rabbit.password=admin
- rabbit.virtualHost=/
- rabbit.queue=spring.queue.sync
- rabbit.routingKey=spring.queue.sync
3.applicationContext.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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd"
- default-lazy-init="false">
- <description>Spring公共配置 </description>
- <context:component-scan base-package="cn.slimsmart.rabbitmq.demo.spring" />
- <aop:aspectj-autoproxy />
- <!-- 定义受环境影响易变的变量 -->
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
- <property name="ignoreResourceNotFound" value="true" />
- <property name="locations">
- <list>
- <!-- 标准配置 -->
- <value>classpath*:/application.properties</value>
- </list>
- </property>
- </bean>
- <import resource="applicationContext-rabbitmq-sync.xml"/>
- </beans>
4.applicationContext-rabbitmq-sync.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <!-- 创建connectionFactory -->
- <bean id="rabbitConnectionFactory"
- class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
- <constructor-arg value="${rabbit.hosts}"/>
- <property name="username" value="${rabbit.username}"/>
- <property name="password" value="${rabbit.password}"/>
- <property name="virtualHost" value="${rabbit.virtualHost}"/>
- <property name="channelCacheSize" value="5"/>
- </bean>
- <!-- 创建rabbitAdmin 代理类 -->
- <!-- <bean id="rabbitAdmin" -->
- <!-- class="org.springframework.amqp.rabbit.core.RabbitAdmin"> -->
- <!-- <constructor-arg ref="rabbitConnectionFactory" /> -->
- <!-- </bean> -->
- <!-- 创建rabbitTemplate 消息模板类 -->
- <bean id="rabbitTemplate"
- class="org.springframework.amqp.rabbit.core.RabbitTemplate">
- <constructor-arg ref="rabbitConnectionFactory"></constructor-arg>
- <property name="queue" value="${rabbit.queue}"></property>
- <property name="routingKey" value="${rabbit.routingKey}"></property>
- </bean>
- <!-- 声明Queue并设定Queue的名称 -->
- <!-- <bean id="helloWorldQueue" -->
- <!-- class="org.springframework.amqp.core.Queue"> -->
- <!-- <constructor-arg value="${rabbit.queue}"></constructor-arg> -->
- <!-- </bean> -->
- </beans>
5.生产者Producer.java
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- //生产者
- public class Producer {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
- amqpTemplate.convertAndSend("test spring sync");
- }
- }
6.消费者Consumer.java
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- //消费者
- public class Consumer {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class);
- System.out.println("Received: " + amqpTemplate.receiveAndConvert());
- }
- }
运行生产者向队列中发送一条消息,再运行消费者消费消息。
(转)RabbitMQ学习之spring整合发送同步消息的更多相关文章
- (转) RabbitMQ学习之spring整合发送同步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...
- (转) RabbitMQ学习之spring整合发送异步消息
http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...
- 【RocketMQ源码学习】- 3. Client 发送同步消息
本文较长,代码后面给了方法简图,希望给你帮助 发送的方式 同步发送 异步发送 消息的类型 普通消息 顺序消息 事务消息 发送同步消息的时序图 为了防止读者朋友嫌烦,可以看下时序图,后面我也会给出方法的 ...
- ActiveMQ学习总结------Spring整合ActiveMQ 04
通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...
- RabbitMQ学习笔记之五种模式及消息确认机制
本文详细介绍简单模式Simple.工作模式Work.发布订阅模式Publish/Subscribe.Topic.Routing. Maven依赖引用 <dependencies> < ...
- Spring整合ActiveMQ实现消息延迟投递和定时投递
linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...
- RabbitMQ走过的坑,发送的消息是乱码
发送的消息在可视化界面中是乱码,如图: 看见这个content_tpye没有,是不是很奇怪,就是这个坑,设置下就行,看代码: @Bean Jackson2JsonMessageConverter me ...
- RabbitMQ学习之spring配置文件rabbit标签的使用
下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...
随机推荐
- visual studio2017 创建Vue项目
1:打开Visual studio 2017后 按图片操作新建项目 也可以使用快捷键Ctrl+Shift+N 进入创建项目页面 2:选择JavaScript 里的Node.js创建对应的Vue项目 ...
- salt 根据ip修改主机名
首先定义pillar [root@web1 pillar]# cat hostname.sls ip_hostname: 10.1.1.1: web1 10.1.1.2: web2 10.1.1.3: ...
- 34.初识搜索引擎及timeout机制
主要知识点 1.对搜索执行结果的说明 2.timeout机制讲解 一.对执行 GET /_search 的结果的说明 执行结果如下(只保留部分) { "took": 29, &qu ...
- PAT 1088. Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- php 微擎
pdo_insert('ewei_shop_member', $data); $my = array('agentid' => '4102'); // pdo_update(表明,'修改的值', ...
- 在做公司项目是时,昨天晚上还好的,但是第二天启动tomcat发现tomcat启动了,但是没把项目启动起来
1.问题:在做公司项目是时,昨天晚上还好的,但是第二天启动tomcat发现tomcat启动了,但是没把项目启动起来 2.问题排除: 1)昨天晚上还好着呢,并且没改动代码,排除代码问题.日志中无报错信息 ...
- Eclipse全局搜索
按[Ctrl]+[H] 搜索时支持一些正则表达式. 参考: http://blog.csdn.net/huaweitman/article/details/38709323
- HDU 3987 && DINIC
很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流 ...
- 很实用的50个CSS代码片段
原文:50 Useful CSS Snippets Every Designer Should Have 面对每年如此多的 新趋势 ,保持行业的率先是个非常困难问题. 站点设计者和前 ...
- dlopen failed: empty/missing DT_HASH in "libx.so" (built with --hash-style=gnu?)
崩溃日志内容: java.lang.UnsatisfiedLinkError: dlopen failed: empty/missing DT_HASH in "libxxxx.so&quo ...