2 Commits
Author SHA1 Message Date
wonder f03568cc8e 🔄Update: 获取并完成任务 2025-11-14 09:43:34 +08:00
wonder d744cf8d4e 🔄Update: 任务队列 2025-11-14 09:38:48 +08:00
2 changed files with 32 additions and 5 deletions
+15 -1
View File
@@ -7,6 +7,7 @@ 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 {
@@ -22,7 +23,20 @@ public class Recv {
DeliverCallback deliverCallback = (consumerTag, delivery) -> { DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8); String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + message + "'"); System.out.println(" [x] Received '" + message + "'");
try {
dowork(message);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
System.out.println(" [x] Done");
}
}; };
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {}); boolean autoAck = true;
channel.basicConsume(QUEUE_NAME, autoAck, deliverCallback, consumerTag -> {});
}
private static void dowork(String task) throws InterruptedException {
System.out.println(" [Working] " + task);
Thread.sleep(1000);
} }
} }
+17 -4
View File
@@ -5,6 +5,7 @@ import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException; import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
public class Send { public class Send {
@@ -13,11 +14,23 @@ public class Send {
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)){
channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!"; System.out.println(" === Now you can send message ===");
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'"); while (true) {
System.out.println("[Send]:");
String message = scanner.nextLine();
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
if ("exit".equalsIgnoreCase(message)) {
System.out.println(" [x] Bye");
break;
}
}
} catch (IOException | TimeoutException e) { } catch (IOException | TimeoutException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }