Skip to content

Commit bfeca87

Browse files
committed
docs: [SpringBoot整合RabbitMQ]增加rabbitmq的简单模型示例(生产者-消费者)
1 parent 2f309e1 commit bfeca87

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package edu.study.module.springbootrabbitmq.demo;
2+
3+
import com.rabbitmq.client.Channel;
4+
import com.rabbitmq.client.Connection;
5+
import com.rabbitmq.client.ConnectionFactory;
6+
7+
import java.io.IOException;
8+
import java.util.concurrent.TimeoutException;
9+
10+
/**
11+
* 测试使用:RabbitMQ 简单工作模式(hello world)
12+
* <pre>
13+
* 1.创建链接工厂;
14+
* 2.创建链接Connection
15+
* 3.通过链接获取通道Channel
16+
* 4.通过创建交换机,声明队列,绑定关系,路由key,发送消息,和接受消息;
17+
* 5.准备消息内容
18+
* 6.发送消息给队列queue
19+
* 7.关闭链接
20+
* 8.关闭通道
21+
* </pre>
22+
*
23+
* @author zl
24+
* @create 2021-08-16 11:06
25+
*/
26+
public class RabbitmqDemoConsumer {
27+
public static void main(String[] args) {
28+
// 1. 创建链接工厂
29+
ConnectionFactory connectionFactory = new ConnectionFactory();
30+
connectionFactory.setHost("192.168.174.156");
31+
connectionFactory.setPort(5672);
32+
connectionFactory.setUsername("admin");
33+
connectionFactory.setPassword("admin");
34+
connectionFactory.setConnectionTimeout(6000);
35+
connectionFactory.setVirtualHost("/");
36+
// 2.创建链接
37+
Connection connection = null;
38+
Channel channel = null;
39+
try {
40+
connection = connectionFactory.newConnection("生产者");
41+
channel = connection.createChannel();
42+
String queueName = "queue1";
43+
channel.basicConsume("queue1", true,
44+
(s, delivery) -> System.out.println("收到消息:" + new String(delivery.getBody(), "UTF-8")),
45+
s -> System.out.println("接受失败了……"));
46+
47+
System.out.println("消息发送成功。。。");
48+
System.in.read();
49+
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
} finally {
53+
// 关闭通道
54+
if (channel != null && channel.isOpen()) {
55+
try {
56+
channel.close();
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
} catch (TimeoutException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
// 关闭链接
65+
if (connection != null && connection.isOpen()) {
66+
try {
67+
connection.close();
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
}
73+
}
74+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package edu.study.module.springbootrabbitmq.demo;
2+
3+
import com.rabbitmq.client.Channel;
4+
import com.rabbitmq.client.Connection;
5+
import com.rabbitmq.client.ConnectionFactory;
6+
import com.rabbitmq.client.MessageProperties;
7+
8+
import java.io.IOException;
9+
import java.util.concurrent.TimeoutException;
10+
11+
/**
12+
* 测试使用:RabbitMQ 简单工作模式(hello world)
13+
* <pre>
14+
* 1.创建链接工厂;
15+
* 2.创建链接Connection
16+
* 3.通过链接获取通道Channel
17+
* 4.通过创建交换机,声明队列,绑定关系,路由key,发送消息,和接受消息;
18+
* 5.准备消息内容
19+
* 6.发送消息给队列queue
20+
* 7.关闭链接
21+
* 8.关闭通道
22+
* </pre>
23+
*
24+
* @author zl
25+
* @create 2021-08-16 11:06
26+
*/
27+
public class RabbitmqDemoProductor {
28+
public static void main(String[] args) {
29+
// 1. 创建链接工厂
30+
ConnectionFactory connectionFactory = new ConnectionFactory();
31+
connectionFactory.setHost("192.168.174.156");
32+
connectionFactory.setPort(5672);
33+
connectionFactory.setUsername("admin");
34+
connectionFactory.setPassword("admin");
35+
connectionFactory.setConnectionTimeout(6000);
36+
connectionFactory.setVirtualHost("/");
37+
// 2.创建链接
38+
Connection connection = null;
39+
Channel channel = null;
40+
try {
41+
connection = connectionFactory.newConnection("生产者");
42+
channel = connection.createChannel();
43+
String queueName = "queue1";
44+
/**
45+
* 队列声明信息
46+
* 1.队列名称
47+
* 2.是否要持久化durable=false,所谓持久化消息是否存盘,如果false,非持久化true是否持久化?非持久化会存盘嘛?会存盘,但是会随着rabbitmq服务的关闭而丢失
48+
* 3.排他性,是否是独占队列
49+
* 4.是否自动删除,随着最后一个消息完毕消息以后是否吧队列自动删除
50+
* 5.携带附属参数
51+
*/
52+
channel.queueDeclare(queueName, false, false,false,null);
53+
String message = "hello world";
54+
channel.basicPublish("", queueName, MessageProperties.PERSISTENT_BASIC, message.getBytes());
55+
System.out.println("消息发送成功。。。");
56+
} catch (Exception e) {
57+
e.printStackTrace();
58+
} finally {
59+
// 关闭通道
60+
if (channel != null && channel.isOpen()) {
61+
try {
62+
channel.close();
63+
} catch (IOException e) {
64+
e.printStackTrace();
65+
} catch (TimeoutException e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
70+
// 关闭链接
71+
if (connection != null && connection.isOpen()) {
72+
try {
73+
connection.close();
74+
} catch (IOException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)