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(String queue, boolean durable, boolean exclusive, boolean autoDelete,
        Map<String, Object> arguments) throws IOException;

Source Link

Document

Declare a queue

Usage

From source file:loanbroker.normalizer.NormalizerTeachersXmlBank.java

public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException {
    //Connection//from  www  .j  av  a  2 s  .c o  m
    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.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(RPC_QUEUE_NAME, true, consumer);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

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

    while (true) {
        System.out.println("Reading");
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();

        DtoTeachersXmlBank dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message),
                DtoTeachersXmlBank.class);
        LoanResponse loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(),
                dtoOurSoapXmlBank.getInterestRate(), "Teachers Xml Bank",
                delivery.getProperties().getCorrelationId());
        // loanResponse.setBank(routingKey);
        System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

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

From source file:localdomain.localhost.RabbitMQClient.java

License:Apache License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*  ww w  .j  a va  2  s  . c o m*/
        ConnectionFactory factory = new ConnectionFactory();
        String messages = new String();

        String uri = System.getProperty("CLOUDAMQP_URL");
        factory.setUri(uri);

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

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

        QueueingConsumer consumer = new QueueingConsumer(channel);

        boolean autoACK = false;
        channel.basicConsume(QUEUE_NAME, autoACK, consumer);

        System.out.println(" [*] Waiting 100ms for a message");
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(100);

        while (delivery != null) {
            String message = new String(delivery.getBody());

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

            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

            messages = message + " <br/> " + messages;
            delivery = consumer.nextDelivery(100);
        }
        request.setAttribute("messages", messages);
        request.getRequestDispatcher("/index.jsp").forward(request, response);

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

    } catch (Exception e) {
        request.setAttribute("throwable", e);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

}

From source file:localdomain.localhost.RabbitMQServer.java

License:Apache License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*from  w w  w  .  j  a  v a  2 s  . c om*/
        String message = request.getParameter("message");

        ConnectionFactory factory = new ConnectionFactory();
        String uri = System.getProperty("CLOUDAMQP_URL");
        factory.setUri(uri);

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

        boolean durable = true;
        channel.queueDeclare(QUEUE_NAME, durable, false, false, null);

        channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

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

        response.sendRedirect(request.getContextPath() + "/index.jsp");
    } catch (Exception e) {
        request.setAttribute("throwable", e);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

From source file:main.TestMain.java

public static void sendMessage(String message) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

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

    channel.close();//from ww  w . jav a2s .  c  o  m
    connection.close();
}

From source file:mapas.Mapas.java

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPassword("test");
    factory.setUsername("test");
    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);/*  w w  w . ja v  a 2s .  c  om*/

    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);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println(" [x] Done");
            channel.basicAck(envelope.getDeliveryTag(), false);

        }
    };
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
}

From source file:messaging.Worker.java

License:Apache License

public static void sendMessage(String message) throws java.io.IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename());
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

    channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();//ww  w  .  java2  s . c o  m
    connection.close();
}

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

License:Apache License

/**
 * MQ ??  ./*from  w  ww  .  j  a v  a  2 s  .  c  om*/
 * <p>
 * exchange = fanout
 *
 * @param address ?
 * @param message ?
 * @param confirm ??
 * @return ??rabbit confirm ?????
 */
public boolean doRequest(String address, String message, boolean confirm) {
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.queueDeclare(address, true, false, false, null);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(address), 2,
                0, null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke("", address, properties);
        channel.basicPublish("", address, properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit request error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit request error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit request error.", e);
        }
        connection.close();
    }
}

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

License:Apache License

/**
 * MQ ??  ?./* www.  jav  a2 s.c om*/
 * <p>
 * exchange = topic
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param message    ?
 * @param confirm    ??
 * @return ???rabbit confirm ?????
 */
public boolean publishWithTopic(String topic, String routingKey, String queueName, String message,
        boolean confirm) {
    logger.trace("[MQ] publishWithTopic {}:{}", topic, message);
    Connection connection = rabbitAdapter.getConnection();
    Channel channel = connection.createChannel(false);
    Object funResult = null;
    try {
        if (confirm) {
            channel.confirmSelect();
        }
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0,
                null, null, null, null, null, null, null, null, null);
        funResult = sendBeforeFun.invoke(topic, routingKey, properties);
        channel.basicPublish(topic, routingKey, properties, message.getBytes());
        if (confirm) {
            try {
                return channel.waitForConfirms();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                logger.error("[MQ] Rabbit publishWithTopic error.", e);
                sendErrorFun.invoke(e, funResult);
                return false;
            }
        } else {
            return true;
        }
    } catch (IOException e) {
        logger.error("[MQ] Rabbit publishWithTopic error.", e);
        sendErrorFun.invoke(e, funResult);
        return false;
    } finally {
        try {
            channel.close();
            sendFinishFun.invoke(funResult);
        } catch (IOException | TimeoutException e) {
            logger.error("[MQ] Rabbit publishWithTopic error.", e);
        }
        connection.close();
    }
}

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

License:Apache License

/**
 * MQ ??  .//from ww w .  ja  va  2 s . c o m
 * <p>
 * exchange = topic
 * ??
 *
 * @param topic      
 * @param routingKey Key
 * @param queueName  ??
 * @param consumer   ?
 */
public void subscribeWithTopic(String topic, String routingKey, String queueName, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {
        channel.queueDeclare(queueName, true, false, false, null);
        channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true);
        channel.queueBind(queueName, topic, routingKey);
        channel.basicQos(1);
        channel.basicConsume(queueName, false,
                getDefaultConsumer(channel, topic, topic, routingKey, queueName, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit subscribeWithTopic error.", e);
    }
}

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

License:Apache License

@Override
protected void doResponse(String address, Consumer<String> consumer) {
    Channel channel = rabbitAdapter.getConnection().createChannel(false);
    try {/*from w ww  .j a v  a2 s.c om*/
        channel.queueDeclare(address, true, false, false, null);
        channel.basicQos(1);
        channel.basicConsume(address, false,
                getDefaultConsumer(channel, address, "", address, address, consumer));
    } catch (IOException e) {
        logger.error("[MQ] Rabbit response error.", e);
    }
}