作者:caiyingsheng852 | 来源:互联网 | 2023-09-01 11:28
pom 文件
1
2
3 com.rabbitmq
4 amqp-client
5 4.0.2
6
7
8 org.slf4j
9 slf4j-api
10 1.7.10
11
12
13 org.slf4j
14 slf4j-log4j12
15 1.7.5
16
17
18 log4j
19 log4j
20 1.2.17
21
22
23 junit
24 junit
25 4.11
26
27
28 org.springframework.amqp
29 spring-rabbit
30 1.7.5.RELEASE
31
32
33
pom文件
spring 集成rabbitmq 的核心配置
1 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
9 10 host="192.168.152.5" port="5672" username="wh" password="123"
11 virtual-host="/vhost_wh" />
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
业务方法的类 和方法
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 }