Using try-with-resources for thread executo

This commit is contained in:
2024-11-23 18:50:42 +02:00
parent 7c9a960e11
commit e6b29e6d7c
2 changed files with 23 additions and 12 deletions

View File

@@ -5,6 +5,8 @@ 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.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -14,10 +16,10 @@ public class BrokerConnector {
private static final String RABBITMQ_HOST = "rabbitmq"; private static final String RABBITMQ_HOST = "rabbitmq";
private static final String RABBITMQ_USER = "symphony"; private static final String RABBITMQ_USER = "symphony";
private static final String RABBITMQ_PASSWORD = "symphony"; private static final String RABBITMQ_PASSWORD = "symphony";
private static final CountDownLatch shutdownLatch = new CountDownLatch(1);
public static void connect() { public static void connect() {
ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor(); try (ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor()) {
reconnectExecutor.scheduleWithFixedDelay(() -> { reconnectExecutor.scheduleWithFixedDelay(() -> {
try { try {
connectToRabbitMQ(); connectToRabbitMQ();
@@ -25,6 +27,12 @@ public class BrokerConnector {
System.err.println("Awaiting broker connection: " + e.getMessage()); System.err.println("Awaiting broker connection: " + e.getMessage());
} }
}, 0, 5, TimeUnit.SECONDS); }, 0, 5, TimeUnit.SECONDS);
shutdownLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("Connector interrupted: " + e.getMessage());
}
} }
private static void connectToRabbitMQ() throws Exception { private static void connectToRabbitMQ() throws Exception {
@@ -40,15 +48,18 @@ public class BrokerConnector {
System.out.println("Connected to RabbitMQ and queue declared."); System.out.println("Connected to RabbitMQ and queue declared.");
DeliverCallback deliverCallback = (consumerTag, delivery) -> { DeliverCallback deliverCallback = (consumerTag, delivery) -> {
// String message = new String(delivery.getBody(), StandardCharsets.UTF_8); System.out.println(new String(delivery.getBody(), StandardCharsets.UTF_8));
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}; };
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {}); channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
Thread.currentThread().join(); shutdownLatch.await();
} }
} }
public static void shutdown() {
shutdownLatch.countDown();
}
} }

View File

@@ -53,7 +53,7 @@ public class BrokerConnector {
} catch (IOException e) { } catch (IOException e) {
System.err.println("Failed to send message: " + e.getMessage()); System.err.println("Failed to send message: " + e.getMessage());
} }
}, 0, 1, TimeUnit.NANOSECONDS); }, 0, 1, TimeUnit.SECONDS);
latch.await(); latch.await();
scheduler.shutdown(); scheduler.shutdown();