RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.

The major difference between RabbitMQ and the post office is that it doesn't deal with paper, instead it accepts, stores and forwards binary blobs of data‒messages.


RabbitMQ, and messaging in general, uses some jargon.


  • Producing means nothing more than sending. A program that sends messages is a producer :
  • 生产无非就是发送。发送消息的程序是一个生产者

  • A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host's memory & disk limits, it's essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:
  • 队列是RabbitMQ内部邮箱的名称。尽管消息通过RabbitMQ和应用程序传递,但它们只能存储在队列中。队列只受主机内存的限制。磁盘限制,它本质上是一个大的消息缓冲区。许多生产者可以发送到一个队列的消息,而许多消费者可以尝试从一个队列接收数据。这就是我们表示队列的方式

  • Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:
  • “消费”和“接受”有相似的含义。消费者是一个主要等待接收消息的程序

Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don't. An application can be both a producer and consumer, too.


In this part of the tutorial we'll write two programs in Java; a producer that sends a single message, and a consumer that receives messages and prints them out. We'll gloss over some of the detail in the Java API, concentrating on this very simple thing just to get started. It's a "Hello World" of messaging. In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.

在本教程的这一部分,我们将用Java编写两个程序;发送单个消息的生产者和接收消息并将其打印出来的消费者。我们将忽略Java API中的一些细节,只关注这个非常简单的东西作为开始。这是一个“你好世界”的消息传递。在下图中,“P”是我们的生产者,“C”是我们的消费者。中间的盒子是一个队列——RabbitMQ代表使用者保存的消息缓冲区。

![(P) -> [|||] -> (C)](https://p3-juejin.byteimg.com...

The Java client library

RabbitMQ speaks multiple protocols. This tutorial uses AMQP 0-9-1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We'll use the Java client provided by RabbitMQ.

Download the client library and its dependencies (SLF4J API and SLF4J Simple). Copy those files in your working directory, along the tutorials Java files.

Please note SLF4J Simple is enough for tutorials but you should use a full-blown logging library like Logback in production.

(The RabbitMQ Java client is also in the central Maven repository, with the groupId com.rabbitmq and the artifactId amqp-client.)

Now we have the Java client and its dependencies, we can write some code.

Sending 发送

![(P) -> [|||]](https://p3-juejin.byteimg.com...

We'll call our message publisher (sender) Send and our message consumer (receiver) Recv. The publisher will connect to RabbitMQ, send a single message, then exit.


The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a RabbitMQ node on the local machine - hence the localhost. If we wanted to connect to a node on a different machine we'd simply specify its hostname or IP address here.


Next we create a channel, which is where most of the API for getting things done resides. Note we can use a try-with-resources statement because both Connection and Channel implement java.io.Closeable. This way we don't need to close them explicitly in our code.


To send, we must declare a queue for us to send to; then we can publish a message to the queue, all of this in the try-with-resources statement:


package io.moocstudent.rabbitmqlearning.demo;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
 * @Author: zhangQi * @Date: 2020-11-20 11:28 */public class Send {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        try (Connection connection = factory.newConnection();//get new connection from factory
 Channel channel = connection.createChannel()) {
            String message = "Hello World!";
        } catch (TimeoutException e) {
        } catch (IOException e) {


Receiving 接收者

That's it for our publisher. Our consumer listens for messages from RabbitMQ, so unlike the publisher which publishes a single message, we'll keep the consumer running to listen for messages and print them out.


topic类型demo [java]