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:org.voltdb.bulkloader.RMQCSVSend.java

License:Open Source License

private static void sendMessages(RMQOptions rmqOpts, RandomSleeper.RSOptions sleeperOpts, TestOptions testOpts)
        throws InterruptedException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rmqOpts.host);//from   w w  w .j a v  a2 s . c om

    Connection connection = null;
    Channel channel = null;
    String exchangeName = "";
    // Use the queue name if the routing key is not specified.
    String routingKey = rmqOpts.routing != null ? rmqOpts.routing : rmqOpts.queue;
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        if (rmqOpts.exchange != null) {
            exchangeName = rmqOpts.exchange;
            channel.exchangeDeclare(exchangeName, rmqOpts.extype);
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(255);
    }

    try {
        channel.queueDeclare(rmqOpts.queue, rmqOpts.persistent, false, false, null);
        try {
            while (testOpts.lineIter.hasNext()) {
                String message = testOpts.lineIter.next();
                channel.basicPublish(exchangeName, routingKey, MessageProperties.TEXT_PLAIN,
                        message.getBytes());
                System.out.printf(" [x] Sent '%s'\n", message);
                sleeperOpts.sleeper.sleep();
            }
        } finally {
            testOpts.lineIter.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.wildfly.connector.rabbitmq.ConnectorTestCase.java

License:Open Source License

@Test
public void testConnectionFactory() throws Exception {
    Assert.assertNotNull(connectionFactory1);
    Assert.assertNotNull(queue);//w  ww. j  a v  a  2  s . com

    RabbitmqConnection connection = connectionFactory1.getConnection();
    Assert.assertNotNull(connection);
    String queueName = "testing";
    Channel channel = connection.createChannel();
    channel.queueDeclare(queueName, false, false, false, null);
    String message = "Hello World!";

    final CountDownLatch counter = new CountDownLatch(1);
    Consumer consume = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
                byte[] body) throws IOException {
            Assert.assertEquals("Hello World!", new String(body));
            counter.countDown();
        }
    };

    channel.basicConsume(queueName, true, consume);
    channel.basicPublish("", queueName, null, message.getBytes());
    counter.await(10, TimeUnit.SECONDS);
    Assert.assertEquals(0, counter.getCount());
    channel.close();

}

From source file:org.wso2.carbon.esb.rabbitmq.message.store.jira.ESBJAVA4569RabbiMQSSLStoreWithClientCertValidationTest.java

License:Open Source License

/**
 * Helper method to retrieve queue message from rabbitMQ
 *
 * @return result/*  w w w  .  ja v a 2s.c  o  m*/
 * @throws Exception
 */
private static String consumeWithoutCertificate() throws Exception {
    String result = "";

    String basePath = TestConfigurationProvider.getResourceLocation()
            + "/artifacts/ESB/messageStore/rabbitMQ/SSL/";

    String truststoreLocation = basePath + "rabbitMQ/certs/client/rabbitstore";
    String keystoreLocation = basePath + "rabbitMQ/certs/client/keycert.p12";

    char[] keyPassphrase = "MySecretPassword".toCharArray();
    KeyStore ks = KeyStore.getInstance("PKCS12");
    ks.load(new FileInputStream(keystoreLocation), keyPassphrase);

    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, keyPassphrase);

    char[] trustPassphrase = "rabbitstore".toCharArray();
    KeyStore tks = KeyStore.getInstance("JKS");
    tks.load(new FileInputStream(truststoreLocation), trustPassphrase);

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    tmf.init(tks);

    SSLContext c = SSLContext.getInstance("SSL");
    c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5671);
    factory.useSslProtocol(c);

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

    GetResponse chResponse = channel.basicGet("WithClientCertQueue", true);
    if (chResponse != null) {
        byte[] body = chResponse.getBody();
        result = new String(body);
    }
    channel.close();
    conn.close();
    return result;
}

From source file:org.wso2.carbon.esb.rabbitmq.message.store.jira.ESBJAVA4569RabbiMQSSLStoreWithoutClientCertValidationTest.java

License:Open Source License

/**
 * Helper method to retrieve queue message from rabbitMQ
 *
 * @return result//  w ww. j a  v  a2  s.co  m
 * @throws Exception
 */
private static String consumeWithoutCertificate() throws Exception {
    String result = "";
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5671);
    factory.useSslProtocol();

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

    GetResponse chResponse = channel.basicGet("WithoutClientCertQueue", true);
    if (chResponse != null) {
        byte[] body = chResponse.getBody();
        result = new String(body);
    }
    channel.close();
    conn.close();
    return result;
}

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 ww w. ja  va  2s  .  c  o m
 */
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.event.adapter.rabbitmq.internal.util.RabbitMQOutputEventAdapterPublisher.java

License:Open Source License

public void close(int tenantId) throws OutputEventAdapterException {
    LOGGER.debug("*** DEBUG RabbitMQOutputEventAdapterPublisher().close()");

    Channel channel = cache.remove(tenantId);

    if (channel != null) {
        try {//  w w  w . j av a  2s  .  c  o  m
            channel.close();
            //channel.getConnection().close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOGGER.warn("RabbitMQ connection was closed already", e);
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.wso2.carbon.event.adaptor.rabbitmq.output.RabbitMQOutputEventAdaptorType.java

License:Open Source License

/**
 * <pre>// w w  w . j a v a2s .  c  om
 * removeConnectionInfo
 * <pre>
 * 
 * @param outputEventAdaptorMessageConfiguration
 * @param outputEventAdaptorConfiguration
 * @param tenantId
 * @see org.wso2.carbon.event.output.adaptor.core.AbstractOutputEventAdaptor#removeConnectionInfo(org.wso2.carbon.event.output.adaptor.core.message.config.OutputEventAdaptorMessageConfiguration, org.wso2.carbon.event.output.adaptor.core.config.OutputEventAdaptorConfiguration, int)
 */
@Override
public void removeConnectionInfo(OutputEventAdaptorMessageConfiguration outputEventAdaptorMessageConfiguration,
        OutputEventAdaptorConfiguration outputEventAdaptorConfiguration, int tenantId) {

    Channel channel = cache.remove(tenantId);
    if (channel != null) {
        try {
            channel.close();
            //channel.getConnection().close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            LOGGER.warn("RabbitMQ connection was closed already", e);
        }
    }
}

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 {/*from  w  w  w  .j  a v  a2  s.  com*/
        // 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  . ja va2s . co  m*/
        // 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:org.wso2.siddhi.debs2017.input.DebsBenchmarkSystem.java

License:Open Source License

@Override
public void close() {

    try {//from  ww w.j a va  2 s.  c o  m
        super.close();
        Channel channel = inputQueue.getChannel();
        Connection connection = channel.getConnection();
        channel.close();
        connection.close();
        channel = outputQueue.getChannel();
        connection = channel.getConnection();
        channel.close();
        connection.close();
    } catch (Exception e) {
        logger.debug("Exception", e);
    }
}