pom 文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <dependencies>
2 <dependency>
3 <groupId>com.rabbitmq</groupId>
4 <artifactId>amqp-client</artifactId>
5 <version>4.0.2</version>
6 </dependency>
7 <dependency>
8 <groupId>org.slf4j</groupId>
9 <artifactId>slf4j-api</artifactId>
10 <version>1.7.10</version>
11 </dependency>
12 <dependency>
13 <groupId>org.slf4j</groupId>
14 <artifactId>slf4j-log4j12</artifactId>
15 <version>1.7.5</version>
16 </dependency>
17 <dependency>
18 <groupId>log4j</groupId>
19 <artifactId>log4j</artifactId>
20 <version>1.2.17</version>
21 </dependency>
22 <dependency>
23 <groupId>junit</groupId>
24 <artifactId>junit</artifactId>
25 <version>4.11</version>
26 </dependency>
27 <dependency>
28 <groupId>org.springframework.amqp</groupId>
29 <artifactId>spring-rabbit</artifactId>
30 <version>1.7.5.RELEASE</version>
31 </dependency>
32
33 </dependencies>
pom文件
spring 集成rabbitmq 的核心配置
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
3 xsi:schemaLocation="http://www.springframework.org/schema/rabbit
4 http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd
5 http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
7
8 <!-- 1.定义RabbitMQ的连接工厂 -->
9 <rabbit:connection-factory id="connectionFactory"
10 host="192.168.152.5" port="5672" username="wh" password="123"
11 virtual-host="/vhost_wh" />
12
13
14 <!-- 2.定义Rabbit模板,指定连接工厂以及定义exchange -->
15 <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="fanoutExchange" />
16
17 <!-- MQ的管理,包括队列、交换器 声明等 -->
18 <rabbit:admin connection-factory="connectionFactory" />
19
20 <!-- 定义队列,自动声明 -->
21 <rabbit:queue name="myQueue" auto-declare="true" durable="true"/>
22
23 <!-- 定义交换器,自动声明 -->
24 <rabbit:fanout-exchange name="fanoutExchange" auto-declare="true">
25 <rabbit:bindings>
26 <rabbit:binding queue="myQueue"/>
27 </rabbit:bindings>
28 </rabbit:fanout-exchange>
29
30
31 <!-- 队列监听 -->
32 <rabbit:listener-container connection-factory="connectionFactory">
33 <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
34 </rabbit:listener-container>
35
36 <!-- 消费者 -->
37 <bean id="foo" class="MyConsumer" />
38
39 </beans>
业务方法的类 和方法
1 public class MyConsumer {
2
3
4 //具体执行业务的方法
5 public void listen(String foo) {
6 System.out.println("消费者: " + foo);
7 }
8
9 }
执行rabbitmq
1 import org.springframework.amqp.rabbit.core.RabbitTemplate;
2 import org.springframework.beans.factory.annotation.Autowired;
3 import org.springframework.context.support.AbstractApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 public class SpringMain {
7
8 public static void main(final String... args) throws Exception {
9 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:context.xml");
10 //RabbitMQ模板
11 RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
12 //发送消息
13 template.convertAndSend("Hello, world!");
14 Thread.sleep(1000);// 休眠1秒
15 ctx.destroy(); //容器销毁
16 }
17
18 }