Example usage for com.rabbitmq.client ConnectionFactory ConnectionFactory

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

Introduction

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

Prototype

ConnectionFactory

Source Link

Usage

From source file:com.boulmier.machinelearning.jobexecutor.consumer.RequestConsumer.java

public RequestConsumer(InetAddress vmscheduler_ip, Integer port) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(vmscheduler_ip.getHostAddress());
    if (port != null) {
        factory.setPort(port);//from  ww w  .  j  a v  a2s. com
    }
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);
}

From source file:com.buzz.buzzdata.RMQBuzz.java

private Channel getChannel(String queue) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("hyena.rmq.cloudamqp.com");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(queue, false, false, false, null);
    return channel;
}

From source file:com.buzz.dbhandlers.RMQHandler.java

public RMQHandler(String host, String username, String passwd, String queue) throws IOException {
    Queue = queue;/*from   w w  w  . j a v  a  2s .  c  om*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    factory.setUsername(username);
    factory.setPassword(passwd);
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.queueDeclare(Queue, false, false, false, null);
}

From source file:com.caucho.v5.pipe.rabbit.RabbitPipeImpl.java

License:Open Source License

private void connect() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(_config.uri());//from   w  w  w .  j  a va2  s. com

    try {
        _conn = factory.newConnection();
        _channel = _conn.createChannel();

        AMQP.Queue.DeclareOk responseQueue = _channel.queueDeclare(_config.queue(), _config.durable(),
                _config.exclusive(), _config.autoDelete(), null);

        if (!"".equals(_config.exchange())) {
            AMQP.Exchange.DeclareOk responseExchange = _channel.exchangeDeclare(_config.exchange(),
                    _config.exchangeType(), _config.durable(), _config.autoDelete(), false, null);

            _channel.queueBind(responseQueue.getQueue(), _config.exchange(), _config.routingKey());
        }

        _logger.log(Level.INFO, "connect: " + _id + ", actual queue=" + responseQueue.getQueue() + ", "
                + _config + " . " + _self);
    } catch (Exception e) {
        closeChannel();
        closeConnection();

        throw e;
    }
}

From source file:com.chat.UI.java

private void initRabbitMQ() {
    try {//from  w w w.j av  a2s.  co m
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(jLabel3.getText().trim());
        connectionFactory.setUsername("shoshi");
        connectionFactory.setPassword("shoshi");
        //            connectionFactory.setHost("192.168.0.101");
        connectionFactory.setPort(5672);
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();

        channel.exchangeDeclare(Config.E_ALL, "fanout", false);
        channel.exchangeDeclare(Config.E_TANNEE_NAHID, "fanout", false);
        channel.exchangeDeclare(Config.E_TANNEE_SHOSHI, "fanout", false);
        channel.exchangeDeclare(Config.E_NAHID_SHOSHI, "fanout", false);

        channel.queueDeclare(Config.Q_NAHID_ALL, false, false, false, null);
        channel.queueDeclare(Config.Q_NAHID_SHOSHI, false, false, false, null);
        channel.queueDeclare(Config.Q_NAHID_TANNEE, false, false, false, null);

        channel.queueDeclare(Config.Q_SHOSHI_ALL, false, false, false, null);
        channel.queueDeclare(Config.Q_SHOSHI_TANNEE, false, false, false, null);
        channel.queueDeclare(Config.Q_SHOSHI_NAHID, false, false, false, null);

        channel.queueDeclare(Config.Q_TANNEE_ALL, false, false, false, null);
        channel.queueDeclare(Config.Q_TANNEE_SHOSHI, false, false, false, null);
        channel.queueDeclare(Config.Q_TANNEE_NAHID, false, false, false, null);

        channel.queueBind(Config.Q_NAHID_ALL, Config.E_ALL, Config.E_ALL);
        channel.queueBind(Config.Q_NAHID_SHOSHI, Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI);
        channel.queueBind(Config.Q_NAHID_TANNEE, Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID);

        channel.queueBind(Config.Q_SHOSHI_ALL, Config.E_ALL, Config.E_ALL);
        channel.queueBind(Config.Q_SHOSHI_TANNEE, Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI);
        channel.queueBind(Config.Q_SHOSHI_NAHID, Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI);

        channel.queueBind(Config.Q_TANNEE_ALL, Config.E_ALL, Config.E_ALL);
        channel.queueBind(Config.Q_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI);
        channel.queueBind(Config.Q_TANNEE_NAHID, Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID);

        System.out.println("user: " + user);
        if (user.equalsIgnoreCase("NAHID")) {
            forNahid();
            System.out.println("user: " + user);
        } else if (user.equalsIgnoreCase("TANNEE")) {
            forTannee();
            System.out.println("user: " + user);
        } else if (user.equalsIgnoreCase("SHOSHI")) {
            forShoshi();
            System.out.println("user: " + user);
        }
    } catch (IOException | TimeoutException ex) {
        //            if(ex.toString().contains("UnknownHostException")){
        //                if(tryCounter++ < 2){
        //                    jLabel3.setText(host);
        //                    initRabbitMQ();
        //                }else{
        //                    tryCounter = 0;
        //                    jLabel3.setText("localhost");
        //                    host = "localhost";
        //                    initRabbitMQ();
        //                }
        //            }
        Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.codio.collab.core.queue.QueueFactory.java

License:Apache License

private void openConnection() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setVirtualHost(getConnectionConfig().getVhost());
    factory.setUsername(getConnectionConfig().getUser());
    factory.setPassword(getConnectionConfig().getPassword());
    factory.setPort(getConnectionConfig().getPort());
    factory.setHost(getConnectionConfig().getHost());
    factory.setAutomaticRecoveryEnabled(true);
    try {//from  ww w. j a v  a 2 s .com
        connection = factory.newConnection();
    } catch (IOException e) {
        LOG.error("Can not open rmq connection {}", e.getMessage());
    }
}

From source file:com.colm.app.Main.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Connection connection;//from   www . jav a  2  s  .c  om
    Channel channel;
    String replyQueueName;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.0.13");
    factory.setPassword("admin");
    factory.setUsername("admin");
    factory.setVirtualHost("admin");
    connection = factory.newConnection();
    channel = connection.createChannel();
    String queneNAme = "rabbitmq_test";
    channel.queueDeclare(queneNAme, false, false, false, null);
    String message = "Something Else";
    for (int i = 0; i < 1000; i++) {
        channel.basicPublish("", queneNAme, null, message.getBytes());
    }
    System.out.println("Sent '" + message + "'");
    channel.close();
    connection.close();

}

From source file:com.datatorrent.contrib.rabbitmq.AbstractRabbitMQInputOperator.java

License:Open Source License

@Override
public void activate(OperatorContext ctx) {
    try {// w w  w  .  j a  v  a  2 s .  co  m
        connFactory = new ConnectionFactory();
        connFactory.setHost(host);
        if (port != 0) {
            connFactory.setPort(port);
        }

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

        channel.exchangeDeclare(exchange, exchangeType);
        if (queueName == null) {
            // unique queuename is generated
            // used in case of fanout exchange
            queueName = channel.queueDeclare().getQueue();
        } else {
            // user supplied name
            // used in case of direct exchange
            channel.queueDeclare(queueName, true, false, false, null);
        }

        channel.queueBind(queueName, exchange, routingKey);

        //      consumer = new QueueingConsumer(channel);
        //      channel.basicConsume(queueName, true, consumer);
        tracingConsumer = new TracingConsumer(channel);
        cTag = channel.basicConsume(queueName, true, tracingConsumer);
    } catch (IOException ex) {
        throw new RuntimeException("Connection Failure", ex);
    }
}

From source file:com.DeadLetterReceiver.java

License:Open Source License

public static void main(String[] args)
        throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOSTNAME);//from   w  w  w.ja  v  a2 s  . c om
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    //creating reply queue
    try {
        channel.queueDeclarePassive(QUEUE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    }

    System.out.println(" [*] Waiting for responses. To exit press CTRL+C");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        BasicProperties props = delivery.getProperties();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Response received '" + message + "'");
        System.out.println("Correlation id : " + props.getCorrelationId());
    }
}

From source file:com.DeadLetterSender.java

License:Open Source License

public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOSTNAME);//from   w  w w  .j a va 2 s . c o  m
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    try {
        channel.exchangeDeclarePassive(EXCHANGE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);
    }

    try {
        channel.queueDeclarePassive(QUEUE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    }
    channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, QUEUE_NAME);

    // Request Message
    String message = "<soapenv:Envelope" + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
            + "<soapenv:Body>\n" + "  <p:greet xmlns:p=\"http://service.wso2.org\">\n" + "     <in>" + "IBM"
            + "</in>\n" + "  </p:greet>\n" + "</soapenv:Body>\n" + "</soapenv:Envelope>";

    // Adding Message Properties
    AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
    builder.messageId("007");
    builder.contentType("text/xml");
    builder.correlationId("1111");
    builder.replyTo(REPLY_QUEUE_NAME);
    builder.contentEncoding("UTF-8");

    // Custom user properties
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("SOAP_ACTION", "getQuote");
    builder.headers(headers);

    // Publish the message to exchange
    channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, builder.build(), message.getBytes());
    channel.close();
    connection.close();
}