以下实现使用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>
#============== 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.sync3.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()); } }运行生产者向队列中发送一条消息,再运行消费者消费消息。