POM.xml
<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.properties
rabbit.hosts=192.168.42.128 rabbit.username=root rabbit.password=123456 rabbit.virtualHost=/ rabbit.queue=spring.queue.sync #routingkey的名称默认为Queue的名称 rabbit.routingKey=spring.queue.sync
applicationContext.xml
<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*:rabbitmq.properties</value> </list> </property> </bean> <import resource="applicationContext-rabbitmq-sync.xml"/>
applicationContext-rabbitmq-sync.xml
<!-- 创建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> <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 --> <!-- <bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin"> <constructor-arg ref="rabbitConnectionFactory" /> </bean> --> <!--定义rabbit template用于数据的接收和发送 --> <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> -->
Producer.java
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"); System.out.println("发送成功"); } }
Consumer.java
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()); } }