官网文档演示:http://projects.spring.io/spring-amqp/#quick-start
该项目由两部分组成:spring-amqp
是基础抽象,spring-rabbit
是RabbitMQ实现。
特征
- 用于异步处理入站消息的侦听器容器
- RabbitTemplate发送和接收消息
- RabbitAdmin用于自动声明队列,交换和绑定
有两种:1、java方式,,,,,2、xml配置方式
1、java方式
在pom.xml引入相关依赖
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.wisezone</groupId> 6 <artifactId>spring_rabbit</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 10 <name>spring_rabbit</name> 11 <url>http://maven.apache.org</url> 12 13 <properties> 14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 15 </properties> 16 17 <dependencies> 18 19 <dependency> 20 <groupId>org.springframework.amqp</groupId> 21 <artifactId>spring-rabbit</artifactId> 22 <version>1.6.3.RELEASE</version> 23 </dependency> 24 25 <dependency> 26 <groupId>junit</groupId> 27 <artifactId>junit</artifactId> 28 <version>3.8.1</version> 29 <scope>test</scope> 30 </dependency> 31 </dependencies> 32 </project>
Client客户端:
1 package com.wisezone.rabbit.first; 2 3 4 import org.springframework.amqp.core.BindingBuilder; 5 import org.springframework.amqp.core.Queue; 6 import org.springframework.amqp.core.TopicExchange; 7 import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 8 import org.springframework.amqp.rabbit.connection.ConnectionFactory; 9 import org.springframework.amqp.rabbit.core.RabbitAdmin; 10 import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; 11 import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; 12 13 public class FirstClient { 14 15 16 public static void main(String[] args) { 17 com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); 18 factory.setHost("localhost"); 19 factory.setUsername("guest"); 20 factory.setPassword("guest"); 21 22 ConnectionFactory cf = new CachingConnectionFactory(factory); 23 24 // set up the queue, exchange, binding on the broker 25 RabbitAdmin admin = new RabbitAdmin(cf); 26 Queue queue = new Queue("myQueue"); 27 admin.declareQueue(queue); 28 TopicExchange exchange = new TopicExchange("myExchange"); 29 admin.declareExchange(exchange); 30 admin.declareBinding( 31 BindingBuilder.bind(queue).to(exchange).with("foo.*")); 32 33 // set up the listener and container 34 SimpleMessageListenerContainer container = 35 new SimpleMessageListenerContainer(cf); 36 Object listener = new Object() { 37 public void handleMessage(String foo) { 38 System.out.println(foo); 39 } 40 }; 41 42 MessageListenerAdapter adapter = new MessageListenerAdapter(listener); 43 container.setMessageListener(adapter); 44 container.setQueueNames("myQueue"); 45 container.start(); 46 47 } 48 }
Server服务端:
1 package com.wisezone.rabbit.first; 2 3 4 import org.springframework.amqp.core.BindingBuilder; 5 import org.springframework.amqp.core.Queue; 6 import org.springframework.amqp.core.TopicExchange; 7 import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 8 import org.springframework.amqp.rabbit.connection.ConnectionFactory; 9 import org.springframework.amqp.rabbit.core.RabbitAdmin; 10 import org.springframework.amqp.rabbit.core.RabbitTemplate; 11 import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; 12 13 public class FirstServer { 14 15 public static void main(String[] args) throws InterruptedException { 16 com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); 17 factory.setHost("localhost"); 18 factory.setUsername("guest"); 19 factory.setPassword("guest"); 20 21 ConnectionFactory cf = new CachingConnectionFactory(factory); 22 23 // set up the queue, exchange, binding on the broker 24 RabbitAdmin admin = new RabbitAdmin(cf); 25 Queue queue = new Queue("myQueue"); 26 admin.declareQueue(queue); 27 TopicExchange exchange = new TopicExchange("myExchange"); 28 admin.declareExchange(exchange); 29 admin.declareBinding( 30 BindingBuilder.bind(queue).to(exchange).with("foo.*")); 31 32 33 // send something 34 RabbitTemplate template = new RabbitTemplate(cf); 35 template.convertAndSend("myExchange", "foo.bar", "Hello, world!"); 36 37 } 38 39 40 }
运行结果:
2、xml配置方式
application.xml配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd" >
<!-- 扫描package方便注解依赖注入-->
<context:component-scan base-package="com.wisezone" />
<mvc:annotation-driven />
<rabbit:connection-factory id="connectionFactory" host="localhost"
username="guest" password="guest" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="myExchange" routing-key="foo.bar"/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="myQueue" />
<rabbit:topic-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="myQueue" pattern="foo.*" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="comsumer" method="listen" queue-names="myQueue" />
</rabbit:listener-container>
<bean id="comsumer" class="com.wisezone.rabbit.second.Consumer" />
</beans>
Producer:
1 package com.wisezone.rabbit.second; 2 3 import org.springframework.amqp.rabbit.core.RabbitTemplate; 4 import org.springframework.context.support.AbstractApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 public class Producer 8 { 9 10 public static void main(String[] args) throws InterruptedException 11 { 12 //读取spring的配置文件 13 AbstractApplicationContext ctx = new 14 ClassPathXmlApplicationContext("classpath:application.xml"); 15 16 //获取RabbitTemplate 17 RabbitTemplate template = ctx.getBean(RabbitTemplate.class); 18 19 //发送消息 20 template.convertAndSend("你好"); 21 Thread.sleep(1000); 22 23 //销毁 24 ctx.destroy(); 25 } 26 }
Consumer:
1 package com.wisezone.rabbit.second; 2 3 public class Consumer 4 { 5 public void listen(String message) { 6 System.out.println("接收消息: " + message); 7 } 8 }