Example usage for com.rabbitmq.client Channel close

List of usage examples for com.rabbitmq.client Channel close

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel close.

Prototype

@Override
void close() throws IOException, TimeoutException;

Source Link

Document

Close this channel with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

From source file:rabbitmqapp.EmitLog.java

public static void main(String... args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(Cons.RABBIT_MQ_URL);
    factory.setConnectionTimeout(3000);//from w w w . j av  a2  s  .c o m
    factory.setRequestedHeartbeat(30);

    //Create a connection
    Connection connection = factory.newConnection();
    //create a channel from connection
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(Cons.EXCHANGE_NAME, EXCHANGE_TYPE);
    String message = "Hello Dolly";

    channel.basicPublish(Cons.EXCHANGE_NAME, "", null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
    channel.close();
    connection.close();
}

From source file:rabbitmqapp.RabbitMQApp.java

/**
 * @param args the command line arguments
 *//*from   ww w.j  av  a2s .  c o  m*/
public static void main(String[] args) throws Exception {
    ProductOrder order = new ProductOrder("Thando Mlauzi", 34.50);
    ConnectionFactory factory = new ConnectionFactory();
    String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex";
    factory.setUri(uri);

    //Recommended settings
    factory.setRequestedHeartbeat(30);
    factory.setConnectionTimeout(30000);

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUENAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUENAME, null, RabbitUtility.convertToByteArray(order));
    System.out.println(" [x] Sent '" + order + "'");

    channel.close();
    connection.close();

}

From source file:reactor.rabbitmq.ConnectionRecoveryTests.java

License:Open Source License

@BeforeEach
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();/*www .j a va  2s.  c  o m*/
    connectionFactory.setNetworkRecoveryInterval(RECOVERY_INTERVAL);
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    receiver = null;
    sender = null;
    connectionMono = Mono.just(connectionFactory.newConnection()).cache();
}

From source file:reactor.rabbitmq.ConnectionRecoveryTests.java

License:Open Source License

@AfterEach
public void tearDown() throws Exception {
    if (connection != null) {
        Channel channel = connection.createChannel();
        channel.queueDelete(queue);/*  w w w  .  j a va2  s. com*/
        channel.close();
        connection.close();
    }
    if (sender != null) {
        sender.close();
    }
    if (receiver != null) {
        receiver.close();
    }
    if (connectionMono != null) {
        connectionMono.block().close();
    }
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@BeforeEach
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();//ww w . ja v a  2  s.c  o  m
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    receiver = null;
    sender = null;
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@AfterEach
public void tearDown() throws Exception {
    if (connection != null) {
        Channel channel = connection.createChannel();
        channel.queueDelete(queue);//from   w  w w . j a v a  2s  .  co m
        channel.close();
        connection.close();
    }
    if (sender != null) {
        sender.close();
    }
    if (receiver != null) {
        receiver.close();
    }
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void createResourcesPublishConsume() throws Exception {
    final String queueName = UUID.randomUUID().toString();
    final String exchangeName = UUID.randomUUID().toString();
    final String routingKey = "a.b";
    int nbMessages = 100;
    try {//from   ww  w  . j a  v a 2s  . com
        sender = createSender();
        receiver = RabbitFlux.createReceiver();

        CountDownLatch latch = new CountDownLatch(nbMessages);
        AtomicInteger count = new AtomicInteger();

        Disposable resourceSendingConsuming = sender.declare(exchange(exchangeName))
                .then(sender.declare(queue(queueName)))
                .then(sender.bind(binding(exchangeName, routingKey, queueName)))
                .thenMany(sender.send(Flux.range(0, nbMessages)
                        .map(i -> new OutboundMessage(exchangeName, routingKey, i.toString().getBytes()))))
                .thenMany(receiver.consumeNoAck(queueName, new ConsumeOptions().stopConsumingBiFunction(
                        (emitter, msg) -> Integer.parseInt(new String(msg.getBody())) == nbMessages - 1)))
                .subscribe(msg -> {
                    count.incrementAndGet();
                    latch.countDown();
                });

        assertTrue(latch.await(5, TimeUnit.SECONDS));
        assertEquals(nbMessages, count.get());
        resourceSendingConsuming.dispose();
    } finally {
        final Channel channel = connection.createChannel();
        channel.exchangeDelete(exchangeName);
        channel.queueDelete(queueName);
        channel.close();
    }
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@Before
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();/*w w w.  ja  v  a  2 s.c o m*/
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    receiver = null;
    sender = null;
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@After
public void tearDown() throws Exception {
    if (connection != null) {
        Channel channel = connection.createChannel();
        channel.queueDelete(queue);//from w w w .j  av  a2 s .  co  m
        channel.close();
        connection.close();
    }
    if (sender != null) {
        sender.close();
    }
    if (receiver != null) {
        receiver.close();
    }
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@Test
public void createResources() throws Exception {
    final Channel channel = connection.createChannel();

    final String queueName = UUID.randomUUID().toString();
    final String exchangeName = UUID.randomUUID().toString();

    try {/*  ww w.  j a v  a2s  .  co m*/
        sender = ReactorRabbitMq.createSender();

        Mono<AMQP.Queue.BindOk> resources = sender
                .createExchange(ExchangeSpecification.exchange().name(exchangeName))
                .then(sender.createQueue(QueueSpecification.queue(queueName)))
                .then(sender.bind(BindingSpecification.binding().queue(queueName).exchange(exchangeName)
                        .routingKey("a.b")));

        resources.block(java.time.Duration.ofSeconds(1));

        channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclarePassive(queueName);
    } finally {
        channel.exchangeDelete(exchangeName);
        channel.queueDelete(queueName);
        channel.close();
    }
}