Example usage for com.rabbitmq.client Channel exchangeDeclarePassive

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

Introduction

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

Prototype

Exchange.DeclareOk exchangeDeclarePassive(String name) throws IOException;

Source Link

Document

Declare an exchange passively; that is, check if the named exchange exists.

Usage

From source file:org.mule.transport.amqp.internal.client.AmqpDeclarer.java

License:Open Source License

public String declareExchange(final Channel channel, final ImmutableEndpoint endpoint,
        final boolean activeDeclarationsOnly) throws IOException {
    final String exchangeName = endpointUtil.getExchangeName(endpoint);

    if (endpointUtil.isDefaultExchange(exchangeName)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Using default exchange for endpoint: " + endpoint);
        }//from  ww  w  .  ja v  a 2 s.  com

        return AmqpConnector.ENDPOINT_DEFAULT_EXCHANGE_ALIAS;
    }

    final String exchangeType = endpointUtil.getEndpointType(endpoint);
    if (StringUtils.isNotBlank(exchangeType)) {
        // an exchange type is provided -> the exchange must be declared
        final boolean exchangeDurable = endpointUtil.isExchangeDurable(endpoint);
        final boolean exchangeAutoDelete = endpointUtil.isExchangeAutoDelete(endpoint);

        final Map<String, Object> arguments = endpointUtil.getArguments(endpoint,
                AmqpConnector.ENDPOINT_EXCHANGE_PREFIX);

        channel.exchangeDeclare(exchangeName, exchangeType, exchangeDurable, exchangeAutoDelete, arguments);

        logger.info("Declared exchange: " + exchangeName + " of type: " + exchangeType + ", durable: "
                + exchangeDurable + ", autoDelete: " + exchangeAutoDelete + ", arguments: " + arguments);
    } else if (!activeDeclarationsOnly) {
        // no exchange type -> ensure the exchange exists
        channel.exchangeDeclarePassive(exchangeName);

        if (logger.isDebugEnabled()) {
            logger.debug("Validated presence of exchange: " + exchangeName);
        }
    }

    return exchangeName;
}

From source file:org.mule.transport.amqp.MessageDispatcherItCase.java

License:Open Source License

@Test
@Ignore // AMQP-44
public void testDispatchToNewExchange() throws Exception {
    String bridgeName = "amqpNewExchangeService";
    new MuleClient(muleContext).dispatch(nameFactory.getVmName(bridgeName), "ignored_payload", null);

    // there is no queue bound to this new exchange, so we can only test its
    // presence/*w  w w. j av a2 s  .com*/
    int attempts = 0;
    while (attempts++ < getTestTimeoutSecs() * 2) {
        Channel channel = null;
        try {
            channel = testConnectionManager.getChannel();
            channel.exchangeDeclarePassive(nameFactory.getExchangeName(bridgeName));
            return;
        } catch (IOException ioe) {
            Thread.sleep(500L);
        } finally {
            testConnectionManager.disposeChannel(channel);
        }
    }
    fail("Exchange not created by outbound endpoint");
}

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitMessageChannelBinder.java

License:Apache License

/**
 * Try passive declaration first, in case the user has pre-configured the exchange with incompatible arguments.
 * @param exchange/* www.  j a v a  2s.c  o  m*/
 */
private void declareExchangeIfNotPresent(final Exchange exchange) {
    this.rabbitTemplate.execute(new ChannelCallback<Void>() {

        @Override
        public Void doInRabbit(Channel channel) throws Exception {
            try {
                channel.exchangeDeclarePassive(exchange.getName());
            } catch (IOException e) {
                RabbitMessageChannelBinder.this.rabbitAdmin.declareExchange(exchange);
            }
            return null;
        }

    });
}

From source file:org.springframework.xd.dirt.integration.rabbit.RabbitMessageBus.java

License:Apache License

/**
 * Try passive declaration first, in case the user has pre-configured the exchange with incompatible arguments.
 * @param exchange/*w w  w  .  j a v a 2 s.  c  om*/
 */
private void declareExchangeIfNotPresent(final Exchange exchange) {
    this.rabbitTemplate.execute(new ChannelCallback<Void>() {

        @Override
        public Void doInRabbit(Channel channel) throws Exception {
            try {
                channel.exchangeDeclarePassive(exchange.getName());
            } catch (IOException e) {
                RabbitMessageBus.this.rabbitAdmin.declareExchange(exchange);
            }
            return null;
        }

    });
}

From source file:org.wso2.carbon.event.adapter.rabbitmq.internal.util.RabbitMQEventAdapterUtils.java

License:Open Source License

/**
 * @param connection      Connection to the RabbitMQ
 * @param exchangeName    Name of the exchange
 * @param exchangeType    Type of Exchange
 * @param exchangeDurable Whether durable or not
 * @throws IOException/*from  w w w . j  av  a2s .com*/
 */
public static void declareExchange(Connection connection, String exchangeName, String exchangeType,
        String exchangeDurable) throws IOException, TimeoutException {
    Boolean exchangeAvailable = false;
    Channel channel = connection.createChannel();
    try {
        // check availability of the named exchange.
        // The server will raise an IOException if the named exchange already exists.
        channel.exchangeDeclarePassive(exchangeName);
        exchangeAvailable = true;
    } catch (IOException e) {
        log.info("Exchange :" + exchangeName + " not found.Declaring exchange.");
    }
    if (!exchangeAvailable) {
        // Declare the named exchange if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            if (exchangeType != null && !exchangeType.equals("")) {
                if (exchangeDurable != null && !exchangeDurable.equals("")) {
                    channel.exchangeDeclare(exchangeName, exchangeType, Boolean.parseBoolean(exchangeDurable));
                } else {
                    channel.exchangeDeclare(exchangeName, exchangeType, true);
                }
            } else {
                channel.exchangeDeclare(exchangeName, RabbitMQInputEventAdapterConstants.DEFAULT_EXCHANGE_TYPE,
                        true);
            }
        } catch (IOException e) {
            handleException("Error occurred while declaring exchange.", e);
        }
    }
    channel.close();
}

From source file:org.wso2.carbon.extension.analytics.receiver.rabbitmq.internal.util.RabbitMQUtils.java

License:Open Source License

public static void declareExchange(Connection connection, String exchangeName, String exchangeType,
        String exchangeDurable) throws IOException {
    Boolean exchangeAvailable = false;
    Channel channel = connection.createChannel();
    try {/*  w w  w. ja  v a2  s  .  c  om*/
        // check availability of the named exchange.
        // The server will raise an IOException
        // if the named exchange already exists.
        channel.exchangeDeclarePassive(exchangeName);
        exchangeAvailable = true;
    } catch (IOException e) {
        log.info("Exchange :" + exchangeName + " not found.Declaring exchange.");
    }

    if (!exchangeAvailable) {
        // Declare the named exchange if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            if (exchangeType != null && !exchangeType.equals("")) {
                if (exchangeDurable != null && !exchangeDurable.equals("")) {
                    channel.exchangeDeclare(exchangeName, exchangeType, Boolean.parseBoolean(exchangeDurable));
                } else {
                    channel.exchangeDeclare(exchangeName, exchangeType, true);
                }
            } else {
                channel.exchangeDeclare(exchangeName, "direct", true);
            }
        } catch (IOException e) {
            handleException("Error occurred while declaring exchange.", e);
        }
    }
    channel.close();
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQUtils.java

License:Open Source License

public static void declareExchange(Connection connection, String exchangeName,
        Hashtable<String, String> properties) throws IOException {
    Boolean exchangeAvailable = false;
    Channel channel = connection.createChannel();
    String exchangeType = properties.get(RabbitMQConstants.EXCHANGE_TYPE);
    String durable = properties.get(RabbitMQConstants.EXCHANGE_DURABLE);
    try {/*from w w w  . j av a  2s .c om*/
        // check availability of the named exchange.
        // The server will raise an IOException
        // if the named exchange already exists.
        channel.exchangeDeclarePassive(exchangeName);
        exchangeAvailable = true;
    } catch (IOException e) {
        log.info("Exchange :" + exchangeName + " not found.Declaring exchange.");
    }

    if (!exchangeAvailable) {
        // Declare the named exchange if it does not exists.
        if (!channel.isOpen()) {
            channel = connection.createChannel();
            log.debug("Channel is not open. Creating a new channel.");
        }
        try {
            if (exchangeType != null && !exchangeType.equals("")) {
                if (durable != null && !durable.equals("")) {
                    channel.exchangeDeclare(exchangeName, exchangeType, Boolean.parseBoolean(durable));
                } else {
                    channel.exchangeDeclare(exchangeName, exchangeType, true);
                }
            } else {
                channel.exchangeDeclare(exchangeName, "direct", true);
            }
        } catch (IOException e) {
            handleException("Error occurred while declaring exchange.", e);
        }
    }
    try {
        channel.close();
    } catch (TimeoutException e) {
        log.error("Error occurred while closing connection.", e);
    }
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

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

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

    try {/*from  www.j  a v a  2 s.c  om*/
        CountDownLatch latchCreation = new CountDownLatch(1);
        sender = createSender();
        Disposable resourceCreation = sender.declare(exchange(exchangeName))
                .then(sender.declare(queue(queueName)))
                .then(sender.bind(binding(exchangeName, "a.b", queueName)))
                .doAfterTerminate(() -> latchCreation.countDown()).subscribe();

        assertTrue(latchCreation.await(1, TimeUnit.SECONDS));

        channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclarePassive(queueName);
        resourceCreation.dispose();

        CountDownLatch latchDeletion = new CountDownLatch(1);

        Disposable resourceDeletion = sender.unbind(binding(exchangeName, "a.b", queueName))
                .then(sender.delete(exchange(exchangeName))).then(sender.delete(queue(queueName)))
                .doAfterTerminate(() -> latchDeletion.countDown()).subscribe();

        assertTrue(latchDeletion.await(1, TimeUnit.SECONDS));
        resourceDeletion.dispose();
    } finally {
        try {
            channel.exchangeDeclarePassive(exchangeName);
            fail("The exchange should have been deleted, exchangeDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
            channel = connection.createChannel();
        }
        try {
            channel.queueDeclarePassive(queueName);
            fail("The queue should have been deleted, queueDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
        }
    }
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

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

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

    try {/*from w w  w  . j  a  v  a 2  s  . c o  m*/
        SenderOptions senderOptions = new SenderOptions();
        Mono<Connection> connectionMono = Mono
                .fromCallable(() -> senderOptions.getConnectionFactory().newConnection());
        AtomicInteger channelMonoCalls = new AtomicInteger(0);
        Mono<Channel> channelMono = connectionMono.map(c -> {
            try {
                channelMonoCalls.incrementAndGet();
                return c.createChannel();
            } catch (Exception e) {
                Exceptions.propagate(e);
            }
            return null;
        });
        senderOptions.connectionMono(connectionMono);
        CountDownLatch latchCreation = new CountDownLatch(1);
        sender = createSender(senderOptions);

        ResourceManagementOptions options = new ResourceManagementOptions().channelMono(channelMono);

        Disposable resourceCreation = sender.declare(exchange(exchangeName), options)
                .then(sender.declare(queue(queueName), options))
                .then(sender.bind(binding(exchangeName, "a.b", queueName), options))
                .doAfterTerminate(() -> latchCreation.countDown()).subscribe();

        assertTrue(latchCreation.await(1, TimeUnit.SECONDS));
        assertEquals(3, channelMonoCalls.get());

        channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclarePassive(queueName);
        resourceCreation.dispose();

        CountDownLatch latchDeletion = new CountDownLatch(1);

        Disposable resourceDeletion = sender.unbind(binding(exchangeName, "a.b", queueName), options)
                .then(sender.delete(exchange(exchangeName), options))
                .then(sender.delete(queue(queueName), options))
                .doAfterTerminate(() -> latchDeletion.countDown()).subscribe();

        assertTrue(latchDeletion.await(1, TimeUnit.SECONDS));
        assertEquals(3 + 3, channelMonoCalls.get());
        resourceDeletion.dispose();
    } finally {
        try {
            channel.exchangeDeclarePassive(exchangeName);
            fail("The exchange should have been deleted, exchangeDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
            channel = connection.createChannel();
        }
        try {
            channel.queueDeclarePassive(queueName);
            fail("The queue should have been deleted, queueDeclarePassive should have thrown an exception");
        } catch (IOException e) {
            // OK
        }
    }
}

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 {/*from w  ww  .  j  a v  a  2  s.  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();
    }
}