Example usage for com.rabbitmq.client Channel basicPublish

List of usage examples for com.rabbitmq.client Channel basicPublish

Introduction

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

Prototype

void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;

Source Link

Document

Publish a message.

Usage

From source file:com.mycompany.dreamteamjsontranslator.Translator.java

public static void main(String[] args) throws IOException {

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

    listeningChannel.queueDeclare(LISTENING_QUEUE_NAME, false, false, false, null);

    listeningChannel.queueBind(LISTENING_QUEUE_NAME, EXCHANGE_NAME, "DreamTeamBankJSON");

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

    Consumer consumer = new DefaultConsumer(listeningChannel) {
        @Override//from   w  w w  .  j  a va  2s .  c om
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            System.out.println("Hello");
            message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");

            String[] arr = message.split(",");

            Result res = new Result(arr[0], Integer.parseInt(arr[1]), Double.parseDouble(arr[2]),
                    Integer.parseInt(arr[3]));

            String result = gson.toJson(res);

            sendingChannel.queueDeclare(SENDING_QUEUE_NAME, false, false, false, null);
            sendingChannel.basicPublish("", SENDING_QUEUE_NAME, null, result.getBytes());

        }
    };
    listeningChannel.basicConsume(LISTENING_QUEUE_NAME, true, consumer);

}

From source file:com.mycompany.dreamteamxml.DreamTeamXML.java

public void sender(double interestRate, String ssn) throws IOException {

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

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

    String message = "<LoanResponse>" + "<interestRate>" + interestRate + "</interestRate>" + "<ssn>" + ssn
            + "</ssn>" + "</LoanResponse>";

    System.out.println("Message created as soap");

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

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

    channel.close();//  ww  w .  j  av  a2  s.  c o  m
    connection.close();

}

From source file:com.mycompany.javateste.queues.pubsub.EmitLog.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, "fanout");

    String message = getMessage(argv);

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

    channel.close();/*  w w w.j a  v a2 s  .  c  o  m*/
    connection.close();
}

From source file:com.mycompany.javateste.queues.routing.EmitLogDirect.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, "direct");

    String severity = getSeverity(argv);
    String message = getMessage(argv);

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

    channel.close();//w  w  w  .  j  ava 2 s  . com
    connection.close();
}

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  w w w  . j  ava  2  s  . c om
    connection.close();
}

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

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {//from  w w w.j a  v a2 s .c  om
        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.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();//  w w  w. j av  a2  s  .  c  o  m
    connection.close();
}

From source file:com.mycompany.loanbroker.requestLoan.java

/**
 * Web service operation/*  w w  w  .j  ava  2 s . c o  m*/
 */
@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.RMQProducer.java

License:Open Source License

@SuppressWarnings("override")
public void sendMessage(String message, String queue) {
    try {//from w w w .  jav 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.JPA.java

public void processRequest(Map<String, String> myMap, Channel channel) {
    System.out.println("before begin");
    System.out.println("before getTime");
    List<Data> tmp = getTimeIntervalBySensor(Long.parseLong(myMap.get("start")),
            Long.parseLong(myMap.get("end")), myMap.get("name"), myMap.get("room"));

    System.out.println("before commit");
    System.out.println("free");
    Data data;//  w w w  . ja  va  2s. co  m
    //PropertyConfigurator.configure(log4JPropertyFile);
    //send to queue
    for (Iterator<Data> iterator = tmp.iterator(); iterator.hasNext();) {
        data = (Data) iterator.next();
        String message = data.toString();
        try {
            channel.basicPublish("sensors", "dashboard.response", null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
        } catch (IOException ex) {
            // logger.error(ex);
        }

    }

}