spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码。由于我使用的rabbitmq版本是3.0.4,部分代码做了调整。
具体实例如下(创建自动删除非持久队列):
1.资源配置application.properties
#============== rabbitmq config ====================
rabbit.hosts=192.168.36.102
rabbit.username=admin
rabbit.password=admin
rabbit.virtualHost=/
rabbit.exchange=spring-queue-async
rabbit.queue=spring-queue-async
rabbit.routingKey=spring-queue-async
2..发送端配置applicationContext-rabbitmq-async-send.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">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
<property name="connectionFactory">
<bean class="com.rabbitmq.client.ConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="virtualHost" value="${rabbit.virtualHost}"/>
</bean>
</property>
<property name="hosts" value="${rabbit.hosts}"/>
</bean>
<bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="rabbitTemplate" class="com.rabbitmq.spring.template.ASyncRabbitTemplate">
<property name="channelFactory" ref="rabbitChannelFactory"/>
<property name="exchange" value="${rabbit.exchange}"/>
<property name="routingKey" value="${rabbit.routingKey}"/>
<!--optional-->
<property name="exchangeType" value="TOPIC"/>
<!-- mandatory是否强制发送 -->
<property name="mandatory" value="false"/>
<!-- immediate是否立即发送 -->
<property name="immediate" value="false"/>
</bean>
</beans>
3.接收端配置applicationContext-rabbitmq-async-receive.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">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
<property name="connectionFactory">
<bean class="com.rabbitmq.client.ConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="virtualHost" value="${rabbit.virtualHost}"/>
</bean>
</property>
<property name="hosts" value="${rabbit.hosts}"/>
</bean>
<bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="receiveMsgHandler" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async.ReceiveMsgHandler"/>
<bean id="quotingParamtersTopicAdapter" class="com.rabbitmq.spring.listener.RabbitMessageListenerAdapter">
<property name="channelFactory" ref="rabbitChannelFactory"/>
<property name="delegate" ref="receiveMsgHandler"/>
<property name="listenerMethod" value="handleMessage"/>
<property name="exchange" value="${rabbit.exchange}"/>
<!--optional-->
<property name="exchangeType" value="TOPIC"/>
<property name="routingKey" value="${rabbit.routingKey}"/>
<property name="queueName" value="${rabbit.queue}"/>
<property name="poolsize" value="5"/>
</bean>
</beans>
4.消息处理服务ReceiveMsgHandler.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
public class ReceiveMsgHandler {
public void handleMessage(String text) {
System.out.println("Received: " + text);
}
}
5.发送端启动代码Send.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.rabbitmq.spring.template.ASyncRabbitTemplate;
public class Send {
public static void main(String[] args) throws InterruptedException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-send.xml");
ASyncRabbitTemplate amqpTemplate = context.getBean(ASyncRabbitTemplate.class);
for(int i=0;i<10000;i++){
amqpTemplate.send("test spring async=>"+i);
Thread.sleep(100);
}
}
}
6.接收端启动代码Send.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.async;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Receive {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("applicationContext-rabbitmq-async-receive.xml");
}
}
先启动接收端,再启动发送端。接收到消息如下:
Received: test spring async=>0
Received: test spring async=>1
Received: test spring async=>2
Received: test spring async=>3
Received: test spring async=>4
Received: test spring async=>5
Received: test spring async=>6
Received: test spring async=>7
......