Recently, I have been studying asynchronous messaging mechanisms. I used the rabbit MQ message framework and developed it using Erlang to provide Java jar packages.
Amqp mainly involves two components: Exchange and queue (there will be changes in amqp 1.0), both of which are on the server side, also known as broker, which is implemented by rabbitmq. The client usually has two types: producer and consumer.
The general procedure is:
(1) Consumer: create an information channel.
(2) Consumer: defines a message queue.
(3) Consumer: Define a vswitch of a specific type.
(4) Consumer: set binding rules (including switch name, queue name, and route key ).
(5) Consumer: waiting for a message.
(6) Producer: Creates a message.
(7) Producer: deliver the message to the information channel (indicating the name and route key of the receiving switch ).
(8) vswitch: gets the message and determines whether to match the routing rule based on the switch type. (If yes, compare the message routing key with the bound route key ).
(9) Consumer: obtains and processes messages and sends feedback.
(10) end: Close the channel and connection.
Queue definition parameters:
Durable: whether it is persistent. If it is true, it still exists after the service is restarted; otherwise, it does not exist.
Exclusive: only private queues that the creator can use are automatically deleted after being disconnected. (If it is true, only the Creator thread can be used. If the thread is stopped, the queue is automatically deleted)
Auto_delete: determines whether to automatically delete a queue when all consumer clients are disconnected.
Vswitch definition parameters:
Type: vswitch type, including fanout, direct, and topic.
Auto_delete: whether to automatically delete the vswitch when all bound queues are no longer used.
Switch Type:
Fanout: broadcasts messages to all queues bound to the vswitch without processing the route key. Regardless of the message's routing keyword, the message is routed to all the queues bound to the switch.
The following describes how a broadcast switch works:
No parameters are used to bind the Message Queue with the switch. The publisher (the producer in the direct switch type description is changed to publisher, which implies the difference between the two switch types) sends a message to the switch. Messages are unconditionally transmitted to all message queues bound to the switch.
Direct: process the route key to match the full text of the message path. The message routing key "dog" can only match "dog" binding, but does not match "dog. Puppy" binding.
By precisely matching the routing Keywords of a message, the message is routed to zero or multiple queues. The binding keywords are used to bind the queue and the switch together. This allows us to build a classic point-to-point queue message transmission model. However, like any defined switch type, when the message routing keyword matches multiple bound keywords, messages may be sent to multiple queues.
Topic: process the route key and match the route key by mode. The pattern symbol "#" indicates one or more words. "*" matches only one word. For example, "audit. #" can match "audit. IRS. initate", but "audit. *" only matches "audit. IRS ".
The topic switch type provides a routing mechanism to route messages to the bound queue by matching the message's routing keyword with the binding keyword mode. This type of router can be used to support the classic publish/subscribe message transmission model-the topic namespace is used as the message addressing mode, transmit messages to multiple consumers that partially or all match the topic pattern.
The topic switch type works as follows:
The binding keyword is composed of zero or multiple tags, and each tag is separated by the "." character. The binding keyword must be explicitly stated in this form, and the wildcard character "*" must match one phrase, and "#" must be zero or multiple phrases.
Therefore, the binding keyword "*. Stock. #" matches the routing keyword "USD. Stock" and "EUR. Stock. DB", but does not match "stock. NASDAQ ".
This switch type is optional.
The producer does not need to define queues, switches, and bindings, but only needs to deliver messages to the information channel.
If you define a durable queue, bind the exchange of this queue must also be durable
Define User Permissions
Set_permissions [-P vhostpath] [-s scope] {user} {conf} {write} {read}
Vhostpath
The name of the virtual host to which to grant the user access, defaulting /.
Scope
Scope of the permissions: either client (the default) or all. this determines whether permissions are checked for server-generated resource names (all) or only for client-specified resource names (client ).
User
The name of the user to grant access to the specified virtual host.
Conf
A regular expression matching resource names for which the user is granted Configure permissions.
Write
A regular expression matching resource names for which the user is granted write permissions.
Read
A regular expression matching resource names for which the user is granted read permissions.
Sets user permissions.
For example:
Rabbitmqctl set_permissions-P/myvhost tonyg "^ tonyg -. *"". *"". * "this command instructs the rabbitmq broker to grant the user named tonyg access to the virtual host called/myvhost, with Configure permissions on all resources whose names starts with" tonyg -", and write and read permissions on all resources.
Simplemessagelistenercontainer in spring amqp is encapsulated by spring and contains a series of bean initialization actions. In the current use case, the start method is directly called because the program needs to dynamically place the queue through the program, after reading the source code of spring, it is called during bean initialization.
At present, there are few Chinese tutorials on rabbit MQ. When I first came into contact, I only looked at documents on the Official Site and provided official examples. Because spring-amqp is used, we can see that spring-amqp has a little more time, providing a simple hello world and a complex stock example, let's take a look at the example of Hello world with the API. Then we can get started with spring amqp reference. Of course, we should be familiar with amqp concepts before. It seems that rabbit documents are really few, especially Java interfaces, and it takes a lot of time to get rid of those documents.