Producers names

This commit is contained in:
2025-04-15 21:50:28 +03:00
parent 9bd815aa7e
commit def391aba9
2 changed files with 6 additions and 7 deletions

View File

@@ -17,6 +17,7 @@ services:
- rabbitmq
- consumer
environment:
- PRODUCER_NAME=first
- PRODUCER_DELAY_MS=1000
networks:
- rabbitnet
@@ -29,6 +30,7 @@ services:
- rabbitmq
- consumer
environment:
- PRODUCER_NAME=second
- PRODUCER_DELAY_MS=1000
networks:
- rabbitnet

View File

@@ -8,6 +8,9 @@ public class Send {
private static final String QUEUE_NAME = "hello";
public static void main(String[] args) throws Exception {
String name = System.getenv("PRODUCER_NAME");
name = name != null ? name : "";
String delayEnv = System.getenv("PRODUCER_DELAY_MS");
int delay = delayEnv != null ? Integer.parseInt(delayEnv) : 1000;
@@ -16,17 +19,11 @@ public class Send {
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
boolean durable = false;
boolean exclusive = false;
boolean autoDelete = false;
// channel.queueDeclare(QUEUE_NAME, durable, exclusive, autoDelete, null);
channel.queueDeclarePassive(QUEUE_NAME);
int count = 0;
while (true) {
String message = "Message #" + count++;
String message = "Message from " + name + " #" + count++;
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
Thread.sleep(delay);