Example usage for com.rabbitmq.client Connection close

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

Introduction

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

Prototype

@Override
void close() throws IOException;

Source Link

Document

Close this connection and all its channels with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPBroadcastSenderImpl.java

License:Apache License

public void Send(OMElement message) throws AMQPException {
    try {/* w ww  .  j  a  va  2 s  .  co  m*/
        if (isRoutable(message)) {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT);

            channel.basicPublish(AMQPUtil.EXCHANGE_NAME_FANOUT, "", null, message.toString().getBytes());

            channel.close();
            connection.close();
        }
    } catch (IOException e) {
        throw new AMQPException(e);
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPSenderImpl.java

License:Apache License

public void Send(OMElement message) throws AMQPException {
    try {//  w w  w.ja  va 2 s. c o m
        if (isRoutable(message)) {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_DIRECT, AMQPUtil.EXCHANGE_TYPE_DIRECT);

            List<String> routingKeys = new ArrayList<String>();
            getRoutingKeys(message, routingKeys);

            for (String routingKey : routingKeys) {
                channel.basicPublish(AMQPUtil.EXCHANGE_NAME_DIRECT, routingKey, null,
                        message.toString().getBytes());
            }

            channel.close();
            connection.close();
        }
    } catch (IOException e) {
        throw new AMQPException(e);
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPTopicSenderImpl.java

License:Apache License

public void Send(OMElement message) throws AMQPException {
    try {//  w  w  w.j  av a 2  s. c  o  m
        if (isRoutable(message)) {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_TOPIC, AMQPUtil.EXCHANGE_TYPE_TOPIC);

            List<String> routingKeys = new ArrayList<String>();
            getRoutingKeys(message, routingKeys);

            for (String routingKey : routingKeys) {
                channel.basicPublish(AMQPUtil.EXCHANGE_NAME_TOPIC, routingKey, null,
                        message.toString().getBytes());
            }

            channel.close();
            connection.close();
        }
    } catch (IOException e) {
        throw new AMQPException(e);
    }
}

From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java

License:Apache License

@Test
public void testReadQueue() throws Exception {
    final int maxNumRecords = 10;
    PCollection<RabbitMqMessage> raw = p.apply(RabbitMqIO.read().withUri("amqp://guest:guest@localhost:" + port)
            .withQueue("READ").withMaxNumRecords(maxNumRecords));
    PCollection<String> output = raw.apply(MapElements.into(TypeDescriptors.strings())
            .via((RabbitMqMessage message) -> new String(message.getBody(), StandardCharsets.UTF_8)));

    List<String> records = generateRecords(maxNumRecords).stream()
            .map(record -> new String(record, StandardCharsets.UTF_8)).collect(Collectors.toList());
    PAssert.that(output).containsInAnyOrder(records);

    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
    Connection connection = null;
    Channel channel = null;/*from   w  ww.  ja  va  2  s.c  om*/
    try {
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare("READ", false, false, false, null);
        for (String record : records) {
            channel.basicPublish("", "READ", null, record.getBytes(StandardCharsets.UTF_8));
        }

        p.run();
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java

License:Apache License

@Test(timeout = 60 * 1000)
public void testReadExchange() throws Exception {
    final int maxNumRecords = 10;
    PCollection<RabbitMqMessage> raw = p.apply(RabbitMqIO.read().withUri("amqp://guest:guest@localhost:" + port)
            .withExchange("READEXCHANGE", "fanout", "test").withMaxNumRecords(maxNumRecords));
    PCollection<String> output = raw.apply(MapElements.into(TypeDescriptors.strings())
            .via((RabbitMqMessage message) -> new String(message.getBody(), StandardCharsets.UTF_8)));

    List<String> records = generateRecords(maxNumRecords).stream()
            .map(record -> new String(record, StandardCharsets.UTF_8)).collect(Collectors.toList());
    PAssert.that(output).containsInAnyOrder(records);

    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
    Connection connection = null;
    Channel channel = null;/*from  w ww .  ja  v a  2  s . c  o  m*/
    try {
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare("READEXCHANGE", "fanout");
        Channel finalChannel = channel;
        Thread publisher = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
            for (int i = 0; i < maxNumRecords; i++) {
                try {
                    finalChannel.basicPublish("READEXCHANGE", "test", null,
                            ("Test " + i).getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        });
        publisher.start();
        p.run();
        publisher.join();
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java

License:Apache License

@Test
public void testWriteQueue() throws Exception {
    final int maxNumRecords = 1000;
    List<RabbitMqMessage> data = generateRecords(maxNumRecords).stream()
            .map(bytes -> new RabbitMqMessage(bytes)).collect(Collectors.toList());
    p.apply(Create.of(data))/* ww w  . j  av a2  s. c o  m*/
            .apply(RabbitMqIO.write().withUri("amqp://guest:guest@localhost:" + port).withQueue("TEST"));

    final List<String> received = new ArrayList<>();
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
    Connection connection = null;
    Channel channel = null;
    try {
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare("TEST", true, false, false, null);
        Consumer consumer = new TestConsumer(channel, received);
        channel.basicConsume("TEST", true, consumer);

        p.run();

        while (received.size() < maxNumRecords) {
            Thread.sleep(500);
        }

        assertEquals(maxNumRecords, received.size());
        for (int i = 0; i < maxNumRecords; i++) {
            assertTrue(received.contains("Test " + i));
        }
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java

License:Apache License

@Test
public void testWriteExchange() throws Exception {
    final int maxNumRecords = 1000;
    List<RabbitMqMessage> data = generateRecords(maxNumRecords).stream()
            .map(bytes -> new RabbitMqMessage(bytes)).collect(Collectors.toList());
    p.apply(Create.of(data)).apply(// w ww.  j a  v a2 s  .  co m
            RabbitMqIO.write().withUri("amqp://guest:guest@localhost:" + port).withExchange("WRITE", "fanout"));

    final List<String> received = new ArrayList<>();
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
    Connection connection = null;
    Channel channel = null;
    try {
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare("WRITE", "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, "WRITE", "");
        Consumer consumer = new TestConsumer(channel, received);
        channel.basicConsume(queueName, true, consumer);

        p.run();

        while (received.size() < maxNumRecords) {
            Thread.sleep(500);
        }

        assertEquals(maxNumRecords, received.size());
        for (int i = 0; i < maxNumRecords; i++) {
            assertTrue(received.contains("Test " + i));
        }
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.apache.druid.examples.rabbitmq.RabbitMQProducerMain.java

License:Apache License

public static void main(String[] args) throws Exception {
    // We use a List to keep track of option insertion order. See below.
    final List<Option> optionList = new ArrayList<Option>();

    optionList.add(OptionBuilder.withLongOpt("help").withDescription("display this help message").create("h"));
    optionList.add(OptionBuilder.withLongOpt("hostname").hasArg()
            .withDescription("the hostname of the AMQP broker [defaults to AMQP library default]").create("b"));
    optionList.add(OptionBuilder.withLongOpt("port").hasArg()
            .withDescription("the port of the AMQP broker [defaults to AMQP library default]").create("n"));
    optionList.add(OptionBuilder.withLongOpt("username").hasArg()
            .withDescription("username to connect to the AMQP broker [defaults to AMQP library default]")
            .create("u"));
    optionList.add(OptionBuilder.withLongOpt("password").hasArg()
            .withDescription("password to connect to the AMQP broker [defaults to AMQP library default]")
            .create("p"));
    optionList.add(OptionBuilder.withLongOpt("vhost").hasArg()
            .withDescription("name of virtual host on the AMQP broker [defaults to AMQP library default]")
            .create("v"));
    optionList.add(OptionBuilder.withLongOpt("exchange").isRequired().hasArg()
            .withDescription("name of the AMQP exchange [required - no default]").create("e"));
    optionList.add(OptionBuilder.withLongOpt("key").hasArg()
            .withDescription("the routing key to use when sending messages [default: 'default.routing.key']")
            .create("k"));
    optionList.add(OptionBuilder.withLongOpt("type").hasArg()
            .withDescription("the type of exchange to create [default: 'topic']").create("t"));
    optionList.add(OptionBuilder.withLongOpt("durable")
            .withDescription("if set, a durable exchange will be declared [default: not set]").create("d"));
    optionList.add(OptionBuilder.withLongOpt("autodelete")
            .withDescription("if set, an auto-delete exchange will be declared [default: not set]")
            .create("a"));
    optionList.add(OptionBuilder.withLongOpt("single")
            .withDescription("if set, only a single message will be sent [default: not set]").create("s"));
    optionList.add(OptionBuilder.withLongOpt("start").hasArg()
            .withDescription("time to use to start sending messages from [default: 2010-01-01T00:00:00]")
            .create());/*from  w  w  w .  java  2  s .c om*/
    optionList.add(OptionBuilder.withLongOpt("stop").hasArg().withDescription(
            "time to use to send messages until (format: '2013-07-18T23:45:59') [default: current time]")
            .create());
    optionList.add(OptionBuilder.withLongOpt("interval").hasArg()
            .withDescription("the interval to add to the timestamp between messages in seconds [default: 10]")
            .create());
    optionList.add(OptionBuilder.withLongOpt("delay").hasArg()
            .withDescription("the delay between sending messages in milliseconds [default: 100]").create());

    // An extremely silly hack to maintain the above order in the help formatting.
    HelpFormatter formatter = new HelpFormatter();
    // Add a comparator to the HelpFormatter using the ArrayList above to sort by insertion order.
    //noinspection ComparatorCombinators -- don't replace with comparingInt() to preserve comments
    formatter.setOptionComparator((o1, o2) -> {
        // I know this isn't fast, but who cares! The list is short.
        //noinspection SuspiciousMethodCalls
        return Integer.compare(optionList.indexOf(o1), optionList.indexOf(o2));
    });

    // Now we can add all the options to an Options instance. This is dumb!
    Options options = new Options();
    for (Option option : optionList) {
        options.addOption(option);
    }

    CommandLine cmd = null;

    try {
        cmd = new BasicParser().parse(options, args);
    } catch (ParseException e) {
        formatter.printHelp("RabbitMQProducerMain", e.getMessage(), options, null);
        System.exit(1);
    }

    if (cmd.hasOption("h")) {
        formatter.printHelp("RabbitMQProducerMain", options);
        System.exit(2);
    }

    ConnectionFactory factory = new ConnectionFactory();

    if (cmd.hasOption("b")) {
        factory.setHost(cmd.getOptionValue("b"));
    }
    if (cmd.hasOption("u")) {
        factory.setUsername(cmd.getOptionValue("u"));
    }
    if (cmd.hasOption("p")) {
        factory.setPassword(cmd.getOptionValue("p"));
    }
    if (cmd.hasOption("v")) {
        factory.setVirtualHost(cmd.getOptionValue("v"));
    }
    if (cmd.hasOption("n")) {
        factory.setPort(Integer.parseInt(cmd.getOptionValue("n")));
    }

    String exchange = cmd.getOptionValue("e");
    String routingKey = "default.routing.key";
    if (cmd.hasOption("k")) {
        routingKey = cmd.getOptionValue("k");
    }

    boolean durable = cmd.hasOption("d");
    boolean autoDelete = cmd.hasOption("a");
    String type = cmd.getOptionValue("t", "topic");
    boolean single = cmd.hasOption("single");
    int interval = Integer.parseInt(cmd.getOptionValue("interval", "10"));
    int delay = Integer.parseInt(cmd.getOptionValue("delay", "100"));

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
    Date stop = sdf.parse(cmd.getOptionValue("stop", sdf.format(new Date())));

    Random r = ThreadLocalRandom.current();
    Calendar timer = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH);
    timer.setTime(sdf.parse(cmd.getOptionValue("start", "2010-01-01T00:00:00")));

    String msg_template = "{\"utcdt\": \"%s\", \"wp\": %d, \"gender\": \"%s\", \"age\": %d}";

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

    channel.exchangeDeclare(exchange, type, durable, autoDelete, null);

    do {
        int wp = (10 + r.nextInt(90)) * 100;
        String gender = r.nextBoolean() ? "male" : "female";
        int age = 20 + r.nextInt(70);

        String line = StringUtils.format(msg_template, sdf.format(timer.getTime()), wp, gender, age);

        channel.basicPublish(exchange, routingKey, null, StringUtils.toUtf8(line));

        System.out.println("Sent message: " + line);

        timer.add(Calendar.SECOND, interval);

        Thread.sleep(delay);
    } while ((!single && stop.after(timer.getTime())));

    connection.close();
}

From source file:org.apache.druid.firehose.rabbitmq.RabbitMQFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(final InputRowParser<ByteBuffer> firehoseParser, File temporaryDirectory)
        throws IOException {
    ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory);
    Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries())
            .withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds()))
            .withMaxDuration(Duration.seconds(config.getMaxDurationSeconds())));

    String queue = config.getQueue();
    String exchange = config.getExchange();
    String routingKey = config.getRoutingKey();

    boolean durable = config.isDurable();
    boolean exclusive = config.isExclusive();
    boolean autoDelete = config.isAutoDelete();

    final Connection connection = Connections.create(lyraOptions, lyraConfig);

    connection.addShutdownListener(new ShutdownListener() {
        @Override//from   ww  w  .j av a  2  s.com
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Connection closed!");
        }
    });

    final Channel channel = connection.createChannel();
    channel.queueDeclare(queue, durable, exclusive, autoDelete, null);
    channel.queueBind(queue, exchange, routingKey);
    channel.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Channel closed!");
        }
    });

    // We create a QueueingConsumer that will not auto-acknowledge messages since that
    // happens on commit().
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);

    return new Firehose() {
        /**
         * Storing the latest row as a member variable should be safe since this will only be run
         * by a single thread.
         */
        private InputRow nextRow;

        /**
         * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to
         * and including this tag. See commit() for more detail.
         */
        private long lastDeliveryTag;

        private Iterator<InputRow> nextIterator = Collections.emptyIterator();

        @Override
        public boolean hasMore() {
            nextRow = null;
            try {
                if (nextIterator.hasNext()) {
                    nextRow = nextIterator.next();
                    return true;
                }
                // Wait for the next delivery. This will block until something is available.
                final Delivery delivery = consumer.nextDelivery();
                if (delivery != null) {
                    lastDeliveryTag = delivery.getEnvelope().getDeliveryTag();
                    nextIterator = firehoseParser.parseBatch(ByteBuffer.wrap(delivery.getBody())).iterator();
                    if (nextIterator.hasNext()) {
                        nextRow = nextIterator.next();
                        // If delivery is non-null, we report that there is something more to process.
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                // A little unclear on how we should handle this.

                // At any rate, we're in an unknown state now so let's log something and return false.
                log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen.");
            }

            // This means that delivery is null or we caught the exception above so we report that we have
            // nothing more to process.
            return false;
        }

        @Nullable
        @Override
        public InputRow nextRow() {
            if (nextRow == null) {
                //Just making sure.
                log.wtf("I have nothing in delivery. Method hasMore() should have returned false.");
                return null;
            }

            return nextRow;
        }

        @Override
        public Runnable commit() {
            // This method will be called from the same thread that calls the other methods of
            // this Firehose. However, the returned Runnable will be called by a different thread.
            //
            // It should be (thread) safe to copy the lastDeliveryTag like we do below and then
            // acknowledge values up to and including that value.
            return new Runnable() {
                // Store (copy) the last delivery tag to "become" thread safe.
                final long deliveryTag = lastDeliveryTag;

                @Override
                public void run() {
                    try {
                        log.info("Acknowledging delivery of messages up to tag: " + deliveryTag);

                        // Acknowledge all messages up to and including the stored delivery tag.
                        channel.basicAck(deliveryTag, true);
                    } catch (IOException e) {
                        log.error(e, "Unable to acknowledge message reception to message queue.");
                    }
                }
            };
        }

        @Override
        public void close() throws IOException {
            log.info("Closing connection to RabbitMQ");
            channel.close();
            connection.close();
        }
    };
}

From source file:org.apache.flume.amqp.AmqpConsumer.java

License:Apache License

private void closeConnectionSilently(Connection connection) {
    if (connection != null) {
        try {//w  ww  .ja  va  2s.c  o m
            LOG.info("Closing connection to {} ...", connectionFactory.getHost());
            connection.close();
            LOG.info("Connection to {} closed", connectionFactory.getHost());
        } catch (AlreadyClosedException e) {
            // we catch this specifically so we don't pollute the logs with already closed exceptions

        } catch (Exception e) {
            LOG.warn("Problem closing down connection", e);
        }
    }
}