码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-11 17:00:41      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

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             @Override
26             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!(下)

标签:

原文地址:http://www.cnblogs.com/zhengbin/p/5861989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!