RabbitMQ publish and subscribe (4)

1. Forwarders (Exchanges)

In the previous blog, our main introduction is that the sender sends messages to the queue, and the receiver receives messages from the queue. Below we will introduce Exchanges to show the complete message model of RabbitMQ.

The core idea of ​​the RabbitMQ message model is that the producer will never send any message directly to the queue, and in general the producer doesn't even know which queues the message should be sent to.

Conversely, the producer can only send messages to the forwarder (Exchange). A forwarder is very simple, one side receives messages from producers and the other side pushes messages to a queue. The repeater must know exactly what to do with each message it receives. Should it append to a specified queue? Should it append to multiple queues? Or should it be discarded? These rules are defined by the type of forwarder.


Some of the available transponder types are listed below:

Direct

Topic

Headers

Fanout

For now we focus on the last fanout, the code that declares the repeater type:

channel.exchangeDeclare("logs","fanout");

A fanout type forwarder is particularly simple, broadcasting all the messages it introduces to all the queues it knows about. But that's exactly what we need for our aforementioned logging system.

2. Nameless exchange

As mentioned earlier, the producer can only send messages to the forwarder (Exchange), but the examples in our previous two blogs did not use the forwarder, we can still send and receive messages. This is because we are using a default forwarder with the identifier "". The code to send the message before:

channel.basicPublish("", QUEUE_NAME,MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());

The first parameter is the name of the forwarder, which we set to "" : if there is a routingKey (the second parameter), the message is sent to which queue by the routingKey.

Now we can specify the forwarder to which the message is sent:

channel.basicPublish( "logs","", null, message.getBytes());

3. Temporary queues

In the previous blogs we have given the queue a specific name. Being able to name the queue is critical for us, we need to specify the consumer as a queue. Naming the queue is important when we want to share the queue between producers and consumers.
However, for our logging system we don't care about queue names. We want to receive all messages, and we are only interested in the data that is currently being delivered. To meet our needs, two things need to be done:
First, we need a new empty queue whenever we connect to Rabbit. To do this, we can create the queue with a random number, or better yet, have the server give us a random name.
Second, once the consumer is disconnected from the Rabbit, the queue that the consumer receives should be automatically deleted.
In Java, we can use the queueDeclare() method, without passing any parameters, to create a non-persistent, unique, automatically deleted queue and the queue name is randomly generated by the server.
String queueName = channel.queueDeclare().getQueue();
In general this name is similar to amq.gen-JzTY20BRgKO-HjmUJj0wLg.

4. Bindings
We've created a fanout repeater and queue, we now need to tell the repeater to send messages to our queue via bindings.
channel.queueBind(queueName, "logs", "") parameter 1: queue name; parameter 2: repeater name

5. Complete example
See git for specific code: https://github.com/wenjieatou/rabbitmq

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326140412&siteId=291194637