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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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             @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!(下)的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMQ指南之一:"Hello World!"

RabbitMQ - Hello World!

RabbitMQ消息队列:”Hello, World“

RabbitMQ消息队列:”Hello, World“

RabbitMQ ——“Hello World”

rabbitMQ实战---------使用pika库实现hello world