Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4476f7

Browse files
author
machunyu
committed
RabbitMQ
1 parent 4d22616 commit b4476f7

File tree

3 files changed

+115
-10
lines changed

3 files changed

+115
-10
lines changed

scParent/boot-rabbitmq/src/main/java/com/test/simple/RabbitMQConcumer.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.test.simple;
22

3-
import com.rabbitmq.client.CancelCallback;
3+
import com.rabbitmq.client.AMQP;
44
import com.rabbitmq.client.Channel;
55
import com.rabbitmq.client.Connection;
66
import com.rabbitmq.client.ConnectionFactory;
7-
import com.rabbitmq.client.DeliverCallback;
7+
import com.rabbitmq.client.DefaultConsumer;
8+
import com.rabbitmq.client.Envelope;
89

910
import java.io.IOException;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
1013
import java.util.concurrent.TimeoutException;
1114

1215
public class RabbitMQConcumer {
@@ -15,6 +18,8 @@ public class RabbitMQConcumer {
1518

1619
public static void main(String[] args) throws IOException, TimeoutException {
1720

21+
ExecutorService executorService = Executors.newFixedThreadPool(10);
22+
1823
ConnectionFactory factory = new ConnectionFactory();
1924

2025
factory.setHost("47.115.158.112");
@@ -27,15 +32,21 @@ public static void main(String[] args) throws IOException, TimeoutException {
2732

2833
Channel channel = connection.createChannel();
2934

30-
DeliverCallback deliverCallback = (consumerTag, message) -> {
31-
System.out.println(new String(message.getBody()));
32-
};
35+
Boolean autoAck = false;
36+
channel.basicQos(64);
3337

34-
CancelCallback callback = consumerTag -> {
38+
channel.basicConsume(QUEUE_NAME, autoAck, new DefaultConsumer(channel) {
39+
@Override
40+
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
41+
String routingKey = envelope.getRoutingKey();
42+
String contentType = properties.getContentType();
43+
long deliveryTag = envelope.getDeliveryTag();
44+
System.out.println("consumerTag:" + consumerTag + ". Thread: " + Thread.currentThread().getId() + ". message: " + new String(body));
45+
channel.basicAck(deliveryTag, false);
3546

36-
};
47+
}
48+
});
3749

38-
channel.basicConsume(QUEUE_NAME, true, deliverCallback, callback);
3950

4051
// channel.close();
4152
// connection.close();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.test.simple;
2+
3+
import com.rabbitmq.client.AMQP;
4+
import com.rabbitmq.client.Channel;
5+
import com.rabbitmq.client.Connection;
6+
import com.rabbitmq.client.ConnectionFactory;
7+
import com.rabbitmq.client.DefaultConsumer;
8+
import com.rabbitmq.client.Envelope;
9+
10+
import java.io.IOException;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.Executors;
13+
import java.util.concurrent.TimeoutException;
14+
15+
public class RabbitMQConcumer1 {
16+
17+
public static final String QUEUE_NAME = "Hello";
18+
19+
public static void main(String[] args) throws IOException, TimeoutException {
20+
21+
ExecutorService executorService = Executors.newFixedThreadPool(10);
22+
23+
ConnectionFactory factory = new ConnectionFactory();
24+
25+
factory.setHost("47.115.158.112");
26+
factory.setPort(5672);
27+
factory.setVirtualHost("/");
28+
factory.setUsername("admin");
29+
factory.setPassword("admin");
30+
31+
Connection connection = factory.newConnection();
32+
33+
Channel channel = connection.createChannel();
34+
35+
Boolean autoAck = false;
36+
channel.basicQos(64);
37+
38+
channel.basicConsume(QUEUE_NAME, autoAck, new DefaultConsumer(channel) {
39+
@Override
40+
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
41+
String routingKey = envelope.getRoutingKey();
42+
String contentType = properties.getContentType();
43+
long deliveryTag = envelope.getDeliveryTag();
44+
System.out.println("consumerTag:" + consumerTag + ". Thread: " + Thread.currentThread().getId() + ". message: " + new String(body));
45+
channel.basicAck(deliveryTag, false);
46+
47+
}
48+
});
49+
50+
51+
// channel.close();
52+
// connection.close();
53+
54+
}
55+
}

scParent/boot-rabbitmq/src/main/java/com/test/simple/RabbitMQProduct.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.test.simple;
22

33
import com.rabbitmq.client.Channel;
4+
import com.rabbitmq.client.ConfirmListener;
45
import com.rabbitmq.client.Connection;
56
import com.rabbitmq.client.ConnectionFactory;
67

@@ -10,9 +11,10 @@
1011
public class RabbitMQProduct {
1112

1213
public static final String QUEUE_NAME = "Hello";
14+
public static final String EXCHANGE_NAME = "exchange";
1315
public static final String DEFAULT_SEND_MESSAGE = "Hello, this is one message";
1416

15-
public static void main(String[] args) throws IOException, TimeoutException {
17+
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
1618

1719
ConnectionFactory factory = new ConnectionFactory();
1820

@@ -26,9 +28,46 @@ public static void main(String[] args) throws IOException, TimeoutException {
2628

2729
Channel channel = connection.createChannel();
2830

31+
/**
32+
* exchange: 交换机名称
33+
* type: 交换机类型
34+
* durable: 是否持久化
35+
* autoDelete: 是否自动删除。自动删除的前提是至少有个队列或者交换器与这个交换器绑定 之后所有与这个交换器绑定的队列或者交换器都与解绑
36+
* internal: 是否内置交换机,内置交换机,不可被client直接访问,只能通过交换机路由交换机的形式访问
37+
* arguments: 其他结构参数
38+
*/
39+
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, true, false, null);
40+
41+
/**
42+
* queue:队列名称
43+
* durable:是否持久化
44+
* exclusive:是否排他。如果一个队列被声明为排他队列,该队列仅对首次声明它的连接可见,并在连接断开时自动删除
45+
* 排他队列是针对Connection可见的,同一个Connection不同channel,可以同时访问同一个Connection创建的排他队列
46+
* 注意:即使该队列是持久化的,一旦连接关闭或者客户端退出,该排他队列都会被自动删除
47+
* autoDelete:是否自动删除
48+
*
49+
*/
2950
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
51+
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "hello");
52+
53+
channel.confirmSelect();
54+
channel.addConfirmListener(new ConfirmListener() {
55+
@Override
56+
public void handleAck(long deliveryTag, boolean multiple) throws IOException {
57+
System.out.println(deliveryTag + "成功," + multiple);
58+
}
59+
60+
@Override
61+
public void handleNack(long deliveryTag, boolean multiple) throws IOException {
62+
System.out.println(deliveryTag + "失败," + multiple);
63+
}
64+
});
65+
for (int i = 0; i < 50000; i++) {
66+
channel.basicPublish(EXCHANGE_NAME, "hello", null, DEFAULT_SEND_MESSAGE.getBytes());
67+
System.out.println("第" + i + ", 已经发送数据。。。");
68+
}
3069

31-
channel.basicPublish("", QUEUE_NAME, null, DEFAULT_SEND_MESSAGE.getBytes());
70+
Thread.sleep(5000);
3271

3372
channel.close();
3473
connection.close();

0 commit comments

Comments
 (0)