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:com.mycompany.javateste.queues.Send.java

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("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

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

From source file:com.mycompany.javateste.queues.topic.EmitLogTopic.java

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;/* ww  w. j  av a  2 s.com*/
    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("UTF-8"));
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");

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

From source file:com.mycompany.javateste.queues.topic.ReceiveLogsTopic.java

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, "topic");
    String queueName = channel.queueDeclare().getQueue();

    if (argv.length < 1) {
        System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
        System.exit(1);/*  w  w w. jav  a 2s . c  om*/
    }

    for (String bindingKey : argv) {
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
    }

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

    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 '" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:com.mycompany.javateste.queues.worktasks.NewTask.java

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(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();//from   ww  w .  j  a  va  2s .co  m
    connection.close();
}

From source file:com.mycompany.javateste.queues.worktasks.Worker.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    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 www  . j a v  a2 s .  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:com.mycompany.loanbroker.requestLoan.java

/**
 * Web service operation//w w w  . java  2  s. com
 */
@WebMethod(operationName = "request")
public String request(@WebParam(name = "ssn") String ssn, @WebParam(name = "loanAmount") double loanAmount,
        @WebParam(name = "loanDuration") int loanDuration) throws IOException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("Dreamteam");
    factory.setPassword("bastian");
    Connection connection = factory.newConnection();
    Channel sendingchannel = connection.createChannel();
    Channel listeningChannel = connection.createChannel();

    listeningChannel.exchangeDeclare(EXCHANGE, "direct");
    listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null);
    listeningChannel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE, ssn.replace("-", ""));

    sendingchannel.queueDeclare(SENDING_QUEUE_NAME, false, false, false, null);

    message = ssn + "," + loanAmount + "," + loanDuration;

    sendingchannel.basicPublish("", SENDING_QUEUE_NAME, null, message.getBytes());

    sendingchannel.close();

    Consumer consumer = new DefaultConsumer(listeningChannel) {
        @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] The LoanBroker Has Received '" + message + "'");
            result = message;
        }
    };

    listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer);
    //connection.close();

    return result;
}

From source file:com.mycompany.mavenproject1.RMQConsumer.java

License:Open Source License

private void createChannel(String queue) {
    try {// w  w w .  j  av  a  2s .c  om
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(queue, false, false, false, null);
    } catch (IOException ex) {
        Logger.getLogger(RMQConsumer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TimeoutException ex) {
        Logger.getLogger(RMQConsumer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.mycompany.mavenproject1.RMQProducer.java

License:Open Source License

@SuppressWarnings("override")
public void sendMessage(String message, String queue) {
    try {//from   ww  w .  ja v a 2s.  co  m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel c = connection.createChannel();
        c.queueDeclare(queue, false, false, false, null);
        c.basicPublish("", queue, null, message.getBytes());
    } catch (java.io.IOException ex) {
        Logger.getLogger(SandboxUserInterface.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TimeoutException ex) {
        Logger.getLogger(RMQProducer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.mycompany.net.Run.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*from  www. ja  v a  2 s. c  o  m*/
 * @throws java.util.concurrent.TimeoutException
 */
public static void main(String[] args) throws IOException, TimeoutException {
    // TODO code application logic here

    JPA jpa = new JPA();
    jpa.initDB();

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("systembus"); // RabbitMQ IP
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange
    AMQP.Queue.DeclareOk result = channel.queueDeclare("", false, true, false, null);
    String queuename = result.getQueue();
    //bind to sensor info
    channel.queueBind(queuename, "sensors", "gateway.data"); // Binding key is #, this will consume all messages

    //bind to the dashboard
    channel.queueBind(queuename, "sensors", "dashboard.request");

    //bind to Processing units output
    channel.queueBind(queuename, "sensors", "database.put");

    logger.info(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {

            try {
                String message = new String(body, "UTF-8");
                Gson gson = new Gson();
                Type type = new TypeToken<Map<String, String>>() {
                }.getType();
                Map<String, String> myMap = gson.fromJson(message, type);
                logger.info(" [x] " + envelope.getRoutingKey() + " - Received '" + message + "'");

                String routing_key = envelope.getRoutingKey();
                if ("gateway.data".equals(routing_key)) {
                    jpa.saveRawToDB(myMap);
                } else if ("database.put".equals(routing_key)) {
                    jpa.saveAlertToDB(myMap);
                } else if ("dashboard.request".equals(routing_key)) {
                    Thread.sleep(1000);
                    jpa.processRequest(myMap, channel);
                } else
                    logger.error("NOT A VALID MESSAGE!");

            } catch (Exception e) {
                logger.error(e.toString());
            }

        }
    };

    channel.basicConsume(queuename, true, consumer);

    //channel.close();
    //connection.close();

    //entityManager.close();
    //entityManagerFactory.close();

}

From source file:com.mycompany.normalizer.Normalizer.java

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

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

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

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//from w  w w .java  2  s . c  o  m
        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 + "'");

            //HANDLE HERE
            if (message.startsWith("{")) {
                // Transform with GSON

                schoolBank = message.contains("-");

                if (!schoolBank) {
                    message = message.replace("-", "");
                    JResponse jresponse = gson.fromJson(message, JResponse.class);
                    jresponse.setBank("cphbusiness.bankJSON");
                    message = gson.toJson(jresponse);

                } else {
                    message = message.replace("-", "");
                    JResponse jresponse = gson.fromJson(message, JResponse.class);
                    jresponse.setBank("DreamTeamBankJSON");
                    message = gson.toJson(jresponse);
                }

                sendingChannel.basicPublish(SENDING_QUEUE_NAME, "", null, message.getBytes());

            } else {

                schoolBank = message.contains("-");
                String result = "";

                if (!schoolBank) {
                    message = message.replace("-", "");
                    JSONObject soapDatainJsonObject = XML.toJSONObject(message);

                    result = gson.toJson(soapDatainJsonObject);
                    result = result.replace("{\"map\":{\"LoanResponse\":{\"map\":", "");
                    result = result.replace("}}}", "");

                    JResponse jresponse = gson.fromJson(result, JResponse.class);
                    jresponse.setBank("cphbusiness.bankXML");
                    result = gson.toJson(jresponse);

                } else {
                    message = message.replace("-", "");
                    JSONObject soapDatainJsonObject = XML.toJSONObject(message);

                    result = gson.toJson(soapDatainJsonObject);
                    result = result.replace("{\"map\":{\"LoanResponse\":{\"map\":", "");
                    result = result.replace("}}}", "");

                    JResponse jresponse = gson.fromJson(result, JResponse.class);
                    jresponse.setBank("DreamTeamBankXML");

                    result = gson.toJson(jresponse);
                }

                //  XResponse response = gson.fromJson(soapDatainJsonObject, XResponse.class);
                sendingChannel.basicPublish(SENDING_QUEUE_NAME, "", null, result.getBytes());

            }

        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}