Переполнение есть
This commit is contained in:
@@ -5,23 +5,51 @@ import com.rabbitmq.client.Connection;
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.DeliverCallback;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Recv {
|
||||
private final static String QUEUE_NAME = "hello";
|
||||
|
||||
public static void main(String[] argv) throws Exception {
|
||||
String delayEnv = System.getenv("CONSUMER_DELAY_MS");
|
||||
int delay = delayEnv != null ? Integer.parseInt(delayEnv) : 5000;
|
||||
|
||||
String maxLengthEnv = System.getenv("QUEUE_MAX_LENGTH");
|
||||
int maxLength = maxLengthEnv != null ? Integer.parseInt(maxLengthEnv) : 5;
|
||||
|
||||
String overflowStrategy = System.getenv("QUEUE_OVERFLOW");
|
||||
if (overflowStrategy == null) {
|
||||
overflowStrategy = "drop-head"; // или "reject-publish"
|
||||
}
|
||||
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost("rabbitmq");
|
||||
|
||||
Connection connection = factory.newConnection();
|
||||
Channel channel = connection.createChannel();
|
||||
|
||||
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
||||
Map<String, Object> args = new HashMap<>();
|
||||
args.put("x-max-length", maxLength);
|
||||
args.put("x-overflow", overflowStrategy);
|
||||
|
||||
channel.queueDeclare(QUEUE_NAME, false, false, false, args);
|
||||
channel.basicQos(1);
|
||||
|
||||
System.out.println(" [*] Waiting for messages...");
|
||||
|
||||
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||
String message = new String(delivery.getBody(), "UTF-8");
|
||||
System.out.println(" [x] Received '" + message + "'");
|
||||
try {
|
||||
Thread.sleep(delay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
|
||||
};
|
||||
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
|
||||
|
||||
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user