Java使用RabbitMQ之整合Spring(消费者)
2021-06-29 02:04
标签:pack test new exchange spring model 文件 pass routing 依赖包: 消息者Spring配置文件 消费者消息处理代码: 运行代码: Java使用RabbitMQ之整合Spring(消费者) 标签:pack test new exchange spring model 文件 pass routing 原文地址:https://www.cnblogs.com/gongxr/p/9648172.html
dependency>
groupId>org.springframework.amqpgroupId>
artifactId>spring-rabbitartifactId>
version>2.0.6.RELEASEversion>
dependency>
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
20 rabbit:admin connection-factory="connectionFactory"/>
21
22
23 rabbit:queue durable="true"
24 auto-delete="false" exclusive="false" name="test.spring.queue"/>
25
26
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
35 rabbit:template id="amqpTemplate" exchange="spring.exchange" routing-key="spring.queue.key"
36 connection-factory="connectionFactory"/>
37
38
39
40
41
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
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
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(消费者)
文章链接:http://soscw.com/index.php/essay/99154.html