🔄Update: 队列、消息持久化 & 公平分发

This commit is contained in:
2025-11-14 10:11:55 +08:00
parent f03568cc8e
commit 08b6595fbd
3 changed files with 34 additions and 11 deletions
+22 -3
View File
@@ -1,11 +1,30 @@
package cn.hezhaohui; package cn.hezhaohui;
import cn.hezhaohui.mq.Recv;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class MqApplicaion { public class MqApplicaion {
public static void main(String[] args) { public static void main(String[] args) throws IOException, TimeoutException {
SpringApplication.run(MqApplicaion.class, args); new Thread(() -> {
try {
Recv.work("1");
} catch (IOException | TimeoutException e) {
throw new RuntimeException(e);
}
}).start();
new Thread(() -> {
try {
Recv.work("2");
} catch (IOException | TimeoutException e) {
throw new RuntimeException(e);
}
}).start();
} }
} }
+5 -5
View File
@@ -7,17 +7,16 @@ import com.rabbitmq.client.DeliverCallback;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
public class Recv { public class Recv {
private final static String QUEUE_NAME = "hello"; private final static String QUEUE_NAME = "task";
public static void main(String[] args) throws IOException, TimeoutException { public static void work(String id) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory(); ConnectionFactory connectionFactory = new ConnectionFactory();
Connection connection = connectionFactory.newConnection(); Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel(); Channel channel = connection.createChannel();
channel.basicQos(1);
System.out.println(" [*] Waiting for message"); System.out.println(" [*] Waiting for message");
DeliverCallback deliverCallback = (consumerTag, delivery) -> { DeliverCallback deliverCallback = (consumerTag, delivery) -> {
@@ -29,6 +28,7 @@ public class Recv {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
System.out.println(" [x] Done"); System.out.println(" [x] Done");
System.out.println("== FROM " + id + " ==");
} }
}; };
boolean autoAck = true; boolean autoAck = true;
@@ -37,6 +37,6 @@ public class Recv {
private static void dowork(String task) throws InterruptedException { private static void dowork(String task) throws InterruptedException {
System.out.println(" [Working] " + task); System.out.println(" [Working] " + task);
Thread.sleep(1000); Thread.sleep(5000);
} }
} }
+7 -3
View File
@@ -3,26 +3,30 @@ package cn.hezhaohui.mq;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import java.io.IOException; import java.io.IOException;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
public class Send { public class Send {
private final static String QUEUE_NAME = "hello"; private final static String QUEUE_NAME = "task";
public static void main(String[] args) { public static void main(String[] args) {
ConnectionFactory connectionFactory = new ConnectionFactory(); ConnectionFactory connectionFactory = new ConnectionFactory();
try (Connection connection = connectionFactory.newConnection(); try (Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel(); Channel channel = connection.createChannel();
Scanner scanner = new Scanner(System.in)){ Scanner scanner = new Scanner(System.in)){
channel.queueDeclare(QUEUE_NAME, false, false, false, null); boolean durable = true;
channel.queueDeclare(QUEUE_NAME, durable, false, false, null);
System.out.println(" === Now you can send message ==="); System.out.println(" === Now you can send message ===");
while (true) { while (true) {
System.out.println("[Send]:"); System.out.println("[Send]:");
String message = scanner.nextLine(); String message = scanner.nextLine();
channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); channel.basicPublish("", QUEUE_NAME,
MessageProperties.PERSISTENT_TEXT_PLAIN,
message.getBytes());
System.out.println(" [x] Sent '" + message + "'"); System.out.println(" [x] Sent '" + message + "'");
if ("exit".equalsIgnoreCase(message)) { if ("exit".equalsIgnoreCase(message)) {