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.service.OperationFacadeREST.java

@POST
@Override//from  w w w .java 2s  .co m
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void create(Operation entity) {
    if (!entity.getState().equals("waiting")) {
        super.create(entity);
        return;
    }
    if (entity.getOperationType())//venda
    {
        ArrayList<ClientStock> l = new ArrayList<>(getEntityManager()
                .find(Client.class, entity.getFkOwnerId().getClientId()).getClientStockCollection());
        Boolean fail = false;
        for (int i = 0; i < l.size(); i++) {
            if (l.get(i).getStock().equals(entity.getFkStockId())) {
                if (l.get(i).getQuantity() < entity.getQuantity())
                    return;
                l.get(i).setQuantity(l.get(i).getQuantity() - entity.getQuantity());
                getEntityManager().persist(l.get(i));
                entity.getFkOwnerId().setClientStockCollection(l);

                entity.setCreationDate(Date.from(Instant.now()));
                super.create(entity);
            }
        }
        if (fail)
            return;
    } else {
        entity.setCreationDate(Date.from(Instant.now()));
        if (entity.getFkStockId().getQuantity() > entity.getQuantity()) {

            System.out.println("yes");
            super.create(entity);
        } else {
            return;
        }
    }

    try {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("hello", false, false, false, null);
        System.out.println(super.findAll().get(super.findAll().size() - 1).getOperationId() + ","
                + entity.getFkOwnerId().getName() + "," + entity.getFkStockId().getName() + ","
                + entity.getQuantity());

        String message = super.findAll().get(super.findAll().size() - 1).getOperationId() + ","
                + entity.getFkOwnerId().getName() + "," + entity.getFkStockId().getName() + ","
                + entity.getQuantity();
        channel.basicPublish("", "hello", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    } catch (IOException ex) {
        Logger.getLogger(ClientFacadeREST.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TimeoutException ex) {
        Logger.getLogger(ClientFacadeREST.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.shopwiki.roger.MessagingUtil.java

License:Apache License

private static void sendMessage(Channel channel, String exchange, String routingKey, Object message,
        BasicProperties.Builder props) throws IOException {
    byte[] bytes = objectMapper.writeValueAsBytes(message);
    props = props.contentEncoding(UTF_8.name());
    props = props.contentType("application/json");
    props = props.timestamp(new Date());
    if (DEBUG) {// w  ww. j a  va  2s .  c  o m
        System.out.println("*** MessagingUtil SENDING MESSAGE ***");
        System.out.println("*** routingKey: " + routingKey);
        System.out.println("*** props:\n" + prettyPrint(props.build()));
        System.out.println("*** message: " + prettyPrintMessage(message));
        System.out.println();
    }
    channel.basicPublish(exchange, routingKey, props.build(), bytes);
}

From source file:com.sitewhere.protobuf.test.ActiveMQTests.java

License:Open Source License

@Test
public void doRabbitMQTest() throws Exception {
    String exchangeName = "sitewhere";
    String queueName = "SITEWHERE.IN";
    String routingKey = "sitewhere";

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri("amqp://localhost:5672/SITEWHERE.IN");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchangeName, "direct", true);
    channel.queueDeclare(queueName, true, false, false, null);
    channel.queueBind(queueName, exchangeName, routingKey);

    byte[] messageBodyBytes = generateEncodedMeasurementsMessage();
    channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes);

    channel.close();/* w  ww  . j a  v a 2s  . co  m*/
    connection.close();
}

From source file:com.sitewhere.sources.ActiveMQTests.java

License:Open Source License

@Test
public void doRabbitMQTest() throws Exception {
    String exchangeName = "sitewhere";
    String queueName = "sitewhere.input";
    String routingKey = "sitewhere";

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri("amqp://localhost:5672");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchangeName, "direct", true);
    channel.queueDeclare(queueName, true, false, false, null);
    channel.queueBind(queueName, exchangeName, routingKey);

    byte[] messageBodyBytes = EventsHelper.generateEncodedMeasurementsMessage(HARDWARE_ID);
    channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes);

    channel.close();//ww  w.  j  a  v  a 2 s .co  m
    connection.close();
}

From source file:com.siva.rabbitmq.AmqpMsgPublisher.java

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

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setUsername("admin");
    factory.setPassword("welcome01");
    factory.setPort(5672);//  w  ww.  ja v a 2  s  .  c o m
    factory.setVirtualHost("/admin_vhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic", true);

    String routingKey = "mqtt_topic.iot.admin_vhost";
    String message = "Test Message from IoT";

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

    connection.close();
}

From source file:com.trivago.mail.pigeon.web.data.process.QueueNewsletter.java

License:Apache License

private void queueNewsletter(Mail mail, Sender sender, Recipient recipient, Campaign campaign) {
    Connection conn = ConnectionPool.getConnection();
    Channel channel = null;
    MailTransport transport = templateProcessor.processMail(mail, recipient, sender, campaign);

    if (transport == null) {
        log.warn(//  w  w w .  j av  a 2  s .c  om
                "Template processor returned null instead of a mail transport object. This is probably a bug!");
        return;
    }

    if (transport.shouldAbortSending() && !transport.shouldEnforceSending()) {
        log.info("Skipped mail to " + transport.getTo() + " because transport aborted sending.");
        return;
    }

    String json = JSON.defaultJSON().forValue(transport);

    try {
        channel = conn.createChannel();
        channel.exchangeDeclare("mailpidgeon", "direct", true);
        channel.queueDeclare(channelName, true, false, false, null);
        channel.queueBind(channelName, "mailpidgeon", "mailpidgeon");

        byte[] messageBodyBytes = json.getBytes();
        channel.basicPublish("mailpidgeon", "mailpidgeon", null, messageBodyBytes);

    } catch (IOException e) {
        log.error(e);
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                log.error("Could not close channel", e);
            }
        }
    }
}

From source file:com.UseCaseSimpleConsumer.java

License:Open Source License

public static void main(String[] argv) throws java.io.IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    try {/*from w  w  w .j a  va2 s  .co m*/
        channel.exchangeDeclarePassive(EXCHANGE_NAME);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
    }
    try {
        channel.queueDeclarePassive(ROUTE_KEY);
    } catch (java.io.IOException e) {
        if (!channel.isOpen())
            channel = connection.createChannel();
        channel.queueDeclare(ROUTE_KEY, false, false, false, null);
    }

    channel.queueBind(ROUTE_KEY, EXCHANGE_NAME, ROUTE_KEY);

    String param = "IBM";
    String msg = "<m:placeOrder xmlns:m=\"http://services.samples\">\n" + "    <m:order>\n"
            + "        <m:price>" + getRandom(100, 0.9, true) + "</m:price>\n" + "        <m:quantity>"
            + (int) getRandom(10000, 1.0, true) + "</m:quantity>\n" + "        <m:symbol>" + param
            + "</m:symbol>\n" + "    </m:order>\n" + "</m:placeOrder>";

    channel.basicPublish(EXCHANGE_NAME, ROUTE_KEY,
            new AMQP.BasicProperties.Builder().contentType("text/plain").build(), msg.getBytes());
    System.out.println(" [x] Sent '" + msg + "'");
    channel.close();
    connection.close();
}

From source file:com.vmware.vhadoop.vhm.rabbit.RabbitConnection.java

License:Open Source License

protected void sendMessage(String routeKey, byte[] data) throws CannotConnectException {
    boolean retry = false;
    do {/*from  w  w  w  .  j a va2 s .  c o  m*/
        try {
            Channel channel = getChannel();
            if (channel != null) {
                channel.basicPublish(_credentials.getExchangeName(), routeKey, null, data);
            }
            retry = false;
        } catch (Exception e) {
            if (!retry) {
                connect();
                retry = true;
            } else {
                throw new CannotConnectException("Unable to send message", e);
            }
        }
    } while (retry);
}

From source file:com.wakkir.rabbitmq.basic.Sender.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 ww  .j  a  v a  2 s.com
    connection.close();
}

From source file:com.zigbee.function.util.MessageUtil.java

License:Open Source License

public static void sendMessage(Message message, String queueName) throws AppException {
    try {/*ww  w . j  ava  2  s.co m*/
        Channel channel = openChannel(null);
        channel.basicPublish("", queueName, null, JSON.toJSONString(message).getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }

}