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:dk.getcreditscore.messaging.Send.java

public static void sendMessage(String message) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

    channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());

    channel.close();/*from  w  w  w. j a  va  2s.  c  o  m*/
    connection.close();
}

From source file:dummyloanbroker.DummyLoanBroker.java

public static void main(String[] args) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("student");
    factory.setPassword("cph");
    com.rabbitmq.client.Connection connection = factory.newConnection();
    com.rabbitmq.client.Channel channel = connection.createChannel();
    String corrId = java.util.UUID.randomUUID().toString();

    LoanRequestDTO loanRequest = new LoanRequestDTO("123456-7890", 456289.0, 25, -1);

    Gson gson = new Gson();

    String message = gson.toJson(loanRequest);

    AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().correlationId(corrId).build();
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

    channel.basicPublish("", TASK_QUEUE_NAME, props, message.getBytes());

    channel.close();/*from  w  ww.  j  a va  2  s.  c  o m*/
    connection.close();
}

From source file:edu.iit.rabbitmq.Send.java

/**
 *
 * @param message//from   w  w  w .j  a  va2s.c o  m
 * @throws Exception
 */
public void sendMessage(String message) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(RABBITMQ);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    try {
        channel.queueDeclare(QUEUENAME, false, false, false, null);
    } catch (Exception e) {
        System.out.println("Queue already exists, moving on");
    }

    channel.basicPublish("", QUEUENAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

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

From source file:edu.jhu.pha.vospace.QueueConnector.java

License:Apache License

public static void close(Connection c) {
    if (c != null) {
        try {//w  ww .j  a v  a2  s  .  com
            c.close();
        } catch (Exception ignored) {
        }
    }
}

From source file:edu.kit.dama.util.test.RabbitMQTest.java

License:Apache License

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, false, null);
    //channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    /*String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");*/

    String message = "Hello!";

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

    channel.close();//from w  ww  . j a  v  a2 s . com
    connection.close();

}

From source file:es.devcircus.rabbitmq_examples.rabbitmq_hello_world.Send.java

License:Open Source License

/**
 * /*  www  . java 2s . c  o  m*/
 * @param argv
 * @throws Exception 
 */
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

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

From source file:es.devcircus.rabbitmq_examples.rabbitmq_publish_subscribe.EmitLog.java

License:Open Source License

/**
 *
 * @param argv/*from  www .  j  av a 2s  .  c  o m*/
 * @throws Exception
 */
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

    String message = getMessage(argv);

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

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

From source file:es.devcircus.rabbitmq_examples.rabbitmq_routing.EmitLogDirect.java

License:Open Source License

/**
 *
 * @param argv//from w w  w .j  a v  a 2  s.com
 * @throws Exception
 */
public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    //        factory.setHost("localhost");
    factory.setHost("192.168.0.202");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "direct");

    String severity = getSeverity(argv);
    String message = getMessage(argv);

    channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());
    System.out.println(" [x] Sent '" + severity + "':'" + message + "'");

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

From source file:es.devcircus.rabbitmq_examples.rabbitmq_rpc.RPCServer.java

License:Open Source License

/**
 *
 * @param argv//from   ww  w  .j  a  va 2  s .c  om
 */
public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        //            factory.setHost("localhost");
        factory.setHost("192.168.0.202");
        //            factory.setPort(5671);

        //            factory.useSslProtocol();

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

        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);

        channel.basicQos(1);

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

        System.out.println(" [x] Awaiting RPC requests");

        while (true) {
            String response = null;

            QueueingConsumer.Delivery delivery = consumer.nextDelivery();

            BasicProperties props = delivery.getProperties();
            BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                    .build();

            try {
                String message = new String(delivery.getBody(), "UTF-8");
                int n = Integer.parseInt(message);

                System.out.println(" [.] fib(" + message + ")");
                response = "" + fib(n);
            } catch (Exception e) {
                System.out.println(" [.] " + e.toString());
                response = "";
            } finally {
                channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));

                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    } catch (IOException | InterruptedException | ShutdownSignalException | ConsumerCancelledException e) {
        System.out.println(e.toString());
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:es.devcircus.rabbitmq_examples.rabbitmq_topics.EmitLogTopic.java

License:Open Source License

/**
 * /*from   w w  w .j  a  v  a  2s  .c om*/
 * @param argv 
 */
public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String routingKey = getRouting(argv);
        String message = getMessage(argv);

        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}