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:RabbitMQsender.java

public void send(String[] args) {

    if (args.length != 4) {
        logger.warn(/*from ww w.ja v a2  s. c  o m*/
                "Bad number of arguments, Sender needs String hostURL, String queueName, String fileName, int sleepTime");
        return;
    }

    String hostURL = args[0];
    String queueName = args[1];
    String fileName = args[2];
    int sleepTime = Integer.parseInt(args[3]);
    File myFile = new File(fileName);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostURL);

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

        //send contents of file
        FileReader inputFile = new FileReader(myFile);
        BufferedReader bufferReader = new BufferedReader(inputFile);
        String line = bufferReader.readLine();

        logger.info("Sending file contents");

        do {
            if (sleepTime > 0) {
                Thread.sleep(sleepTime);
            }
            if (line != null) {
                String toSend = addTimestamp(line);
                this.publish(channel, queueName, toSend); //will be dropped till queue is declared (so, declare)
                if (logger.isDebugEnabled()) {
                    logger.debug("Sending '" + toSend + "' from file " + myFile.getAbsolutePath());
                }
                line = bufferReader.readLine();
            }
        } while (line != null);
        bufferReader.close();
        channel.close();
        connection.close();
    } catch (Exception ex) {
        logger.error("Error while reading file line by line: " + ex.getMessage());
        return;
    }
    logger.info("Everything sent without errors\n");
}

From source file:amqp.AmqpClient.java

License:Apache License

protected AmqpClient() {
    this(new ConnectionFactory());
}

From source file:amqp.AmqpClient.java

License:Apache License

protected AmqpClient(String host, int port) {
    connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(host);
    connectionFactory.setPort(port);
}

From source file:amqp.AmqpClient.java

License:Apache License

protected AmqpClient(String host, int port, String virtualHost, String username, String password) {
    connectionFactory = new ConnectionFactory();
    connectionFactory.setHost(host);//  ww  w.  j av  a2  s. co  m
    connectionFactory.setPort(port);
    connectionFactory.setVirtualHost(virtualHost);
    connectionFactory.setUsername(username);
    connectionFactory.setPassword(password);
}

From source file:at.ac.tuwien.dsg.cloud.utilities.messaging.lightweight.rabbitMq.RabbitMqFactory.java

public ConnectionFactory getConnectionFactory() {
    return new ConnectionFactory();
}

From source file:at.ac.tuwien.dsg.comot.m.core.test.utils.TeAgentAdapter.java

License:Apache License

public TeAgentAdapter(String adapterId, String host) throws IOException {

    this.adapterId = adapterId;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);/*from ww w.j  ava 2 s.  c o m*/
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.queueDeclare(queueNameLifecycle(), false, false, true, null);

    channel.queueBind(queueNameLifecycle(), Constants.EXCHANGE_LIFE_CYCLE, "#");
    channel.queueBind(queueNameLifecycle(), Constants.EXCHANGE_CUSTOM_EVENT, "#");
    channel.queueBind(queueNameLifecycle(), Constants.EXCHANGE_EXCEPTIONS, "#");

    consumerLifecycle = new QueueingConsumer(channel);

    channel.basicConsume(queueNameLifecycle(), true, consumerLifecycle);

}

From source file:at.ac.tuwien.dsg.comot.m.core.test.utils.TestAgentAdapter.java

License:Apache License

public TestAgentAdapter(String adapterId, String host) throws IOException {

    this.adapterId = adapterId;

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);// w  ww .  j av a  2s .c  om
    connection = factory.newConnection();
    channel = connection.createChannel();

    channel.queueDeclare(queueNameLifecycle(), false, false, true, null);
    // channel.queueDeclare(queueNameCustom(), false, false, true, null);

    channel.queueBind(queueNameLifecycle(), Constants.EXCHANGE_LIFE_CYCLE, "#");
    channel.queueBind(queueNameLifecycle(), Constants.EXCHANGE_CUSTOM_EVENT, "#");
    channel.queueBind(queueNameLifecycle(), Constants.EXCHANGE_EXCEPTIONS, "#");

    consumerLifecycle = new QueueingConsumer(channel);
    // consumerCustom = new QueueingConsumer(channel);

    channel.basicConsume(queueNameLifecycle(), true, consumerLifecycle);
    // channel.basicConsume(queueNameCustom(), true, consumerCustom);

}

From source file:bank.OurRabbitBank.java

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.OUR_JSON_BANK);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, true, consumer);
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        AMQP.BasicProperties properties = delivery.getProperties();
        String message = new String(delivery.getBody());

        Gson g = new Gson();

        Message msg = g.fromJson(message, Message.class);

        System.out.println(" [x] Received '" + message + "'");

        sendToNormalizer(msg, properties);
    }//from  w w  w .j  av  a2 s .  com
}

From source file:bank.OurRabbitBank.java

private static void sendToNormalizer(Message msg, AMQP.BasicProperties props) {
    try {//from   w  ww .j a  v a  2 s .  c  o m
        Gson g = new Gson();
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //channel.exchangeDeclare(ExchangeName.OUR_JSON_BANK_RESPONSE, "direct");

        int ssn = Integer.valueOf(msg.getSsn());
        double interestRate = calcRate();
        String bank = "OurRabbitBank";
        String correlationId = props.getCorrelationId();

        LoanResponse response = new LoanResponse(ssn, interestRate, bank, correlationId);

        String res = g.toJson(response);

        channel.basicPublish(ExchangeName.OUR_JSON_BANK_RESPONSE, "", props, res.getBytes());

        System.out.println(" [x] Sent '" + res + "'");

        channel.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Error in OutRabbitBank: " + e.getMessage());
    }
}

From source file:benchmarkio.consumer.rabbitmq.RabbitMQMessageConsumer.java

License:Apache License

public RabbitMQMessageConsumer(final String host, final int port, final String topic, final boolean durable) {
    Preconditions.checkNotNull(host);/*from ww  w.j av  a 2  s . com*/
    Preconditions.checkNotNull(topic, "topic cannot be null");

    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    factory.setPort(port);

    try {
        final Connection connection = factory.newConnection();

        this.channel = connection.createChannel();
        this.channel.exchangeDeclare(topic, "topic", durable);
    } catch (final IOException e) {
        throw Throwables.propagate(e);
    }

    this.topic = topic;
    this.histogram = Histograms.create();
}