Java使用RabbitMQ之整合Spring(消费者)

Posted 星瑞的随笔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java使用RabbitMQ之整合Spring(消费者)相关的知识,希望对你有一定的参考价值。

 依赖包:

        <!--RabbitMQ集成spring-->
        <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.0.6.RELEASE</version>
        </dependency>

 

消息者Spring配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:rabbit="http://www.springframework.org/schema/rabbit"
 6        xsi:schemaLocation="
 7             http://www.springframework.org/schema/beans
 8                 http://www.springframework.org/schema/beans/spring-beans.xsd
 9             http://www.springframework.org/schema/context
10                 http://www.springframework.org/schema/context/spring-context.xsd
11             http://www.springframework.org/schema/rabbit
12                 http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
13 
14     <!-- 连接服务配置 -->
15     <rabbit:connection-factory id="connectionFactory"
16                                host="10.15.1.26" username="admin" password="admin" port="5672"
17                                virtual-host="/test_host" channel-cache-size="5"/>
18 
19     <!--MQ的管理,包括队列、交换器的声明等-->
20     <rabbit:admin connection-factory="connectionFactory"/>
21 
22     <!-- queue 队列声明 -->
23     <rabbit:queue durable="true"
24                   auto-delete="false" exclusive="false" name="test.spring.queue"/>
25 
26     <!-- exchange queue binging key 绑定 -->
27     <rabbit:direct-exchange name="spring.exchange"
28                             durable="true" auto-delete="false">
29         <rabbit:bindings>
30             <rabbit:binding queue="test.spring.queue" key="spring.queue.key"/>
31         </rabbit:bindings>
32     </rabbit:direct-exchange>
33 
34     <!-- spring template声明 -->
35     <rabbit:template id="amqpTemplate" exchange="spring.exchange" routing-key="spring.queue.key"
36                      connection-factory="connectionFactory"/>
37 
38 
39     <!-- 监听生产者发送的消息开始 -->
40 
41     <!-- 声明消息转换器为SimpleMessageConverter -->
42     <bean id="messageConverter"
43           class="org.springframework.amqp.support.converter.SimpleMessageConverter">
44     </bean>
45 
46     <!-- 用于接收消息的处理类 -->
47     <bean id="myListener" class="org.study.model.MyListener"/>
48 
49     <!-- 用于消息的监听的代理类MessageListenerAdapter -->
50     <bean id="receiveListenerAdapter"
51           class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
52         <constructor-arg ref="myListener" />
53         <property name="defaultListenerMethod" value="onMessage"></property>
54     </bean>
55 
56     <!-- 用于消息的监听的容器类SimpleMessageListenerContainer,对于queueName的值一定要与定义的Queue的值相同 -->
57     <bean id="listenerContainer"
58           class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
59         <property name="queueNames" value="test.spring.queue"></property>
60         <property name="connectionFactory" ref="connectionFactory"></property>
61         <property name="messageListener" ref="receiveListenerAdapter"></property>
62     </bean>
63 
64 </beans>

 

消费者消息处理代码:

 1 package org.study.model;
 2 
 3 /**
 4  * RabbitMQ与Spring整合
 5  * 消费者消息处理类
 6  */
 7 public class MyListener {
 8     public void onMessage(String message) {
 9         System.out.println(" [RECV] : " + message);
10     }
11 
12 }

 

运行代码:

 1 package org.study.spring5;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * RabbitMQ与Spring整合
 8  * 消费者
 9  */
10 public class SpringConsumer {
11 
12     public static void main(String args[]) {
13         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/consumer-spring-config.xml");
14 
15     }
16 }

 

以上是关于Java使用RabbitMQ之整合Spring(消费者)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2.X - Spring Boot整合AMQP之RabbitMQ

SpringBoot整合RabbitMQ之Spring事件驱动模型

RabbitMQ之交换机及spring整合

荧客技荐RabbitMQ 实战之 Spring Boot 整合 RabbitMQ

SpringBoot整合RabbitMQ之发送接收消息实战

Spring Boot之RabbitMQ