Example usage for com.rabbitmq.client Channel exchangeDeclare

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

Introduction

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

Prototype

Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type) throws IOException;

Source Link

Document

Actively declare a non-autodelete, non-durable exchange with no extra arguments

Usage

From source file:OnlyPackege.TheMightyBank.java

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

    JSONParser jsonParster = new JSONParser();

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

    recvChannel.exchangeDeclare(EXCHANGE_NAME, "fanout");

    String queueName = recvChannel.queueDeclare().getQueue();
    recvChannel.queueBind(queueName, EXCHANGE_NAME, "");

    QueueingConsumer consumer = new QueueingConsumer(recvChannel);
    recvChannel.basicConsume(queueName, true, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String responseQueue = delivery.getProperties().getReplyTo();

        Channel sendChannel = connection.createChannel();
        sendChannel.queueDeclare(responseQueue, false, false, false, null);

        String message = new String(delivery.getBody());
        JSONObject recvObj = (JSONObject) jsonParster.parse(message);

        //to be deleted after testing
        System.out.println(" [x] Received '" + message + "'");

        JSONObject obj = new JSONObject();
        obj.put("ssn", recvObj.get("ssn"));
        obj.put("interestRate", Math.random() * 10);

        sendChannel.basicPublish("", responseQueue, null, obj.toJSONString().getBytes());
    }//w w  w . j a  va 2s  . c o m
}

From source file:org.apache.airavata.datacat.agent.messageBroker.AiravataUpdateListener.java

License:Apache License

public void startBroker() {
    (new Thread(new Runnable() {
        @Override//w  w w.  j a  v a  2 s . co m
        public void run() {
            try {
                ConnectionFactory factory = new ConnectionFactory();
                factory.setHost(RABBITMQ_HOST);

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

                channel.exchangeDeclare(EXCHANGE_NAME, "topic");
                String queueName = channel.queueDeclare().getQueue();

                channel.basicQos(1);
                channel.queueBind(queueName, EXCHANGE_NAME, BINDING_KEY);

                logger.debug("Waiting for messages. To exit press CTRL+C");

                QueueingConsumer consumer = new QueueingConsumer(channel);
                channel.basicConsume(queueName, true, consumer);

                while (runFileUpdateListener) {
                    QueueingConsumer.Delivery delivery = consumer.nextDelivery();

                    Message message = new Message();
                    ThriftUtils.createThriftFromBytes(delivery.getBody(), message);
                    TBase event = null;

                    if (message.getMessageType().equals(MessageType.EXPERIMENT_OUTPUT)) {

                        ExperimentOutputCreatedEvent experimentOutputCreatedEvent = new ExperimentOutputCreatedEvent();
                        ThriftUtils.createThriftFromBytes(message.getEvent(), experimentOutputCreatedEvent);

                        logger.debug(" Message Received with message id '" + message.getMessageId()
                                + "' and with message type '" + message.getMessageType()
                                + "'  with experiment name "
                                + experimentOutputCreatedEvent.getExperimentName());

                        event = experimentOutputCreatedEvent;

                        logger.debug(" [x] Received FileInfo Message'");
                        process(experimentOutputCreatedEvent, message.getUpdatedTime());
                        logger.debug(" [x] Done Processing FileInfo Message");
                    } else {
                        logger.debug("Recieved message of type ..." + message.getMessageType());
                    }
                }
            } catch (Exception e) {
                logger.error(e);
            }
        }

    })).start();

}

From source file:org.apache.airavata.datacat.agent.org.apache.airavata.datacat.agent.messageBroker.RabbitMQConsumerTest.java

License:Apache License

/**
 * Test method to publish dummy data to RabbitMQ
 * @param messageType//from  www.j  a va 2  s  .c  o m
 * @throws java.io.IOException
 * @throws TException
 */
public void publish(MessageType messageType) throws java.io.IOException, TException {

    //establishing the connection
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(RABBITMQ_HOST);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic");

    String DATA_ROOT = AgentProperties.getInstance().getProperty(Constants.DATA_ROOT, "");

    //creating the output created Event
    ExperimentOutputCreatedEvent event = new ExperimentOutputCreatedEvent();
    event.setExperimentId("test");
    event.setOutputPath(DATA_ROOT + "/2H2OOHNCmin.com.out");

    //serializing the event
    byte[] body = ThriftUtils.serializeThriftObject(event);
    Message message = new Message();
    message.setEvent(body);
    message.setMessageId("sad");
    message.setMessageType(messageType);
    message.setUpdatedTime(993344232);
    String routingKey = "*";

    //serializing the message object
    byte[] messageArray = ThriftUtils.serializeThriftObject(message);
    channel.basicPublish(EXCHANGE_NAME, BINDING_KEY, null, messageArray);

    logger.debug(" [x] Sent '" + message + "'");

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

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPBroadcastReceiverImpl.java

License:Apache License

public void Subscribe() throws AMQPException {
    if (callback != null) {
        try {/*from  w w w.j  a v  a  2 s.c  om*/
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT);

            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_FANOUT, "");

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());

                callback.onMessage(message);
            }
        } catch (Exception e) {
            throw new AMQPException(e);
        }
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPBroadcastSenderImpl.java

License:Apache License

public void Send(OMElement message) throws AMQPException {
    try {//from  w w  w  .j  a  v a2  s.  c  o m
        if (isRoutable(message)) {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT);

            channel.basicPublish(AMQPUtil.EXCHANGE_NAME_FANOUT, "", null, message.toString().getBytes());

            channel.close();
            connection.close();
        }
    } catch (IOException e) {
        throw new AMQPException(e);
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPReceiverImpl.java

License:Apache License

public void Subscribe(AMQPRoutingKey key) throws AMQPException {
    if (callback != null) {
        try {//from w  w  w  .j a  va 2  s  .  co m
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_DIRECT, AMQPUtil.EXCHANGE_TYPE_DIRECT);

            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_DIRECT, key.getNativeKey());

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());

                callback.onMessage(message);
            }
        } catch (Exception e) {
            throw new AMQPException(e);
        }
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPSenderImpl.java

License:Apache License

public void Send(OMElement message) throws AMQPException {
    try {/*from  ww  w  .j  av  a 2s . c  om*/
        if (isRoutable(message)) {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_DIRECT, AMQPUtil.EXCHANGE_TYPE_DIRECT);

            List<String> routingKeys = new ArrayList<String>();
            getRoutingKeys(message, routingKeys);

            for (String routingKey : routingKeys) {
                channel.basicPublish(AMQPUtil.EXCHANGE_NAME_DIRECT, routingKey, null,
                        message.toString().getBytes());
            }

            channel.close();
            connection.close();
        }
    } catch (IOException e) {
        throw new AMQPException(e);
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPTopicReceiverImpl.java

License:Apache License

public void Subscribe(AMQPRoutingKey topic) throws AMQPException {
    if (callback != null) {
        try {//from w w  w.  ja  va 2  s .com
            Connection connection = connectionFactory.newConnection();

            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_TOPIC, AMQPUtil.EXCHANGE_TYPE_TOPIC);

            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_TOPIC, topic.getNativeKey());

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);

            while (true) {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                String message = new String(delivery.getBody());

                callback.onMessage(message);
            }
        } catch (Exception e) {
            throw new AMQPException(e);
        }
    }
}

From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPTopicSenderImpl.java

License:Apache License

public void Send(OMElement message) throws AMQPException {
    try {/* w ww . ja v  a 2s .  c  o  m*/
        if (isRoutable(message)) {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_TOPIC, AMQPUtil.EXCHANGE_TYPE_TOPIC);

            List<String> routingKeys = new ArrayList<String>();
            getRoutingKeys(message, routingKeys);

            for (String routingKey : routingKeys) {
                channel.basicPublish(AMQPUtil.EXCHANGE_NAME_TOPIC, routingKey, null,
                        message.toString().getBytes());
            }

            channel.close();
            connection.close();
        }
    } catch (IOException e) {
        throw new AMQPException(e);
    }
}

From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java

License:Apache License

@Test(timeout = 60 * 1000)
public void testReadExchange() throws Exception {
    final int maxNumRecords = 10;
    PCollection<RabbitMqMessage> raw = p.apply(RabbitMqIO.read().withUri("amqp://guest:guest@localhost:" + port)
            .withExchange("READEXCHANGE", "fanout", "test").withMaxNumRecords(maxNumRecords));
    PCollection<String> output = raw.apply(MapElements.into(TypeDescriptors.strings())
            .via((RabbitMqMessage message) -> new String(message.getBody(), StandardCharsets.UTF_8)));

    List<String> records = generateRecords(maxNumRecords).stream()
            .map(record -> new String(record, StandardCharsets.UTF_8)).collect(Collectors.toList());
    PAssert.that(output).containsInAnyOrder(records);

    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri("amqp://guest:guest@localhost:" + port);
    Connection connection = null;
    Channel channel = null;
    try {/* w  ww.  j a  va2s  . c  o  m*/
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare("READEXCHANGE", "fanout");
        Channel finalChannel = channel;
        Thread publisher = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
            for (int i = 0; i < maxNumRecords; i++) {
                try {
                    finalChannel.basicPublish("READEXCHANGE", "test", null,
                            ("Test " + i).getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        });
        publisher.start();
        p.run();
        publisher.join();
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}