Introduction
RabbitMQ (Rabbit兔子) 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(术语).
1、Producing(生产) means nothing more than (只不过) sending. A program that sends messages is a producer :
2、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:
3、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.
"Hello World"
(using the Java Client)
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.
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
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.
In Send.java, we need some classes imported:
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
Set up the class and name the queue:
public class Send {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
...
}
}
then we can create a connection to the server:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
}
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 broker on the local machine - hence the localhost. If we wanted to connect to a broker on a different machine we'd simply specify its name 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:
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 + "'");
Declaring a queue is idempotent(声明队列是幂等的) - it will only be created if it doesn't exist already. The message content is a byte array, so you can encode whatever you like there.
Sending doesn't work!
If this is your first time using RabbitMQ and you don't see the "Sent" message then you may be left scratching your head(饶头) wondering what could be wrong. Maybe the broker was started without enough free disk space (by default it needs at least 200 MB free) and is therefore(因此) refusing to accept messages. Check the broker logfile to confirm and reduce(减少) the limit if necessary. The configuration file documentation will show you how to set disk_free_limit.