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:com.frannciscocabral.ufs.rabbitmq.Receiver.java

public static void main(String[] argv)
        throws java.io.IOException, java.lang.InterruptedException, TimeoutException {

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

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override/*  w  w w  .  j a v a  2s . c o m*/
        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 + "'");
        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}

From source file:com.frannciscocabral.ufs.rabbitmq.Send.java

public static void main(String[] argv) throws java.io.IOException, TimeoutException {
    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());
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();//from   w  w w. ja v  a  2 s  .  c  om
    connection.close();
}

From source file:com.gettec.fsnip.fsn.web.controller.rest.deal.DealProblemRESTService.java

/**
 * ?//from  w  w w  .  j  a  v  a2 s. c o m
 * @param name
 * @param model
 * @return
 */
@RequestMapping(method = RequestMethod.POST, value = "/editDealToProblem/{id}")
public View editDealToProblem(@PathVariable(value = "id") long id, Model model, HttpServletRequest req,
        HttpServletResponse resp) {
    try {
        AuthenticateInfo info = SSOClientUtil.validUser(req, resp);
        RabbitMQUtil rabbitMQUtil = new RabbitMQUtil();
        Channel channel = rabbitMQUtil.connect();
        channel.exchangeDeclare(SUPERVISE_JG, SUPERVISE_DIRECT, true);
        channel.queueDeclare(SUPERVISE_JG, true, false, false, null);
        channel.queueBind(SUPERVISE_JG, SUPERVISE_JG, SUPERVISE_KEY);
        JSONObject jsonObject = dealToProblemService.getDealProblemLogVO(id, info);
        channel.basicPublish("", SUPERVISE_JG, MessageProperties.PERSISTENT_TEXT_PLAIN,
                jsonObject.toString().getBytes("UTF-8"));
        model.addAttribute("status", true);
    } catch (Exception e) {
        model.addAttribute("status", false);
        e.printStackTrace();
    }
    return JSON;
}

From source file:com.gettec.fsnip.fsn.web.controller.rest.deal.DealProblemRESTService.java

@RequestMapping(method = RequestMethod.GET, value = "/notice/{id}/{status}/{pageStatus}")
public View notice(@PathVariable("id") long id, @PathVariable("status") long status,
        @PathVariable("pageStatus") long pageStatus, Model model, HttpServletRequest request,
        HttpServletResponse response) {/*  www. j a v  a 2s .  c om*/

    try {
        DealProblem sendproblem = dealProblemService.noticeComplainById(id, status);

        if (status == 0) {
            //??RabbitMQ,
            RabbitMQUtil rabbitMQUtil = new RabbitMQUtil();
            Channel channel = rabbitMQUtil.connect();
            channel.exchangeDeclare(SUPERVISE_JG, SUPERVISE_DIRECT, true);
            channel.queueDeclare(SUPERVISE_JG, true, false, false, null);
            channel.queueBind(SUPERVISE_JG, SUPERVISE_JG, SUPERVISE_KEY);
            JSONObject jsonObject = dealToProblemService.getJsonDealToProblem(sendproblem, pageStatus);
            channel.basicPublish("", SUPERVISE_JG, MessageProperties.PERSISTENT_TEXT_PLAIN,
                    jsonObject.toString().getBytes("UTF-8"));
        } else if (status == 1) {
            //?
            dealToProblemService.getNoticeDealToProblem(sendproblem);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return JSON;
}

From source file:com.github.hexsmith.rabbitmq.consumer.MessageConsumer.java

License:Open Source License

public boolean consume(String queueName) {
    //RabbitMQ//from   www.  j a v  a  2  s  .c  o  m
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    Connection connection = null;
    Channel channel = null;
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        //queue??queue
        //?queueDeclare???queue
        channel.queueDeclare(queueName, false, false, false, null);

        //?DefaultConsumerhandleDelivery???getByte()???String
        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");
                logger.info("Received '" + message + "'");
            }
        };
        //??
        channel.basicConsume(queueName, true, consumer);

        //???rabbitMQ

    } catch (IOException | TimeoutException e) {
        //?false
        logger.error("send message failed!", e);
        return false;
    }

    return true;
}

From source file:com.github.hexsmith.rabbitmq.consumer.MultiMessageConsumer.java

License:Open Source License

public boolean consume(String queueName, String consumerName) {
    //RabbitMQ/*from www .ja  va  2 s.  c  om*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    Connection connection = null;
    Channel channel = null;
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        //queue??queue
        //?queueDeclare???queue
        channel.queueDeclare(queueName, false, false, false, null);

        //?DefaultConsumerhandleDelivery???getByte()???String
        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");
                logger.info("[{}]" + "Received '" + message + "'", consumerName);
            }
        };
        //??
        channel.basicConsume(queueName, true, consumer);

        //???rabbitMQ

    } catch (IOException | TimeoutException e) {
        //?false
        logger.error("send message failed!", e);
        return false;
    }

    return true;
}

From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java

License:Open Source License

public boolean sendMessage(String message) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.7");
    Connection connection = null;
    Channel channel = null;
    try {//from w  w  w .  j a v a 2  s .  c o m
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        logger.info("send message = {}", message);
        channel.close();
        connection.close();
    } catch (IOException | TimeoutException e) {
        logger.error("send message failed!,exception message is {}", e);
        return false;
    }
    return true;
}

From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java

License:Open Source License

public boolean sendMulitMessage(String message) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.7");
    Connection connection = null;
    Channel channel = null;
    try {/*w  w  w  . j  av  a  2s  .c  o m*/
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        int i = 0;
        int loop = 0;
        String originalMessage = message;
        while (loop < 10000) {
            loop++;
            message += i++;
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            logger.info("send message = {}", message);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            message = originalMessage;
        }

        channel.close();
        connection.close();
    } catch (IOException | TimeoutException e) {
        logger.error("send message failed!,exception message is {}", e);
        return false;
    }
    return true;
}

From source file:com.github.kislayverma.dredd.action.async.amqp.AmqpActionQueue.java

License:Apache License

public AmqpActionQueue(ConnectionFactory factory, String queue, String exchange, String routingKey)
        throws IOException, TimeoutException {
    this.factory = factory;
    this.queue = queue;
    this.exchange = exchange;
    this.routingKey = routingKey;

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchange, "direct", true);
    channel.queueDeclare(queue, true, false, false, null);
    channel.queueBind(queue, exchange, routingKey);
}

From source file:com.github.liyp.rabbitmq.rpc.RPCServer.java

License:Apache License

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {//  www. jav a 2  s .c om
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

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

        channel.basicQos(1);

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

        System.out.println(" [x] Awaiting RPC requests");

        while (true) {
            String response = null;

            QueueingConsumer.Delivery delivery = consumer.nextDelivery();

            BasicProperties props = delivery.getProperties();
            BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                    .build();

            try {
                String message = new String(delivery.getBody(), "UTF-8");
                int n = Integer.parseInt(message);

                System.out.println(" [.] fib(" + message + ")");
                response = "" + fib(n);
            } catch (Exception e) {
                System.out.println(" [.] " + e.toString());
                response = "";
            } finally {
                channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));

                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}