首页 > 代码库 > RabbitMQ文档翻译——Hello World!(下)

RabbitMQ文档翻译——Hello World!(下)

Receiving

源码:

技术分享
 1 package Consuming; 2  3 import com.rabbitmq.client.*; 4  5 import java.io.IOException; 6  7 /** 8  * Created by zhengbin06 on 16/9/11. 9  */10 public class Recv {11     private final static String QUEUE_NAME = "hello";12     13     public static void main(String[] argv)14             throws java.io.IOException,15             java.lang.InterruptedException {16         17         ConnectionFactory factory = new ConnectionFactory();18         factory.setHost("localhost");19 //        factory.setPort(5672);20         Connection connection = factory.newConnection();21         Channel channel = connection.createChannel();22         channel.queueDeclare(QUEUE_NAME, false, false, false, null);23         System.out.println(" [*] Waiting for messages. To exit press CTRL+C");24         Consumer consumer = new DefaultConsumer(channel) {25             @Override26             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)27                     throws IOException {28                 String message = new String(body, "UTF-8");29                 System.out.println(" [x] Received ‘" + message + "‘");30             }31         };32         channel.basicConsume(QUEUE_NAME, true, consumer);33     }34 }
View Code

java运行命令

java -cp命令解释

RabbitMQ文档翻译——Hello World!(下)