Before you speak Amqp, talk about the traditional JMS message model, with three participants in JMS: The producer of the message, the consumer, the channel (queue or topic) that delivered the message, and the two message models are as follows:
The channel is a queue:
The channel is a queue:
The channel is the subject:
In JMS, although channels help decouple the producers and consumers of messages, they are still coupled to the channel. Producers will post messages to a specific queue or topic, consumers receive them from a particular queue or topic, and the channel has a double responsibility to pass the data and determine where the messages are sent, and the queue will be sent using a point-to-point algorithm, with the topic using a publish-subscribe approach.
With AMQP, the producer does not directly publish the message to the queue, and the producer of the AMQP message and the queue that passes the message introduce an indirect mechanism exchange,exchange then bind to the queue. The diagram is as follows:
As you can see from the diagram, Exchange receives the message, and it routes the information to the queue, and the consumer then takes the data from the queue and processes it. Here exchange does not simply pass messages to the queue, and AMQP defines four different types of exchange, each with a different routing algorithm to decide whether to put the information in the queue. Depending on the exchange algorithm, it may use the routing key and/or parameters of the message and compare it to the binding and routing key and parameters between Exchange and the queue, if the comparison satisfies the corresponding algorithm, Then the message is routed to the queue.
Four different types of exchange defined in AMQP:
Direct: If the routing key of the message matches the binding's routing key directly, the message will be routed to that queue;
Topic: If the routing key of the message matches the routing key of the binding, the message will be routed to that queue;
Headers: If the header information and values in the message parameter table match the binding parameter table, the message will be routed to that queue;
Fanout: Regardless of the message's routing key and the header information/value of the parameter table, the message will be routed to all queues.
Here is an in-depth look at AMQP to www.amqp.org, which uses spring, AMQP implementations to send and receive messages.
Configure spring to support AMQP messages first
Before using spring AMQP, configure a connection factory first, specifically, here you choose to configure the RABBITMQ connection factory. RABBITMQ implements AMQP and is now a more commonly used message agent. Before using RABBITMQ to send Receive message, install RABBITMQ First, the specific installation step can find the installation guide on www.rabbitmq.com/download.html, this is not expanded in detail here.
The simplest way to configure the RABBITMQ connection factory is to use the Rabbit configuration namespace provided by spring AMQP to make sure that the schema is declared in the spring configuration file first:
<?xml version= "1.0" encoding= "UTF-8"?> <beans:beans xmlns= "http://www.springframework.org/schema/ Beans " xmlns:rabbit=" http://www.springframework.org/schema/rabbit " xsi:schemalocation="/http/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http ://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/ Spring-rabbit-1.0.xsd "> ...</beans:beans>
This configuration is not required, here is the choice to rabbit as the preferred namespace, the beans as the second namespace, because in this configuration, there will be more declarations rabbit instead of beans, so there will only be a small number of bean elements using the "beans:" prefix, The rabbit element avoids the use of prefixes.
The Rabbit namespace contains multiple elements that are configured RABBITMQ in spring, and the most used is <connection-factory>, which is best used when setting a bean ID. Otherwise it would be difficult to assemble the connection factory into the bean that needs it.
<connectioin-factory id= "ConnectionFactory" >
Connection factory By default, the RABBITMQ server listens to the 5672 port of localhost, user name password is guest, generally in the development environment can not be modified, in the production environment is modified as follows:
<connectioin-factory id= "ConnectionFactory" host= "${rabbitmq.host}" post= "${rabbitmq.post}" Username= "${rabbitmq.username}" password= "${rabbitmq.password}"/>
The benefit of this setting is that the specific value can be configured in the properties file.
Next look at how to create queues, Exchange, binding
In traditional JMS, the routing behavior of queues and topics is established by specification, and AMQP relies on how queues and exchange are defined and how they are bound together. One way to declare queues, exchange, and binding is to use the various methods of the RABBITMQ channel interface, but it is more convenient to use the Rabbit namespace, which contains multiple elements that can help us declare queues, Exchange and the binding that binds them together. The elements are as follows:
These configuration elements are used with the <admin> element to create a RABBITMQ management component that,<admin> automatically creates if the elements in the table above are not already present in the RABBITMQ agent.,<admin> elements.
For example, to declare a queue named Spittle.test.queue now, simply add the following configuration to the spring configuration:
<admin connection-factory= "ConnectionFactory"/><queue id= "Spittletestqueue" name= "Spittle.test"/>
When this is configured, Exchange defaults to direct Exchange without a name, and all queues are bound to this exchange, and the routing key is the same as the queue name. In this configuration, we can send the message to this non-named Exchange and set the routing key to spittle.test.queue so that the message is routed to this queue (which is actually similar to the JMS point-to-point model).
Other interesting routes require that we declare one or more of the exchange and bind it to the queue, for example, whatever routing key is, to route messages to multiple queues, you can configure one fanout and multiple queues as follows:
<admin connection-factory= "ConnectionFactory"/><queue name= "Spittle.test.queue.1" ><queue name= "spittle.test.queue.2" ><queue name= "spittle.test.queue.3" ><fanout-exchange name= " Spittle.fanout "> <bindings> <binding queue=" spittle.test.queue.1 "/> <binding Queue = "Spittle.test.queue.2"/> <binding queue= "spittle.test.queue.3"/> </bindings></ Fanout-exchange>
Other types of exchange readers can try them out according to the table above, and the next one will continue to write how to send messages.
Spring uses spring and AMQP to send receive messages (top)