Consumer.java 730 B

12345678910111213141516171819202122
  1. package com.sf.message.pull;
  2. import com.rabbitmq.client.Channel;
  3. import com.rabbitmq.client.GetResponse;
  4. import com.sf.util.RabbitMqUtils;
  5. public class Consumer {
  6. private static String queueName = "hello1";
  7. public static void main(String[] args) throws Exception{
  8. Channel channel = RabbitMqUtils.getChannel();
  9. // 消息的处理有两种模式 推和拉
  10. // basicAck 推送,是由mq主动推送的方式
  11. // basicGet 拉取,消费者主动获取消息
  12. GetResponse response = channel.basicGet(queueName, true);
  13. byte[] body = response.getBody();
  14. String message = new String(body, "UTF-8");
  15. System.out.println(message);
  16. channel.close();
  17. }
  18. }