Переполнение есть
This commit is contained in:
@@ -5,23 +5,51 @@ import com.rabbitmq.client.Connection;
|
|||||||
import com.rabbitmq.client.ConnectionFactory;
|
import com.rabbitmq.client.ConnectionFactory;
|
||||||
import com.rabbitmq.client.DeliverCallback;
|
import com.rabbitmq.client.DeliverCallback;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class Recv {
|
public class Recv {
|
||||||
private final static String QUEUE_NAME = "hello";
|
private final static String QUEUE_NAME = "hello";
|
||||||
|
|
||||||
public static void main(String[] argv) throws Exception {
|
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();
|
ConnectionFactory factory = new ConnectionFactory();
|
||||||
factory.setHost("rabbitmq");
|
factory.setHost("rabbitmq");
|
||||||
|
|
||||||
Connection connection = factory.newConnection();
|
Connection connection = factory.newConnection();
|
||||||
Channel channel = connection.createChannel();
|
Channel channel = connection.createChannel();
|
||||||
|
|
||||||
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
Map<String, Object> args = new HashMap<>();
|
||||||
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
|
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) -> {
|
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
|
||||||
String message = new String(delivery.getBody(), "UTF-8");
|
String message = new String(delivery.getBody(), "UTF-8");
|
||||||
System.out.println(" [x] Received '" + message + "'");
|
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 -> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ services:
|
|||||||
context: ./producer
|
context: ./producer
|
||||||
depends_on:
|
depends_on:
|
||||||
- rabbitmq
|
- rabbitmq
|
||||||
|
- consumer
|
||||||
|
environment:
|
||||||
|
- PRODUCER_DELAY_MS=1000
|
||||||
networks:
|
networks:
|
||||||
- rabbitnet
|
- rabbitnet
|
||||||
|
|
||||||
@@ -24,6 +27,9 @@ services:
|
|||||||
context: ./producer
|
context: ./producer
|
||||||
depends_on:
|
depends_on:
|
||||||
- rabbitmq
|
- rabbitmq
|
||||||
|
- consumer
|
||||||
|
environment:
|
||||||
|
- PRODUCER_DELAY_MS=1000
|
||||||
networks:
|
networks:
|
||||||
- rabbitnet
|
- rabbitnet
|
||||||
|
|
||||||
@@ -33,6 +39,10 @@ services:
|
|||||||
context: ./consumer
|
context: ./consumer
|
||||||
depends_on:
|
depends_on:
|
||||||
- rabbitmq
|
- rabbitmq
|
||||||
|
environment:
|
||||||
|
- CONSUMER_DELAY_MS=1000
|
||||||
|
- QUEUE_MAX_LENGTH=10
|
||||||
|
- QUEUE_OVERFLOW=reject-publish # drop-head, reject-publish
|
||||||
networks:
|
networks:
|
||||||
- rabbitnet
|
- rabbitnet
|
||||||
|
|
||||||
|
|||||||
@@ -8,14 +8,29 @@ public class Send {
|
|||||||
private static final String QUEUE_NAME = "hello";
|
private static final String QUEUE_NAME = "hello";
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
String delayEnv = System.getenv("PRODUCER_DELAY_MS");
|
||||||
|
int delay = delayEnv != null ? Integer.parseInt(delayEnv) : 1000;
|
||||||
|
|
||||||
ConnectionFactory factory = new ConnectionFactory();
|
ConnectionFactory factory = new ConnectionFactory();
|
||||||
factory.setHost("rabbitmq");
|
factory.setHost("rabbitmq");
|
||||||
|
|
||||||
try (Connection connection = factory.newConnection();
|
try (Connection connection = factory.newConnection();
|
||||||
Channel channel = connection.createChannel()) {
|
Channel channel = connection.createChannel()) {
|
||||||
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
|
|
||||||
String message = "Hello World!";
|
boolean durable = false;
|
||||||
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
boolean exclusive = false;
|
||||||
System.out.println(" [x] Sent '" + message + "'");
|
boolean autoDelete = false;
|
||||||
|
|
||||||
|
// channel.queueDeclare(QUEUE_NAME, durable, exclusive, autoDelete, null);
|
||||||
|
channel.queueDeclarePassive(QUEUE_NAME);
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
while (true) {
|
||||||
|
String message = "Message #" + count++;
|
||||||
|
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
|
||||||
|
System.out.println(" [x] Sent '" + message + "'");
|
||||||
|
Thread.sleep(delay);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user