Spring AMQP Source Analysis 07-messagelisteneradapter

Source: Internet
Author: User
Tags rabbitmq

# # Ready# # Target Understand how spring AMQP handles messages with POJO # # pre-Knowledge "spring AMQP source Analysis 04-messagelistener"# # Related Resources Offical doc: gordon.study.rabbitmq.springamqp.AsyncConsumerWithAdapter. Java # # # analysis # # MessagelisteneradapterMessagelisteneradapterUsing the reflection mechanism, the ordinary POJO can process the message.Messagelisteneradapteritself to achieve theChannelawaremessagelistenerinterface, the core of the entire logic isOnMessageMethod. Line No. 269 Gets the object that actually processes the messageDelegate, which in this case isCommonprintbeanInstance. Next judgeDelegateWhether toMessageListenerorChannelawaremessagelistenerThe interface, if it is, calls the OnMessage method to process. In other words,a delegate instance of Messagelisteneradapter can It's MessageListener or Channelawaremessagelistener .。 For this example of this POJO delegate class, the No. 288 row extracts the message first.ExtractmessageMethod attempts to obtainMessageconverter,Messagelisteneradapter The default message converter is simplemessageconverter. If messageconverteris present, its frommessage method is called to convert the message to an object. Otherwise, the Message itself is returned directly. Note that thedefault simplemessageconverter of Spring AMQP is easy to deceptive, leaving an impression in your mind that the message will be converted by Messageconverter before it is consumed by the corresponding method! Line No. 289, based on the original message message, byGetlistenermethodnamemethod to determine which method the message should be consumed by. The core attribute isMessagelisteneradapterOfmap<string, string> queueortagtomethodname, whose key is the queue name or consumer tag, and the value is the method name. Other wordswe can set different methods for different queues, or we can set different methods for different Consumer .。If there is no matching method, the default method is used Handlemessage。 Line No. 297, according toConvertedmessageCreate a parameter list.MessagelisteneradapterThe argument list created is always an array of length 1, which means thata reasonable message handling method in POJO is bound to have only one parameter .
Line No. 298, using the reflection mechanism to call the corresponding method consumption message. ObviouslyConvertedmessage type determines which method of the same name the reflection invokes. # # sample Code Analysis in sample codeCommonprintbeanprovides three differentPrintmessageMethod. Consider the default usageSimplemessageconverter,ConvertedmessageType is string, so the string argument version is called  printmessage method.  If you open the 22nd line comment and set messageconverter to NULL, the Message parameter version is called printmessage method.  In general, the Object parameter version is not used  printmessage method, but providing this method ensures that the message processing method (error log) can be found in the messagelisteneradapter delegate POJO. # # Exception AnalysisThe business exception is exactly the same as the direct use of the MessageListener interface. The amqprejectanddontrequeueexception exception that is thrown in line 45th of the code directs the framework to reject the message and make it non-re-queued.  if the expected message consumption method does not exist, it will throw the nosuchmethodexceptionthat is listenerexecutionfailedexception wrapped, becausenosuchmethodexceptionis a defaultexceptionstrategy fatal exception, so the exception is amqprejectanddontrequeueexception packaging again. The Asyncmessageprocessingconsumer Run method loops through the consumption message logic and encountersAsyncmessageprocessingconsumerdirect Silent processing. Therefore, if there is no corresponding method, the framework will eventually transfer all messages to the dead-letter queue .    

Spring AMQP Source Analysis 07-messagelisteneradapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.