BAEL-6072 | Create Dynamic Queues in RabbitMQ (#13301)
* BAEL-6072 | Article code * Code changes as per editor review * Test cases updated * Removed unused imports * Updated test case
This commit is contained in:
parent
e48229dc25
commit
a43afaccc9
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user