RabbitMQ learning-the second quick start HelloWorld

1. Related concepts

RabbitMQ is a message broker, in fact, it receives messages generated by producers and then passes the messages to consumers. In this process, it can be routed, buffered, or more persistent according to the rules you set. RabbitMQ and message transmission generally use some terms:

  • Producer (Producing) : It
    means nothing more than the end that sends the message. If a program sends a message, it will be called a producer, which is represented by a capital P.

  • Queue (queue) :
    equivalent to the name of the mailbox, it is active in the RabbitMQ server. Although the message flow will go through RabbitMQ and your application, it will only be stored in the queue. The queue is not subject to any restrictions. It can store as many messages as you need (in essence, it is an infinite buffer). Multiple producers can send messages to the same message queue, or multiple consumers can receive messages from a message queue at the same time.

  • Consumer (Consuming) :
    That is, the receiving end, the consumer is mainly a program waiting to receive messages.

    Note: In most application scenarios, producers, consumers, and RabbitMQ services will not run on the same machine at the same time.

2. Java client package

RabbitMQ complies with the AMQP protocol, which is an open and universal messaging protocol. Regarding the AMQP protocol client, there are multiple language implementation versions. This article uses the Java client provided by RabbitMQ.

It can be downloaded from http://www.rabbitmq.com/java-client.html, and then copy the corresponding Jar package to your working directory. The Java client of RabbitMQ is also available in the Maven library, groupId is com.rabbitmq, and artifactId is amqp-client.

3. Client

We named the message sender Send and the message receiver Recv. The sender will connect to RabbitMQ, send a message, and then exit.

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class Send {
  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws java.io.IOException {
    /*连接可以抽象为socket连接,为我们维护协议版本信息和协议证书等。这里我们连接
    上了本机的消息服务器实体(localhost)。如果我们想连接其它主机上的RabbitMQ服务,只需要修改一下主机名或是IP就可以了*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    /*接下创建channel(信道),这是绝大多数API都能用到的。为了发送消息,你必须要声明一个消息消息队列,然后向该队列里推送消息*/
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
    
    /*声明一个幂等的队列(只有在该队列不存在时,才会被创建)。消息的上下文是一个
      字节数组,你可以指定它的编码。*/
    channel.close();
    connection.close();
  }
}

Note: If there is insufficient disk space in the RabbitMQ service, the operation will be error. The parameter is set to: disk_free_limit. More parameter configurations can be found here http://www.rabbitmq.com/configure.html#config-items

4. Receiver

RabbitMQ will push the message to the receiver, unlike the sender that only sends a message, here we will listen to the message and print the message.

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;

public class Recv {

  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException {
    /*这里怎么打开连接和信道,以及声明用于接收消息的队列,这些步骤与发送端基本上是一样的*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    
    /*确保这里的队列是存在的*/
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
    /*这里用到了额外的类QueueingConsumer来缓存服务器将要推过来的消息。我们通知服务器向接收端推送消息,
   然后服务器将会向客户端异步推送消息,这里提供了一个可以回调的对象来缓存消息,直到我们做好准备来使用
    它,这个类就是QueueingConsumer*/
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
      System.out.println(" [x] Received '" + message + "'");
    }
  }
}

After receiving the message, QueueingConsumer.nextDelivery () will block and suspend operation until other messages are pushed.

If you need to check and verify the queue, you need to use rabbitmqctl list_queues.

Published 40 original articles · 25 praises · 100,000+ views

Guess you like

Origin blog.csdn.net/yym373872996/article/details/105651672