Example usage for com.rabbitmq.client ConnectionFactory newConnection

List of usage examples for com.rabbitmq.client ConnectionFactory newConnection

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory newConnection.

Prototype

public Connection newConnection() throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

From source file:com.github.dann.wspusher.pubsub.subscriber.impl.RabbitMQSubscriberImpl.java

License:Apache License

@Override
public void subscribe(String exchange, DataPusherWebSocket webSocket) {
    Connection connection = null;
    Channel channel = null;//  w w  w. j a v a  2 s.  com

    try {
        // FIXME Cache connection!
        ConnectionFactory factory = new ConnectionFactory();
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.exchangeDeclare(exchange, DEFAULT_EXCHANGE_TYPE);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, exchange, "");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);

        doWork(consumer, webSocket);

    } catch (Exception e) {
        logger.error("Error occured", e);
        throw new WsRuntimeException(e);

    } finally {
        RabbitMQResourceUtils.closeQuietly(channel);
        RabbitMQResourceUtils.closeQuietly(connection);
    }

}

From source file:com.github.hexsmith.rabbitmq.consumer.MessageConsumer.java

License:Open Source License

public boolean consume(String queueName) {
    //RabbitMQ/*from   w  w w .j  a  v a 2s.co  m*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    Connection connection = null;
    Channel channel = null;
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        //queue??queue
        //?queueDeclare???queue
        channel.queueDeclare(queueName, false, false, false, null);

        //?DefaultConsumerhandleDelivery???getByte()???String
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                logger.info("Received '" + message + "'");
            }
        };
        //??
        channel.basicConsume(queueName, true, consumer);

        //???rabbitMQ

    } catch (IOException | TimeoutException e) {
        //?false
        logger.error("send message failed!", e);
        return false;
    }

    return true;
}

From source file:com.github.hexsmith.rabbitmq.consumer.MultiMessageConsumer.java

License:Open Source License

public boolean consume(String queueName, String consumerName) {
    //RabbitMQ//from w  w w  . j a  v a2 s . c o m
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    Connection connection = null;
    Channel channel = null;
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        //queue??queue
        //?queueDeclare???queue
        channel.queueDeclare(queueName, false, false, false, null);

        //?DefaultConsumerhandleDelivery???getByte()???String
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                logger.info("[{}]" + "Received '" + message + "'", consumerName);
            }
        };
        //??
        channel.basicConsume(queueName, true, consumer);

        //???rabbitMQ

    } catch (IOException | TimeoutException e) {
        //?false
        logger.error("send message failed!", e);
        return false;
    }

    return true;
}

From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java

License:Open Source License

public boolean sendMessage(String message) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.7");
    Connection connection = null;
    Channel channel = null;/*from  w  w w.  j  a va  2 s. co  m*/
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        logger.info("send message = {}", message);
        channel.close();
        connection.close();
    } catch (IOException | TimeoutException e) {
        logger.error("send message failed!,exception message is {}", e);
        return false;
    }
    return true;
}

From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java

License:Open Source License

public boolean sendMulitMessage(String message) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.7");
    Connection connection = null;
    Channel channel = null;/*from   w  ww. j  a va  2 s  .  c o m*/
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        int i = 0;
        int loop = 0;
        String originalMessage = message;
        while (loop < 10000) {
            loop++;
            message += i++;
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            logger.info("send message = {}", message);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            message = originalMessage;
        }

        channel.close();
        connection.close();
    } catch (IOException | TimeoutException e) {
        logger.error("send message failed!,exception message is {}", e);
        return false;
    }
    return true;
}

From source file:com.github.kislayverma.dredd.action.async.amqp.AmqpActionQueue.java

License:Apache License

public AmqpActionQueue(ConnectionFactory factory, String queue, String exchange, String routingKey)
        throws IOException, TimeoutException {
    this.factory = factory;
    this.queue = queue;
    this.exchange = exchange;
    this.routingKey = routingKey;

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchange, "direct", true);
    channel.queueDeclare(queue, true, false, false, null);
    channel.queueBind(queue, exchange, routingKey);
}

From source file:com.github.liyp.rabbitmq.demo2.Producer.java

License:Apache License

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername("liyp");
    factory.setPassword("liyp");
    factory.setVirtualHost("/");
    factory.setHost("127.0.0.1");
    factory.setPort(5672);/*from w w  w  .j av  a  2s  .  c o m*/
    Connection conn = factory.newConnection();
    Channel channel = conn.createChannel();

    for (int i = 0; i < 10000; i++) {
        byte[] messageBodyBytes = "Hello, world!".getBytes();
        channel.basicPublish("", "my-queue", null, messageBodyBytes);
        System.out.println(channel.isOpen());
    }

    channel.close();
    conn.close();
}

From source file:com.github.liyp.rabbitmq.rpc.RPCClient.java

License:Apache License

public RPCClient() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    connection = factory.newConnection();
    channel = connection.createChannel();

    replyQueueName = channel.queueDeclare().getQueue();
    consumer = new QueueingConsumer(channel);
    channel.basicConsume(replyQueueName, true, consumer);
}

From source file:com.github.liyp.rabbitmq.rpc.RPCServer.java

License:Apache License

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

        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 (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:com.googlecode.jmxtrans.model.output.RabbitMQWriter.java

License:Open Source License

private Channel createChannel() throws Exception {
    ConnectionFactory cf = new ConnectionFactory();
    cf.setUri(this.uri);
    Connection connection = cf.newConnection();
    return connection.createChannel();
}