Amoeba Source Code Analysis 1: Startup Process Analysis

Source: Internet
Author: User

There is very little information on the Internet. I just want to do this again. Simply write it out. I am also exploring it. If I understand it incorrectly, I apologize here. please correct me. It is also a little contribution to the promotion of amoeba!


Is part of the directory of my eclipse amoeba project (for how to import it, see the previous article:

The red mark in the figure is the Startup File of amoeba. It should be noted that amoeba is started using the batch processing method. In batch processing, classword is first found and classes are loaded. The first class to be loaded is the class above.

Next, open the amoebaproxyserver. Java file. I added comments to important statements. For details, see.

Public class amoebaproxyserver {Private Static logger log = logger. getlogger (amoebaproxyserver. class); Private Static logger repoterlog = logger. getlogger ("Report"); // defines two logger files. This is the usage of log4j, not the focus/** used to generate "state of server" reports. */protected static arraylist <reporter> reporters = new arraylist <reporter> ();/** the time at which the server was started. */protected static long serverstart Time = system. currenttimemillis ();/** the last time at which {@ link # generatereport} was run. */protected static long lastreportstamp = serverstarttime; public static void registerreporter (reporter) {Reporters. add (Reporter);}/*** generates a report for all system services registered as a * {@ link reporter }. */public static string generatereport () {return generatereport (system. currenttimem Illis (), false);}/*** generates and logs a "state of server" report. */protected static string generatereport (long now, Boolean reset) {long sincelast = now-lastreportstamp; long uptime = now-serverstarttime; stringbuilder report = new stringbuilder ("state of Server Report: "+ stringutil. line_separator); report. append ("-uptime :"). append (stringutil. intervaltostring (uptime )). append (stringutil. lin E_separator); report. append ("-report period :"). append (stringutil. intervaltostring (sincelast )). append (stringutil. line_separator); // Report on the State of memoryruntime RT = runtime. getruntime (); long Total = RT. totalmemory (), max = RT. maxmemory (); long used = (total-RT. freememory (); report. append ("-memory :"). append (used/1024 ). append ("K used,"); report. append (total/1024 ). append ("k total ,"); Report. append (max/1024 ). append ("K max "). append (stringutil. line_separator); For (int ii = 0; II <reporters. size (); II ++) {reporter rptr = reporters. get (II); try {rptr. appendreport (report, now, sincelast, reset, repoterlog. getlevel ();} catch (throwable t) {log. error ("reporter choked [rptr =" + rptr + "]. ", T) ;}}// only reset the last report time if this is a periodic reportif (reset) {lastreportstam P = now;} return report. tostring () ;}protected static void LogReport (string Report) {repoterlog.info (report) ;}// all operations related to log files are described above, you can modify these parameters as needed/*** @ Param ARGs * @ throws ioexception * @ throws exception * @ throws illegalaccessexception * @ throws instantiationexception * // start with main start to execute public static void main (string [] ARGs) throws exception {string level = system. getproperty ("benchmark. level "," War N "); system. setproperty ("benchmark. level ", level); If (ARGs. length> = 1) {shutdownclient client = new shutdownclient (monitorconstant. application_name);/*** application_name is a constant defined in monitorconstant. in the Java file, this file is in Amoeba/src/Java/COM/meidusa/amoeba/monitoer * shutdownclient is a monitoring class, its run function is responsible for obtaining network data packets and analyzing them to establish connections. * The shutdownclient object client calls the run function in the second if below. If the run function is successfully executed, the output amoeba server is running... this is why we can see that amoeba is displayed at startup. **/Monitorcommandpacket packet = new monitorcommandpacket (); If ("START ". equalsignorecase (ARGs [0]) {packet. funtype = monitorcommandpacket. fun_type_ping; If (client. run (packet) {/*** according to the previous comment, you can know the key and timely run function of this client. * Before analyzing the run function, pay attention to its parameter packet. The packet type is monitorcommandpacket. * You can see from monitorcommandpacket. Java that this class inherits the abstractpacket class. * The abstractpacket class is in src/amoeba/src/Java/NET/packet. The source code shows that this class is a derived class of the abstract class * packet. * After entering the run function of the client, go to the run function to continue the analysis */system. out. println ("amoeba server is running with Port =" + client. getport (); system. exit (-1) ;}} else {packet. funtype = monitorcommandpacket. fun_type_amoeba_shutdown; If (client. run (packet) {system. out. println ("amoeba server shutting down with Port =" + client. getport ();} else {system. out. println ("amoeba server not running with Port =" + client. getport ();} system. exit (0) ;}} else {System. Out. println ("amoeba START | stop"); system. Exit (0);} // After the above if block is executed, continue here. String log4jconf = system. getproperty ("log4j. conf "," $ {amoeba. home}/CONF/log4j. XML "); log4jconf = configutil. filter (log4jconf); file logconf = new file (log4jconf); If (logconf. exists () & logconf. isfile () {domconfigurator. configureandwatch (logconf. getabsolutepath (), system. getproperties ();} final logger = logger. getlogger (amoebaproxyserver. class); string Config = system. getproperty ("amoeba. conf "," $ {Amoeba. home}/CONF/amoeba. XML "); string contextclass = system. getproperty ("amoeba. context. class ", proxyruntimecontext. class. getname (); If (contextclass! = NULL) {proxyruntimecontext context = (proxyruntimecontext) class. forname (contextclass ). newinstance (); proxyruntimecontext. setinstance (context);} Config = configutil. filter (config); file configfile = new file (config); If (Config = NULL |! Configfile. exists () {logger. error ("cocould not find config file:" + configfile. getabsolutepath (); system. exit (-1);} else {proxyruntimecontext. getinstance (). init (configfile. getabsolutepath ();} registerreporter (proxyruntimecontext. getinstance (); For (connectionmanager connmgr: proxyruntimecontext. getinstance (). getconnectionmanagerlist (). values () {registerreporter (connmgr);} Map <string, Object> context = new hashmap <string, Object> (); context. putall (proxyruntimecontext. getinstance (). getconnectionmanagerlist (); List <beanobjectentityconfig> serviceconfiglist = proxyruntimecontext. getinstance (). getconfig (). getserviceconfiglist (); For (beanobjectentityconfig serverconfig: serviceconfiglist) {service = (service) serverconfig. createbeanobject (false, context); service. init (); service. start (); priorityshutdownhook. addshutdowner (service); registerreporter (service) ;}// the thread enters until now, and the new thread () {This. setdaemon (true); this. setname ("amoeba report thread");} public void run () {While (true) {try {thread. sleep (60*1000);} catch (interruptedexception e) {}try {LogReport (generatereport ();} catch (exception e) {logger. error ("report error", e );}}}}. start ();}}

The above code starts from the client. Run (packet) function and runs the run function in the shutdownclient. Java file. The run function snippets are described as follows:

Public Boolean run (monitorcommandpacket command) {// go to the main function of the amoebaproxyserver class and continue to execute it. // You Can See That amoeba is started normally (because no connection is established) if (Port <= 0) {socketinfofile = new file (configutil. filter ("$ {amoeba. home} "), appplicationname + ". shutdown. port "); If (! Socketinfofile. exists () {system. out. println ("B"); // Therefore, this output can be obtained. Then, false is returned, and return to the main function with return false;} // when a connection is established, the above if block is not executed, but the following block try {bufferedreader reader = new bufferedreader (New filereader (socketinfofile); string sport = reader. readline (); string TMP [] = stringutil. split (sport, ":"); If (TMP. length <= 1) {system. out. println ("C"); Return false;} This. port = integer. parseint (TMP [1]); this. host = TMP [0]; reader. close ();} catch (exception e) {e. printstacktrace (); system. out. println ("D"); Return false ;}}

During startup, the preceding run function returns false and returns to the main function. The following code is available based on the main code:

If (client. Run (packet) {/*** according to the previous comment, you can know the key and timely run function of this client. * Before analyzing the run function, pay attention to its parameter packet. The packet type is monitorcommandpacket. * You can see from monitorcommandpacket. Java that this class inherits the abstractpacket class. * The abstractpacket class is in src/amoeba/src/Java/NET/packet. The source code shows that this class is a derived class of the abstract class * packet. * After entering the run function of the client, go to the run function to continue the analysis */system. out. println ("amoeba server is running with Port =" + client. getport (); system. exit (-1 );}

Will jump out of this if, and then jump to the main function to execute the following code:

// After the above if block is executed, continue the execution here. String log4jconf = system. getproperty ("log4j. conf "," $ {amoeba. home}/CONF/log4j. XML "); log4jconf = configutil. filter (log4jconf); file logconf = new file (log4jconf); If (logconf. exists () & logconf. isfile () {domconfigurator. configureandwatch (logconf. getabsolutepath (), system. getproperties ();} final logger = logge

Finally, enter the while loop. the startup of amoeba is complete, and then the listener is continuously started.

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.