# # Ready# # Target Understand the Spring AMQP core code
# # Pre-Knowledge rabbitmq Getting Started # # Related Resources Quick tour for the impatient:
gordon.study.rabbitmq.springamqp.Impatient. Java
# # # Analysis # # ConnectionFactory Analysis org.springframework.amqp.rabbit.connection.ConnectionFactory is the connection factory interface defined by Spring AMQP and is responsible for creating the connection. Note that the RabbitMQ client also has a connection factory with the same name, which is defined by Spring AMQP .
Org.springframework.amqp.rabbit.connection.CachingConnectionFactory is an implementation class of ConnectionFactory. Its properties com.rabbitmq.client.ConnectionFactory rabbitconnectionfactory refers to a real RABBITMQ connection factory, typically in the constructor is Create it . All the actual connections that follow are through thisrabbitconnectionfactorycreated, Cachingconnectionfactory just manages these connections. Unlike the official website sample code, line 16th defines a URI that indicates access to the localhost:5672 RabbitMQ service through the guest user. The reason is that the host that I automatically parse through Spring AMQP when I am running a machine name instead of localhost causes the guest user to not have permission to connect and throws an exception, so I need to proactively set the value of host to localhost. In addition to the methods specified by the URI that are used in the sample code, there are several ways to do this:
1. You can specify the hostname constructor by using the. Narrow application, can not set user name and other connection factory propertiesconnectionfactory connectionfactory = new Cachingconnectionfactory ("localhost");2. Create your owncom.rabbitmq.client.ConnectionFactorycom.rabbitmq.client.ConnectionFactory rabbitconnfactory = new Com.rabbitmq.client.ConnectionFactory ();rabbitconnfactory.sethost ("localhost");rabbitconnfactory.setautomaticrecoveryenabled (false);connectionfactory connectionfactory = new Cachingconnectionfactory (rabbitconnfactory);3. Byabstractconnectionfactoryget to internal rabbitconnectionfactory abstractconnectionfactory connectionfactory = new Cachingconnectionfactory ();connectionfactory.getrabbitconnectionfactory (). Sethost ("localhost");4.throughabstractconnectionfactoryencapsulated Method Indirect operationrabbitconnectionfactoryabstractconnectionfactory connectionfactory = new Cachingconnectionfactory ();connectionfactory.sethost ("localhost");# # Amqpadmin AnalysisOrg.springframework.amqp.core.AmqpAdminThe interface defines the AMQP basic management operations, primarily the declaration and deletion of various resources (switches, queues, bindings).Org.springframework.amqp.rabbit.core.RabbitAdminImplements the Amqpadmin interface. PassThe constructor is passed in to the ConnectionFactory instance created earlier, and is set to the connectionfactory connectionfactory property of Rabbitadmin. A rabbittemplate instance is also created in the constructor, set torabbittemplate rabbittemplatePropertythe. The declarequeue method is used to declare the queue. Org.springframework.amqp.core. The queue is a wrapper for the Spring AMQP queues, and its properties are basically consistent with the properties of the RabbitMQ defined in the Java client,new Queue ("Spring"); Equivalent to RabbitMQ Java client in channel.queuedeclare ("Spring", True, False, false, null); Specifies the queue attribute, that is, the queue is persisted, non-exclusive, and not automatically deleted. Declarequeuecalledrabbittemplateof theExecute (channelcallback) method, callback method in Channelcallback doinrabbit (com.rabbitmq.client.Channel) The Channel.queuedeclare method declaration queue provided by the RabbitMQ Java client is invoked through the incoming channel . There is a detail in the code: All queues beginning with "AMQ." are ignored by the Spring AMQP framework and do not trigger the Channel.queuedeclare method call . Rabbittemplate details are analyzed below. # # Amqptemplate Analysisorg.springframework.amqp.core.AmqpTemplateThe interface defines the AMQP base operations, which are primarily synchronous messaging methods. org.springframework.amqp.rabbit.core.RabbitTemplateImplements the Amqptemplate interface. Similar to Rabbitadmin,Rabbittemplate also needs to refer to external connectionfactory for creating the connection. As the name implies, Rabbittemplate is the template method for RabbitMQ to send and receive messages, similar to the JdbcTemplate design. Sorabbittemplateto implement template code such as creating a connection, getting a channel, sending and receiving messages (and so on), message format conversion, and closing channels and connections. For example, for the convertandsend (String routingkey, Object message) method, the convertmessageifnecessary method is first used to The Object message is converted to an org.springframework.amqp.core.Message instance. The message class is the encapsulation of Spring AMQP messages. The Convertmessageifnecessary method is created by getting the Rabbittemplate constructor in the Org.springframework.amqp.support.converter.SimpleMessageConverter, call its tomessage method to complete the Message The creation of instances (that is, message conversions). Next, withExecute (channelcallback action, connectionfactory connectionfactory) method, doinrabbit (Channel) of an anonymous real class on the Channelcallback interfaceMethod is implemented in theto send the message function, the code is as follows:Execute (channelcallback action, ConnectionFactory Connection) method andChannelcallbackinterface Cooperation, successfullyThe sending of messages (and so on) is isolated from the code that obtains the connection and the channel . The Execute method obtains the connection and the channel through the incoming ConnectionFactory,Channelcallbackinterface ofDoinrabbit (channel channel)Partyas a callback function, the channel parameter is used to accept the channels obtained in the Execute method, and the specific business of message sending is completed. The code implementation is simple, after the Execute method gets to channel channels, call action.doinrabbit (channel); Can. Call stack information: Dosendmethod to implement a message to send this specific operation. The essence is provided by invoking the RabbitMQ Java clientOfChannel.basicpublishmethod to send a message. after the business operation is completed, the Execute method reclaims the connection and channel resources, and the entire message-Sending template function is complete. the Receiveandconvert Method realizes the thought andConvertandsendbasically consistent, the call stack is as follows:
Spring AMQP Source Analysis 01-impatient