简体   繁体   中英

Two questions about RabbitMQ

Hi,all

I have two questions when using RabbitMQ in ubuntu with jJava.

First,how to get a message from a queue? There are two 'corret' ways,such as:

ConnectionFactory factory = new ConnectionFactory(params);  
Connection conn = factory.newConnection("localhost", 5672);  
Channel channel = conn.createChannel(); 
String message=channel.basicGet("queuename", noAck).getBody();

and the other one is

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("queuename", true, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());

so which one is correct? If both are,what is the difference between them?

Second, How can I know the the number of messages in a queue? That is, I want to check whether the queue is empty or not.

Are there some APIs that I can use? channel.basicGet ?

If there are some messages with ack in the queue,what will channel.basicGet return?Actually,i have no idea what the second parameter of channel.basicGet mean....

Thank you very much

The first option is to retrieving individual messages. The noAck paramter is boolean says to auto ack or no. if it set to no, you need to manually ack.

See more at: http://www.rabbitmq.com/api-guide.html

The second option using the QueueingConsumer. a class used to buffer the messages pushed to us by the server. you also creating the queue in this option:

channel.queueDeclare("queuename", true, false, false, null);

See more on this option here: http://www.rabbitmq.com/tutorials/tutorial-one-java.html

One last thing. if you are familiar with Spring , i suggest you read this as well: http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/

I used it. Its easy and clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM