Example usage for com.rabbitmq.client ConnectionFactory setHost

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

Introduction

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

Prototype

public void setHost(String host) 

Source Link

Usage

From source file:v2.Student.java

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;/*from  www  .ja v a2 s .  c o  m*/
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");

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

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String routingKey = "AP.DAT";//getRouting(argv);
        String message = "Plz enroll me, breh";//getMessage(argv);
        String corrId = java.util.UUID.randomUUID().toString();
        String callbackQueueName = channel.queueDeclare().getQueue();
        String response = null;
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(callbackQueueName, true, consumer);

        BasicProperties props = new BasicProperties.Builder().replyTo(callbackQueueName).build();

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

        while (true) {
            System.out.println("blocked?");
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println("unblocked");
            System.out.println(delivery);
            response = new String(delivery.getBody());
            System.out.println(response);
            //        if (delivery.getProperties().getCorrelationId().equals(corrId)) {
            //            response = new String(delivery.getBody());
            //            System.out.println(response);
            //            break;
            //        }
        }

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

From source file:wiki.messaging.NewTask.java

License:Open Source License

public static void main(String[] args) throws IOException {
    int n = 500;/*from  ww w  . j a  v  a2  s . c  o m*/
    String url = null;
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("n")) {
            n = Integer.valueOf(args[i + 1]);
        }
    }
    DbConnector ds = new DbConnector(url);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    RandomDocGetter rdg = new RandomDocGetter(ds);
    for (int i = 0; i < n; i++) {
        String message = getMessage(rdg);
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
    }

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

From source file:wiki.messaging.Receive.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String url = null;//  w  w w .  j  a v a  2 s  .c  o  m
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("mj")) {
            maxJobs = Integer.valueOf(args[i + 1]);
        }
    }
    if (rabbitMqUrl == null) {
        rabbitMqUrl = "192.168.1.108";
    }
    if (url == null) {
        url = "localhost";
    }
    //        if (resultUrl == null) {
    //            resultUrl = url;
    //        }
    threadPool = new ExecutorCompletionService<>(Executors.newFixedThreadPool(maxJobs));
    System.out.println("DataSource: " + url);
    //        System.out.println("ResultWrite: " + resultUrl);
    DbConnector ds = new DbConnector(url);
    //        DbConnector rs = new DbConnector(resultUrl);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    channel.basicQos(maxJobs);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message, ds);
        saveResult(channel);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:wiki.messaging.ResultConsumer.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String rabbitMqUrl = null;/*from  w ww  .java2 s . c  om*/
    String resultUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("rs")) {
            resultUrl = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        }
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(RESULT_QUEUE_NAME, autoAck, consumer);

    DbConnector dbc = new DbConnector(resultUrl);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);
        if (delivery == null)
            break;
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        saveResult(message, dbc);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
    channel.close();
    connection.close();
}

From source file:wiki.messaging.Treeceive.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String url = null;//from  w w w .  j  ava2s .co  m
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("mj")) {
            maxJobs = Integer.valueOf(args[i + 1]);
        }
    }
    if (rabbitMqUrl == null) {
        rabbitMqUrl = "192.168.1.108";
    }
    if (url == null) {
        url = "localhost";
    }
    //        if (resultUrl == null) {
    //            resultUrl = url;
    //        }
    threadPool = new ExecutorCompletionService<>(Executors.newFixedThreadPool(maxJobs));
    System.out.println("DataSource: " + url);
    //        System.out.println("ResultWrite: " + resultUrl);
    DbConnector ds = new DbConnector(url);
    //        DbConnector rs = new DbConnector(resultUrl);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    channel.basicQos(maxJobs);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message, ds);
        saveResults(channel);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:workqueue.NewTask.java

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

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

    String message = getMessage(argv);

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

    channel.close();//w  ww. jav a2  s.c om
    connection.close();
}

From source file:workqueue.Worker.java

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

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);//from  w  w w.j  a  v a 2s.co  m

    final 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");

            System.out.println(" [x] Received '" + message + "'");
            try {
                doWork(message);
            } finally {
                System.out.println(" [x] Done");
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        }
    };
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
}

From source file:ws.ament.hammock.rabbitmq.ConnectionFactoryProducer.java

License:Apache License

@Produces
@ApplicationScoped/*from  ww  w  .ja va 2s .co m*/
public ConnectionFactory createConnectionFactory(RabbitMQConfiguration rabbitMQConfiguration) {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setAutomaticRecoveryEnabled(rabbitMQConfiguration.isAutomaticRecovery());
    connectionFactory.setClientProperties(rabbitMQConfiguration.getClientProperties());
    connectionFactory.setConnectionTimeout(rabbitMQConfiguration.getConnectionTimeout());
    connectionFactory.setExceptionHandler(rabbitMQConfiguration.getExceptionHandler());
    connectionFactory.setHandshakeTimeout(rabbitMQConfiguration.getHandshakeTimeout());
    connectionFactory.setHeartbeatExecutor(rabbitMQConfiguration.getHeartbeatExecutor());
    connectionFactory.setMetricsCollector(rabbitMQConfiguration.getMetricsCollector());
    connectionFactory.setHost(rabbitMQConfiguration.getHost());
    connectionFactory.setNetworkRecoveryInterval(rabbitMQConfiguration.getNetworkRecoveryInterval());
    if (rabbitMQConfiguration.isNio()) {
        connectionFactory.useNio();
        connectionFactory.setNioParams(rabbitMQConfiguration.getNioParams());
    }
    connectionFactory.setPassword(rabbitMQConfiguration.getPassword());
    connectionFactory.setPort(rabbitMQConfiguration.getPort());
    connectionFactory.setRequestedChannelMax(rabbitMQConfiguration.getRequestedChannelMax());
    connectionFactory.setRequestedFrameMax(rabbitMQConfiguration.getRequestedFrameMax());
    connectionFactory.setRequestedHeartbeat(rabbitMQConfiguration.getRequestedHeartbeat());
    connectionFactory.setSaslConfig(rabbitMQConfiguration.getSaslConfig());
    connectionFactory.setSharedExecutor(rabbitMQConfiguration.getSharedExecutor());
    connectionFactory.setShutdownExecutor(rabbitMQConfiguration.getShutdownExecutor());
    connectionFactory.setShutdownTimeout(rabbitMQConfiguration.getShutdownTimeout());
    connectionFactory.setSocketConfigurator(rabbitMQConfiguration.getSocketConf());
    connectionFactory.setSocketFactory(rabbitMQConfiguration.getFactory());
    connectionFactory.setThreadFactory(rabbitMQConfiguration.getThreadFactory());
    connectionFactory.setTopologyRecoveryEnabled(rabbitMQConfiguration.isTopologyRecovery());
    try {
        connectionFactory.setUri(rabbitMQConfiguration.getUri());
    } catch (Exception e) {
        throw new RuntimeException("Unable to populate URI ", e);
    }
    connectionFactory.setUsername(rabbitMQConfiguration.getUsername());
    connectionFactory.setVirtualHost(rabbitMQConfiguration.getVirtualHost());
    if (rabbitMQConfiguration.getSslContext() != null) {
        connectionFactory.useSslProtocol(rabbitMQConfiguration.getSslContext());
    }
    return connectionFactory;
}

From source file:zebrogamq.gamelogic.ChannelsManager.java

License:Open Source License

private ConnectionFactory getConnectionFactory(final GameLogicState state) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(Util.getRabbitMQProperties().getProperty("gameLogicBrokerHost"));
    factory.setUsername(state.login);//from   w  w w. ja v  a 2s  .  c o  m
    factory.setPassword(state.password);
    factory.setVirtualHost(state.virtualHost);
    factory.setPort(Integer.valueOf(Util.getRabbitMQProperties().getProperty("gameLogicBrokerPort",
            String.valueOf(factory.getPort()))));
    factory.setConnectionTimeout(CONNECTION_ESTABLISHMENT_TIMEOUT);
    // set the heartbeat value for the amqp connections
    // to avoid the keeping of obsolete consumers
    factory.setRequestedHeartbeat(Integer.valueOf(Util.getRabbitMQProperties()
            .getProperty("amqpConnectionHeartbeat", String.valueOf(factory.getRequestedHeartbeat()))));
    return factory;
}