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.openmrs.module.amqpmodule.utils.impl.PublisherServiceImpl.java

License:Open Source License

@Override
public boolean PublisherCreateConnection() throws java.io.IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.43.123");
    factory.setPort(5672);/*from   www. j  av  a2 s  .c o m*/
    factory.setUsername("chs");
    factory.setPassword("chs123");
    Connection connection = null;
    try {
        connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);

        channel.basicPublish(EXCHANGE_NAME, topic, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
        System.out.println(" [x] Sent '" + msg + "'");

        channel.close();

    } catch (TimeoutException e) {
        System.out.println("Connection Timed out");
        e.printStackTrace();
    }

    connection.close();

    return true;
}

From source file:org.opennaas.extensions.genericnetwork.capability.nclprovisioner.components.NetworkObservationsPusher.java

License:Apache License

private void sendStatistics(String topicName, String csvMessage)
        throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException {

    Connection conn = null;//from  w  ww  .  j  a  v  a  2s .c  o m
    Channel channel = null;

    try {

        log.debug("Establishing RabbitMQ connection with sla manager with URI: " + slaManagerUri);

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(slaManagerUri.getHost());

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

        channel.exchangeDeclare(topicName, "direct");

        log.debug("Publishing message in RabbitMQ channel.");

        channel.basicPublish(topicName, RABBIT_MQ_ROUTING_KEY,
                new AMQP.BasicProperties.Builder().contentType(OBSERVATIONS_CONTENT_TYPE).build(),
                csvMessage.getBytes());

        log.debug("Message successfully sent to SLA manager.");

    } finally {
        if (channel != null && channel.isOpen())
            channel.close();
        if (conn != null && conn.isOpen())
            conn.close();

        log.debug("Connection to RabbitMQ server closed.");

    }

}

From source file:org.pushtrigger.mule.agent.CreateQueueAgent.java

License:Apache License

@Override
public void start() throws MuleException {
    System.out.println("Create Queue Agent is running...");

    Connection connection = null;
    Channel channel = null;
    try {/*from w ww . j a  v  a 2s  .  co  m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World! I am creating a queue...";

        Map<String, Object> props = new HashMap<String, Object>();
        props.put("path", "queue");
        props.put("branch", "create");

        AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
        AMQP.BasicProperties basicProps = bob.headers(props).build();

        channel.basicPublish("", QUEUE_NAME, basicProps, message.getBytes());
        System.out.println("Agent has created the queue...");
    } catch (IOException e) {
        System.out.println("Something wrong " + e);
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException e) {
        }
        try {
            if (connection != null)
                connection.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.smartdeveloperhub.curator.connector.BrokerController.java

License:Apache License

private void closeQuietly(final Channel channel) {
    if (channel.isOpen()) {
        try {/* ww w .  j a va 2 s.c o m*/
            channel.close();
        } catch (final Exception e) {
            LOGGER.trace("Could not close channel gracefully", e);
        }
    }
}

From source file:org.smartdeveloperhub.harvesters.it.notification.ConnectionManagerTest.java

License:Apache License

@Test
public void testThreadLocalChannelLifecycle(@Mocked final Channel currentChannel) throws Exception {
    new MockUp<ConnectionFactory>() {
        @Mock/*ww w.  j a v  a2 s  .  c  om*/
        public void setHost(final String host) {
        }

        @Mock
        public void setPort(final int port) {
        }

        @Mock
        public void setVirtualHost(final String virtualHost) {
        }

        @Mock
        public void setThreadFactory(final ThreadFactory threadFactory) {
        }

        @Mock
        public Connection newConnection() throws IOException, TimeoutException {
            return ConnectionManagerTest.this.connection;
        }
    };
    new Expectations() {
        {
            ConnectionManagerTest.this.connection.createChannel();
            returns(ConnectionManagerTest.this.channel, currentChannel, ConnectionManagerTest.this.channel);
            ConnectionManagerTest.this.connection.isOpen();
            this.result = true;
            currentChannel.isOpen();
            this.times = 1;
            this.result = true;
            currentChannel.close();
            this.times = 1;
        }
    };
    this.sut.connect();
    final Channel aChannel = this.sut.currentChannel();
    assertThat(aChannel, sameInstance(currentChannel));
    this.sut.discardChannel();
    assertThat(this.sut.currentChannel(), sameInstance(this.channel));
}

From source file:org.smartdeveloperhub.harvesters.it.notification.ConnectionManagerTest.java

License:Apache License

@Test
public void testCloseQuietlyObservesChannelStatus(@Mocked final Channel currentChannel) throws Exception {
    new MockUp<ConnectionFactory>() {
        @Mock/* www  .  j a va  2  s .c  o  m*/
        public void setHost(final String host) {
        }

        @Mock
        public void setPort(final int port) {
        }

        @Mock
        public void setVirtualHost(final String virtualHost) {
        }

        @Mock
        public void setThreadFactory(final ThreadFactory threadFactory) {
        }

        @Mock
        public Connection newConnection() throws IOException, TimeoutException {
            return ConnectionManagerTest.this.connection;
        }
    };
    new Expectations() {
        {
            ConnectionManagerTest.this.connection.createChannel();
            returns(ConnectionManagerTest.this.channel, currentChannel);
            ConnectionManagerTest.this.connection.isOpen();
            this.result = true;
            currentChannel.isOpen();
            this.times = 1;
            this.result = false;
            currentChannel.close();
            this.times = 0;
        }
    };
    this.sut.connect();
    final Channel aChannel = this.sut.currentChannel();
    assertThat(aChannel, sameInstance(currentChannel));
    this.sut.discardChannel();
}

From source file:org.smartdeveloperhub.harvesters.it.notification.ConnectionManagerTest.java

License:Apache License

@Test
public void testCloseQuietlySwallowsRegularExceptions(@Mocked final Channel currentChannel) throws Exception {
    new MockUp<ConnectionFactory>() {
        @Mock//from   ww  w  . j a v  a  2  s  .c o m
        public void setHost(final String host) {
        }

        @Mock
        public void setPort(final int port) {
        }

        @Mock
        public void setVirtualHost(final String virtualHost) {
        }

        @Mock
        public void setThreadFactory(final ThreadFactory threadFactory) {
        }

        @Mock
        public Connection newConnection() throws IOException, TimeoutException {
            return ConnectionManagerTest.this.connection;
        }
    };
    new Expectations() {
        {
            ConnectionManagerTest.this.connection.createChannel();
            returns(ConnectionManagerTest.this.channel, currentChannel);
            ConnectionManagerTest.this.connection.isOpen();
            this.result = true;
            currentChannel.isOpen();
            this.times = 1;
            this.result = true;
            currentChannel.close();
            this.times = 1;
            this.result = new IOException("Failure");
        }
    };
    this.sut.connect();
    final Channel aChannel = this.sut.currentChannel();
    assertThat(aChannel, sameInstance(currentChannel));
    this.sut.discardChannel();
}

From source file:org.smartdeveloperhub.harvesters.it.notification.ConnectionManagerTest.java

License:Apache License

@Test
public void testCloseQuietlyFailsOnRuntimeExceptions(@Mocked final Channel currentChannel) throws Exception {
    new MockUp<ConnectionFactory>() {
        @Mock//  w ww . j  a  v a  2 s  . c  om
        public void setHost(final String host) {
        }

        @Mock
        public void setPort(final int port) {
        }

        @Mock
        public void setVirtualHost(final String virtualHost) {
        }

        @Mock
        public void setThreadFactory(final ThreadFactory threadFactory) {
        }

        @Mock
        public Connection newConnection() throws IOException, TimeoutException {
            return ConnectionManagerTest.this.connection;
        }
    };
    new Expectations() {
        {
            ConnectionManagerTest.this.connection.createChannel();
            returns(ConnectionManagerTest.this.channel, currentChannel);
            ConnectionManagerTest.this.connection.isOpen();
            this.result = true;
            currentChannel.isOpen();
            this.times = 1;
            this.result = true;
            currentChannel.close();
            this.times = 1;
            this.result = new Error("Failure");
        }
    };
    this.sut.connect();
    final Channel aChannel = this.sut.currentChannel();
    assertThat(aChannel, sameInstance(currentChannel));
    try {
        this.sut.discardChannel();
        fail("Should fail on runtime exception");
    } catch (final AssertionError e) {
    } catch (final Error e) {
        assertThat(e.getMessage(), equalTo("Failure"));
    }
}

From source file:org.springframework.amqp.rabbit.connection.CachePropertiesTests.java

License:Apache License

@Test
public void testChannelCache() throws Exception {
    Connection c1 = this.channelCf.createConnection();
    Connection c2 = this.channelCf.createConnection();
    assertSame(c1, c2);/*w ww.j  a  va2 s  . c  o m*/
    Channel ch1 = c1.createChannel(false);
    Channel ch2 = c1.createChannel(false);
    Channel ch3 = c1.createChannel(true);
    Channel ch4 = c1.createChannel(true);
    Channel ch5 = c1.createChannel(true);
    ch1.close();
    ch2.close();
    ch3.close();
    ch4.close();
    ch5.close();
    Properties props = this.channelCf.getCacheProperties();
    assertEquals("4", props.getProperty("channelCacheSize"));
    assertEquals("2", props.getProperty("idleChannelsNotTx"));
    assertEquals("3", props.getProperty("idleChannelsTx"));
    assertEquals("2", props.getProperty("idleChannelsNotTxHighWater"));
    assertEquals("3", props.getProperty("idleChannelsTxHighWater"));
    ch1 = c1.createChannel(false);
    ch3 = c1.createChannel(true);
    props = this.channelCf.getCacheProperties();
    assertEquals("1", props.getProperty("idleChannelsNotTx"));
    assertEquals("2", props.getProperty("idleChannelsTx"));
    assertEquals("2", props.getProperty("idleChannelsNotTxHighWater"));
    assertEquals("3", props.getProperty("idleChannelsTxHighWater"));
    ch1 = c1.createChannel(false);
    ch2 = c1.createChannel(false);
    ch3 = c1.createChannel(true);
    ch4 = c1.createChannel(true);
    ch5 = c1.createChannel(true);
    Channel ch6 = c1.createChannel(true);
    Channel ch7 = c1.createChannel(true); // #5
    ch1.close();
    ch2.close();
    ch3.close();
    ch4.close();
    ch5.close();
    ch6.close();
    ch7.close();
    props = this.channelCf.getCacheProperties();
    assertEquals("2", props.getProperty("idleChannelsNotTx"));
    assertEquals("4", props.getProperty("idleChannelsTx")); // not 5
    assertEquals("2", props.getProperty("idleChannelsNotTxHighWater"));
    assertEquals("4", props.getProperty("idleChannelsTxHighWater")); // not 5

}

From source file:org.springframework.amqp.rabbit.connection.CachePropertiesTests.java

License:Apache License

@Test
public void testConnectionCache() throws Exception {
    Connection c1 = this.connectionCf.createConnection();
    Connection c2 = this.connectionCf.createConnection();
    Channel ch1 = c1.createChannel(false);
    Channel ch2 = c1.createChannel(false);
    Channel ch3 = c2.createChannel(true);
    Channel ch4 = c2.createChannel(true);
    Channel ch5 = c2.createChannel(false);
    ch1.close();
    ch2.close();//from   w w w.j av a 2s.co m
    ch3.close();
    ch4.close();
    ch5.close();
    c1.close();
    Properties props = this.connectionCf.getCacheProperties();
    assertEquals("10", props.getProperty("channelCacheSize"));
    assertEquals("5", props.getProperty("connectionCacheSize"));
    assertEquals("2", props.getProperty("openConnections"));
    assertEquals("1", props.getProperty("idleConnections"));
    c2.close();
    props = this.connectionCf.getCacheProperties();
    assertEquals("2", props.getProperty("idleConnections"));
    assertEquals("2", props.getProperty("idleConnectionsHighWater"));
    int c1Port = c1.getLocalPort();
    int c2Port = c2.getLocalPort();
    assertEquals("2", props.getProperty("idleChannelsNotTx:" + c1Port));
    assertEquals("0", props.getProperty("idleChannelsTx:" + c1Port));
    assertEquals("2", props.getProperty("idleChannelsNotTxHighWater:" + c1Port));
    assertEquals("0", props.getProperty("idleChannelsTxHighWater:" + c1Port));
    assertEquals("1", props.getProperty("idleChannelsNotTx:" + c2Port));
    assertEquals("2", props.getProperty("idleChannelsTx:" + c2Port));
    assertEquals("1", props.getProperty("idleChannelsNotTxHighWater:" + c2Port));
    assertEquals("2", props.getProperty("idleChannelsTxHighWater:" + c2Port));
}