1、准备
----|jar:spring3.1下所有、aopalliance、commons.logging、spring.retry、spring.rabibt、spring.amqp、rabbitmq.client、aspectj。
----|编译工具:eclipse
----|rabbitMQ:rabbitMQ3.6.1
----|erlang
2、搭建
----|工程:java工程
--------|实体包:com.wind.main
------------|消息发送方:Producer.java:
package com.wind.main;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
RabbitTemplate rabbitTemplate;
public void send(String msg){
rabbitTemplate.convertAndSend(msg);
}
}
------------|消息接收方:QueueListener.java:
package com.wind.main;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
@Component
public class QueueListener implements MessageListener{
@Override
public void onMessage(Message msg) {
System.out.println(msg);
}
}
--------|测试包:com.wind.test
------------|测试类TestRabbit.java:
package com.wind.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wind.main.Producer;
public class TestRabbit {
@Test
public void fun1() throws InterruptedException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:applicationContext-rabbitmq.xml");
ctx.start();
Producer producer = (Producer) ctx.getBean(Producer.class);
producer.send("this is the test msg");
Thread.sleep(1000);
ctx.destroy();
}
}
--------|配置文件:applicationContex-rabbitmq.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"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<!-- 自动扫描指定包下的所有类上的@Component、@Controller等注解 -->
<context:component-scan base-package="com.wind.main" />
<!-- 引入外部propperties文件 -->
<context:property-placeholder location="classpath:rabbit.properties" />
<!-- rabbit的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}"
password="${rabbit.password}" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- rabbit模板对象,在这里绑定交换机和连接工厂 -->
<rabbit:template id="rabbitTemplate"
connection-factory="connectionFactory" exchange="my_queue" />
<!-- rabbit队列 -->
<rabbit:queue name="test_queue" />
<!-- rabbit的交换机,在这里绑定队列 -->
<rabbit:topic-exchange name="my_queue">
<rabbit:bindings>
<rabbit:binding queue="test_queue" pattern="#" />
</rabbit:bindings>
</rabbit:topic-exchange>
<!-- rabbit的监听容器,在这里绑定要监听的队列和用来监听的Bean -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener queues="test_queue" ref="queueListener" />
</rabbit:listener-container>
</beans>
--------|资源文件:rabbit.properties:
rabbit.username=guest
rabbit.password=guest
rabbit.port=5672
rabbit.host=localhost
3、测试
----|运行TestRabbit.java:
出现如下结果说明测试成功:
(Body:'this is the test msg' MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, appId=null, clusterId=null, type=null, correlationId=null, replyTo=null, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, deliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=my_queue, receivedRoutingKey=, deliveryTag=1, messageCount=0])