楼主: ReneeBK
1297 11

RabbitMQ Cookbook [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


  1. Author:Gabriele Santomaggio, Sigismondo Boschi
  2. ISBN-10:1849516502
  3. Year:2013
  4. Pages:288
  5. Language:English
  6. File size:5.66 MB
  7. File format:PDF
  8. Category:Python
复制代码

本帖隐藏的内容

RabbitMQ Cookbook.pdf (16.6 MB, 需要: 5 个论坛币)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook Rabbit Book Cook ABB

沙发
ReneeBK 发表于 2017-8-27 02:41:08 |只看作者 |坛友微信交流群
  1. Connecting to the broker

  2. Every application that uses AMQP needs to establish a connection with the AMQP broker. By default, RabbitMQ (as well as any other AMQP broker up to version 1.0) works over TCP as a reliable transport protocol on port 5672, that is, the IANA-assigned port.

  3. We are now going to discuss how to create the connection. In all the subsequent recipes we will refer to the connection and channel as the results of the operations presented here.

  4. Getting ready

  5. To use this recipe we need to set up the Java development environment as mentioned in the Introduction section.

  6. How to do it…

  7. In order to create a Java client that connects to the RabbitMQ broker, you need to perform the following steps:

  8. Import the needed classes from the Java RabbitMQ client library in the program namespace:
  9. import com.rabbitmq.client.Channel;
  10. import com.rabbitmq.client.Connection;
  11. import com.rabbitmq.client.ConnectionFactory;
  12. Create an instance of the client ConnectionFactory:
  13. ConnectionFactory factory = new ConnectionFactory();
  14. Set the ConnectionFactory options:
  15. factory.setHost(rabbitMQhostname);
  16. Connect to the RabbitMQ broker:
  17. Connection connection = factory.newConnection();
  18. Create a channel from the freshly created connection:
  19. Channel channel = connection.createChannel();
  20. As soon as we are done with RabbitMQ, release the channel and the connection:
  21. channel.close();
  22. connection.close();
复制代码

使用道具

藤椅
ReneeBK 发表于 2017-8-27 02:43:25 |只看作者 |坛友微信交流群
  1. Producing messages

  2. In this recipe we are learning how to send a message to an AMQP queue. We will be introduced to the building blocks of AMQP messaging: messages, queues, and exchanges.

  3. You can find the source at Chapter01/Recipe02/src/rmqexample.

  4. Getting ready

  5. To use this recipe we need to set up the Java development environment as indicated in the Introduction section.

  6. How to do it…

  7. After connecting to the broker, as seen in the previous recipe, you can start sending messages performing the following steps:

  8. Declare the queue, calling the queueDeclare() method on com.rabbitmq.client.Channel:
  9. String myQueue = "myFirstQueue";
  10. channel.queueDeclare(myQueue, true, false, false, null);
  11. Send the very first message to the RabbitMQ broker:
  12. String message = "My message to myFirstQueue";
  13. channel.basicPublish("",myQueue, null, message.getBytes());
  14. Send the second message with different options:
  15. channel.basicPublish("",myQueue,MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
复制代码

使用道具

板凳
ReneeBK 发表于 2017-8-27 02:44:12 |只看作者 |坛友微信交流群
  1. Consuming messages

  2. In this recipe we are closing the loop; we have already seen how to send messages to RabbitMQ—or to any AMQP broker—and now we are ready to learn how to retrieve them.

  3. You can find the source code of the recipe at Chapter01/Recipe03/src/rmqexample/nonblocking.

  4. Getting ready

  5. To use this recipe we need to set up the Java development environment as indicated in the introduction.

  6. How to do it…

  7. In order to consume the messages sent as seen in the previous recipe, perform the following steps:

  8. Declare the queue where we want to consume the messages from:
  9. String myQueue="myFirstQueue";
  10. channel.queueDeclare(myQueue, true, false, false, null);
  11. Define a specialized consumer class inherited from DefaultConsumer:
  12. public class ActualConsumer extends DefaultConsumer {
  13.   public ActualConsumer(Channel channel) {
  14.     super(channel);
  15.   }
  16.   @Override
  17.   public void handleDelivery(
  18.     String consumerTag,
  19.     Envelope envelope,
  20.     BasicProperties properties,
  21.     byte[] body) throws java.io.IOException {
  22.       String message = new String(body);
  23.       System.out.println("Received: " + message);
  24.     }
  25. }
  26. Create a consumer object, which is an instance of this class, bound to our channel:
  27. ActualConsumer consumer = new ActualConsumer(channel);
  28. Start consuming messages:
  29. String consumerTag = channel.basicConsume(myQueue, true, consumer);
  30. Once done, stop the consumer:
  31. channel.basicCancel(consumerTag);
复制代码

使用道具

报纸
ReneeBK 发表于 2017-8-27 02:45:02 |只看作者 |坛友微信交流群
  1. Using body serialization with JSON

  2. In AMQP the messages are opaque entities; AMQP does not provide any standard way to encode/decode them.

  3. However, web applications very often use JSON as an application layer format, that is, the JavaScript serialization format that has become a de-facto standard; in this way, the RabbitMQ client Java library can include some utility functions for this task.

  4. On the other side, this is not the only protocol; any application can choose its own protocol (XML, Google Protocol Buffers, ASN.1, or proprietary).

  5. In this example we are showing how to use the JSON protocol to encode and decode message bodies. We are using a publisher written in Java (Chapter01/Recipe04/Java_4/src/rmqexample) and a consumer in Python (Chapter01/Recipe04/Python04).

  6. Getting ready

  7. To use this recipe you will need to set up Java and Python environments as described in the introduction.

  8. How to do it…

  9. To implement a Java producer and a Python consumer, you can perform the following steps:

  10. Java: In addition to the standard import described in the recipe Connecting to the broker, we have to import:
  11. import
  12. com.rabbitmq.tools.json.JSONWriter;
  13. Java: We create a queue that is not persistent:
  14. String myQueue="myJSONBodyQueue_4";
  15.   channel.queueDeclare(MyQueue, false, false, false, null);
  16. Java: We create a list for the Book class and fill it with example data:
  17. List<Book>newBooks = new ArrayList<Book>();
  18.   for (inti = 1; i< 11; i++) {
  19.     Book book = new Book();
  20.     book.setBookID(i);
  21.     book.setBookDescription("History VOL: " + i  );
  22.     book.setAuthor("John Doe");
  23.     newBooks.add(book);
  24.   }
  25. Java: We are ready to serialize the newBooks instance with JSONwriter:
  26. JSONWriter rabbitmqJson = new JSONWriter();
  27. String jsonmessage = rabbitmqJson.write(newBooks);
  28. Java: We can finally send our jsonmessage:
  29. channel.basicPublish("",MyQueue,null, jsonmessage.getBytes());
  30. Python: To use the Pika library we must add the follow import:
  31. import pika;
  32. import json;
  33. Python has a powerful built-in library for JSON.

  34. Python: In order to create a connection to RabbitMQ, use the following code:
  35. connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host));
  36. Python: Let's declare a queue, bind as a consumer, and then register a callback:
  37. channel = connection.channel()
  38. my_queue = "myJSONBodyQueue_4"
  39. channel.queue_declare(queue=my_queue)
  40. channel.basic_consume(consumer_callback, queue=my_queue, no_ack=True)
  41. channel.start_consuming()
复制代码

使用道具

地板
军旗飞扬 发表于 2017-8-27 07:00:14 |只看作者 |坛友微信交流群
谢谢楼主分享!

使用道具

7
yazxf 发表于 2017-8-27 08:24:55 |只看作者 |坛友微信交流群
谢谢你的书!

使用道具

8
MouJack007 发表于 2017-8-27 11:54:21 |只看作者 |坛友微信交流群
谢谢楼主分享!

使用道具

9
MouJack007 发表于 2017-8-27 11:55:07 |只看作者 |坛友微信交流群

使用道具

10
franky_sas 发表于 2017-8-27 13:18:29 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 03:08