Hadoop source code learning notes (2) -- enter the main function to print package information

Source: Internet
Author: User

Hadoop source code learning notes (2)

-- Enter the main function to print the package information

After finding the main function and setting up a quick start method, let's take a look.

After entering the main functions of namenode and datanode, it is found that the form is similar:

  1. Public static void main (string ARGs []) {
  2. Try {
  3. Stringutils. startupshutdownmessage (datanode. Class, argS, log );
  4. Datanode = createdatanode (ARGs, null); // Replace the type in namenode with namenode
  5. If (datanode! = NULL)
  6. Datanode. Join ();
  7. } Catch (throwable e ){
  8. Log. Error (stringutils. stringifyexception (e ));
  9. System. Exit (-1 );
  10. }
  11. }

What is the first sentence after we come in? When we run it here, we find:

When running every program, this section is output in the console or log file. The information mainly describes the class, host, parameter, version, and compilation information started.

The trail enters the stringutils. startupshutdownmessage class,

  1. Public static void startupshutdownmessage (class <?> Clazz, string [] ARGs,
  2. Final org. Apache. commons. Logging. log ){
  3. Final string hostname = gethostname ();
  4. Final string classname = clazz. getsimplename ();
  5. Log.info (
  6. Tostartupshutdownstring ("startup_msg:", new string [] {
  7. "Starting" + classname,
  8. "Host =" + hostname,
  9. "ARGs =" + arrays. aslist (ARGs ),
  10. "Version =" + versioninfo. getversion (),
  11. "Build =" + versioninfo. geturl () + "-R"
  12. + Versioninfo. getrevision ()
  13. + "; Compiled by '" + versioninfo. getuser ()
  14. + "'On" + versioninfo. getdate ()}
  15. )
  16. );
  17.  
  18. Runtime. getruntime (). addshutdownhook (New thread (){
  19. Public void run (){
  20. Log.info (tostartupshutdownstring ("shutdown_msg:", new string [] {
  21. "Shutting down" + classname + "at" + hostname }));
  22. }
  23. });
  24. }

In row 3, The log4j component is used for output (here, log4j is a log Writing component, which is log4net in. NET and is not described in detail ). Then there are no problems with the first several categories (classname), host (hostname), and parameter (ARGs). You can use the corresponding function to obtain them.

For other information, let's take a look at the first line. Here we add a handler to the process to capture the Close event. Then, when it is closed, we can output the information. Let's try again, when running on the console and pressing CTRL + C to terminate, you can see the following information:

This section of information is exactly the output of the 20th lines of code.

 

Other information such as version and compilation information? The input content is relatively fine. How does it know? Follow up to see:

  1. Private Static hadoopversionannotation version;
  2. Static {
  3. Mypackage = hadoopversionannotation. Class. getpackage ();
  4. Version = mypackage. getannotation (hadoopversionannotation. Class );
  5. }
  6. Public static string getuser (){
  7. Return version! = NULL? Version. User (): "unknown ";
  8. }

This code finds that getuser is a user () function through a hadoopversionannotation class, and this hadoopversionannotation finds an interface:

  1. @ Retention (retentionpolicy. runtime)
  2. @ Target (elementtype. Package)
  3. Public @ interface hadoopversionannotation {...}

This interface is a special interface, and there is a special mark in line 1 and 2. We know that some functions in Java can add a special mark, such as @ override. This override is a special mark, and it is also an interface that can be attached to this function for special significance.

This hadoopversionannotation is an interface that is attached to a package to obtain package information.

Where is the attachment in the package? We found the following in the build Folder:

It is found that there is a Java file, which defines the package and adds @ information to the package. This is the definition of this interface. At the same time, the version, editor, and, compile time and other information. So we can get the value here in the program.

But I thought again, isn't this build folder Not Available at the beginning? Is it created only after ant is run? Where does this Java file come from?

We found that there is a saveversion. Sh file in the SRC Folder:

It can be seen that it is writing relevant information to the package-info.java.

This saveversion. Sh can be traced and called in build. xml.

This @ xxx is actually a feature (attribute) in. net ).

 

Okay, the first cut in the main function is gone.

 

 

Hadoop source code learning notes (2) -- enter the main function to print package information

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.