Example usage for com.rabbitmq.client Channel queueDeclare

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

Introduction

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

Prototype

Queue.DeclareOk queueDeclare() throws IOException;

Source Link

Document

Actively declare a server-named exclusive, autodelete, non-durable queue.

Usage

From source file:loanbroker.Aggregator.java

public void reciveFromNormalizer(Hashtable<String, Message> messageFroumBankList,
        Hashtable<String, Message> messagesFromNormalizer, ArrayList<Message> foundMessages)
        throws IOException, TimeoutException, Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);//from w  ww . j  av a 2  s  .  com
    factory.setPort(5672);
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    // channel.exchangeDeclare(inputEXCHANGE_NAME, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator);
    System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL
            + RoutingKeys.NormalizerToAggregator + ". 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 m = new String(body, "UTF-8");

            Gson gson = new GsonBuilder().create();
            LoanResponse lp = gson.fromJson(m, LoanResponse.class);
            Message fm = new Message("" + lp.getSsn(), (int) lp.getInterestRate(), 0, lp.getBank());
            messagesFromNormalizer.put(lp.getCorrelationId(), fm);

            System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'");
            try {
                checkLoanMessages(messageFroumBankList, messagesFromNormalizer, foundMessages);
            } catch (InterruptedException ex) {
                Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TimeoutException ex) {
                Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex);
            }

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

From source file:loanbroker.Aggregator.java

public void reciveFromRecieptList(Hashtable<String, Message> messagesFromBankList)
        throws IOException, TimeoutException, Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);//from  w ww.java2 s . c o  m
    factory.setPort(5672);
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ExchangeName.GLOBAL, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.RecipientListToAggregator);
    System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL
            + RoutingKeys.RecipientListToAggregator + ".. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String m = new String(body, "UTF-8");
            // System.out.println("reciveFromRecieptList" + m);
            String p = properties.getCorrelationId();
            if (p != null) {
                //send to translator
                Gson g = new Gson();
                Message fm = g.fromJson(m, Message.class);
                if (fm.getBanks() != null) {
                    Message k = new Message(fm.getSsn(), fm.getCreditScore(), fm.getLoanAmount(),
                            fm.getLoanDuration());

                    k.setBanks(fm.getBanks());

                    messagesFromBankList.put(p, k);
                }

                System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'");
                //   System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'");
            } else {
                System.out.println("No correlationId");
            }

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

From source file:loanbroker.GetCreditScore.java

void recive() throws IOException, TimeoutException, InterruptedException, Exception {
    //setting the connection to the RabbitMQ server
    ConnectionFactory connfac = new ConnectionFactory();
    connfac.setHost(hostName);//from  w  ww .ja v  a2  s.c  om
    connfac.setUsername("student");
    connfac.setPassword("cph");

    //make the connection
    Connection conn = connfac.newConnection();
    //make the channel for messaging
    Channel chan = conn.createChannel();

    //Declare a queue
    chan.exchangeDeclare(ExchangeName.OUTPUT_LOAN_REQUEST, "fanout");
    String queueName = chan.queueDeclare().getQueue();
    chan.queueBind(queueName, ExchangeName.OUTPUT_LOAN_REQUEST, "");

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

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

    //start polling messages
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String m = new String(delivery.getBody());
        System.out.println(" [x] Received '" + m + "'");
        Gson gson = new GsonBuilder().create();
        Message fm = gson.fromJson(m, Message.class);
        int creditScore = creditScore(fm.getSsn());
        fm.setCreditScore(creditScore);
        fm.setSsn(fm.getSsn().replace("-", ""));
        send(fm);

    }

}

From source file:loanbroker.normalizer.NormalizerOurJsonBank.java

public static void main(String[] args) {
    try {/*from w w  w  . j a va 2s. c om*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");
        factory.setPort(5672);
        factory.setUsername("student");
        factory.setPassword("cph");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        //channel.queueDeclare(RPC_QUEUE_NAME,false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

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

        //producer 
        Channel channelOutput = connection.createChannel();
        channelOutput.exchangeDeclare(ExchangeName.GLOBAL, "direct");
        String queueNameProducer = channelOutput.queueDeclare().getQueue();
        channelOutput.queueBind(queueNameProducer, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator);

        LoanResponse loanResponse;
        while (true) {
            System.out.println("Reading");
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

            String message = new String(delivery.getBody());

            JSONObject jsonObj = new JSONObject(message);

            loanResponse = new LoanResponse(jsonObj.getInt("ssn"), Math.ceil(jsonObj.getDouble("interestRate")),
                    "Our Json Bank", delivery.getProperties().getCorrelationId());
            System.out.println("renter: " + loanResponse.getInterestRate());
            System.out.println("ssn: " + loanResponse.getSsn());
            System.out.println("bank : " + loanResponse.getBank());
            System.out.println("JSON:" + loanResponse);
            System.out.println("TOstring:" + jsonObj.toString());
            Gson g = new Gson();
            String fm = g.toJson(loanResponse);
            channelOutput.basicPublish(ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator, null,
                    fm.getBytes());

        }

    } catch (IOException | TimeoutException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:loanbroker.normalizer.NormalizerOurSoapXmlBank.java

public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException {
    //Connection/*from  ww  w .  j a v a2  s. c  om*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    //Consumer
    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    //s channel.queueBind(queueName, EXCHANGE_NAME, "OurSoapXmlBank");
    channel.queueBind(queueName, EXCHANGE_NAME, "");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, true, consumer);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    //Producer
    Channel channelOutput = connection.createChannel();
    channelOutput.exchangeDeclare("TeamFirebug", "direct");

    LoanResponse loanResponse;
    DtoOurSoapXmlBank dtoOurSoapXmlBank;
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();

        dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message), DtoOurSoapXmlBank.class);
        loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(), dtoOurSoapXmlBank.getInterestRate(),
                "Our Soap Xml bank", delivery.getProperties().getCorrelationId());
        // loanResponse.setBank(routingKey);

        System.out.println("renter: " + loanResponse.getInterestRate());
        System.out.println("ssn: " + loanResponse.getSsn());
        System.out.println("bank : " + loanResponse.getBank());
        JSONObject jsonObj = new JSONObject(loanResponse);
        // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes());
        channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null,
                jsonObj.toString().getBytes());
    }
}

From source file:loanbroker.RecipientList.java

public static void main(String[] args) throws Exception {
    //make correlationId for Aggregator

    //rabbit connect
    final String exchangeName = ExchangeName.OUTPUT_GET_BANKS;

    RabbitConnection rabbitConnection = new RabbitConnection();

    Channel channel = rabbitConnection.makeConnection();
    channel.exchangeDeclare(exchangeName, "fanout");

    String queueName = channel.queueDeclare().getQueue();

    //channel.queueBind(queueName, exchangeName, RoutingKeys.RecipientListInput);
    channel.queueBind(queueName, exchangeName, "");

    //get banks from queue. "Get banks" component
    QueueingConsumer consumer = new QueueingConsumer(channel) {
        String corrId = java.util.UUID.randomUUID().toString();

        @Override//www .  ja v  a  2  s .  com
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            //send to Aggregator first
            sendMessage(ExchangeName.GLOBAL, RoutingKeys.RecipientListToAggregator, message, corrId);

            //send to translator
            Message request = getFromJson(message);
            if (request.getBanks() != null) {
                for (Bank bank : request.getBanks()) {
                    sendMessage(ExchangeName.GLOBAL, bank.getRoutingKey(), message, corrId);
                    //remember that the component "Get banks" must choose which banks we need to send to(according to credit score)
                }
            }
        }
    };
    channel.basicConsume(queueName, true, consumer);

}

From source file:loanbroker.Result.java

public void reciveResultFromAggregator() throws IOException, TimeoutException, Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);/*w  w  w .  ja  va2 s  .  co m*/
    factory.setPort(5672);
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.Result);
    System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.Result
            + ".. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String m = new String(body, "UTF-8");
            //  System.out.println("reciveFromRecieptList"+ m);
            String p = properties.getCorrelationId();

            Gson gson = new GsonBuilder().create();
            LoanResponse fm = gson.fromJson(m, LoanResponse.class);

            // System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'");
            //   System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'");
            System.out.println("**** [info]**** You're best interestrate is '" + fm.getInterestRate()
                    + "' on the CPR number. '" + fm.getSsn() + "' at '" + fm.getBank() + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java

License:Apache License

/**
 * MQ ??  ./*from   w w w  .j  av  a 2  s .  com*/
 * <p>
 * exchange = fanout
 * ??
 *
 * @param topic    
 * @param consumer ?
 */
@Override
protected void doSubscribe(String topic, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {
        channel.exchangeDeclare(topic, BuiltinExchangeType.FANOUT, true);
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, topic, "");
        channel.basicQos(1);
        channel.basicConsume(queueName, false,
                getDefaultConsumer(channel, topic, topic, "", queueName, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit response error.", e);
    }
}

From source file:mx.bigdata.utils.amqp.AMQPClientHelperImpl.java

License:Apache License

public String createQueue(Channel channel, String key) throws Exception {
    AMQP.Queue.DeclareOk result = channel.queueDeclare();
    String queue = result.getQueue();
    channel.queueBind(queue, getExchangeName(key), getRoutingKey(key));
    return queue;
}

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());
    }// www.j  a  v  a  2 s.co m
}