diff --git a/messaging-modules/rabbitmq/src/main/java/com/baeldung/queue/dynamic/DynamicQueueCreation.java b/messaging-modules/rabbitmq/src/main/java/com/baeldung/queue/dynamic/DynamicQueueCreation.java new file mode 100644 index 0000000000..f62660bf81 --- /dev/null +++ b/messaging-modules/rabbitmq/src/main/java/com/baeldung/queue/dynamic/DynamicQueueCreation.java @@ -0,0 +1,34 @@ +package com.baeldung.queue.dynamic; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.rabbitmq.client.AMQP; +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +public class DynamicQueueCreation { + + private static final Logger log = LoggerFactory.getLogger(DynamicQueueCreation.class); + + private static final String QUEUE_NAME = "baeldung-queue"; + + public static void main(String[] args) throws IOException, TimeoutException { + + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + + try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { + AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(QUEUE_NAME, true, false, false, null); + log.info(declareOk.getQueue()); + + AMQP.Queue.DeclareOk declareOkExists = channel.queueDeclarePassive(QUEUE_NAME); + log.info(declareOkExists.getQueue()); + } + } + +} diff --git a/messaging-modules/rabbitmq/src/test/java/com/baeldung/benchmark/queue/dynamic/DynamicQueueCreationLiveTest.java b/messaging-modules/rabbitmq/src/test/java/com/baeldung/benchmark/queue/dynamic/DynamicQueueCreationLiveTest.java new file mode 100644 index 0000000000..aa430035ef --- /dev/null +++ b/messaging-modules/rabbitmq/src/test/java/com/baeldung/benchmark/queue/dynamic/DynamicQueueCreationLiveTest.java @@ -0,0 +1,71 @@ +package com.baeldung.benchmark.queue.dynamic; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.rabbitmq.client.AMQP; +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +public class DynamicQueueCreationLiveTest { + + private static final String QUEUE_NAME = "baeldung-queue"; + private static final String QUEUE_NAME_NEW = "baeldung-queue-new"; + + private static Connection connection; + + @BeforeAll + public static void setUpConnection() throws IOException, TimeoutException { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + connection = factory.newConnection(); + } + + @Test + void givenQueueName_whenCreatingQueue_thenCheckingIfQueueCreated() throws IOException, TimeoutException { + + try (Channel channel = connection.createChannel()) { + AMQP.Queue.DeclareOk declareOk = channel.queueDeclare(QUEUE_NAME, true, false, false, null); + + assertNotNull(declareOk); + assertEquals(QUEUE_NAME, declareOk.getQueue()); + } + } + + @Test + void givenQueueName_whenCreatingQueue_thenCheckingIfQueueExists() throws IOException, TimeoutException { + + try (Channel channel = connection.createChannel()) { + channel.queueDeclare(QUEUE_NAME, true, false, false, null); + + AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(QUEUE_NAME); + + assertNotNull(declareOk); + assertEquals(QUEUE_NAME, declareOk.getQueue()); + } + } + + @Test + void givenQueueName_whenQueueDoesNotExist_thenCheckingIfQueueExists() throws IOException, TimeoutException { + + try (Channel channel = connection.createChannel()) { + assertThrows(IOException.class, () -> { + channel.queueDeclarePassive(QUEUE_NAME_NEW); + }); + } + } + + @AfterAll + public static void destroyConnection() throws IOException { + connection.close(); + } +}