In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use Play source code to analyze the Server startup process. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Play is a Rails-style Java Web framework.
See here on how to debug. Let's move on to the topic ^ _ ^
The Server startup process mainly involves three areas:
The play.Play class: represents the business model of Play itself.
Play.server.Server class: responsible for server startup.
Play.classloading package: responsible for reading, compiling, and loading .java files.
Overall process:
Server.main is the entry method:
Public static void main (String [] args) throws Exception {… Play.init (root, System.getProperty ("play.id", ")); if (System.getProperty (" precompile ") = = null) {new Server ();} else {Logger.info (" Done. ");}}
Do two things:
Play.init
Then create the Server object.
Play.init
Public static void init (File root, String id) {… ReadConfiguration (); Play.classes = new ApplicationClasses (); … / / Build basic java source path VirtualFile appRoot = VirtualFile.open (applicationPath); roots.add (appRoot); javaPath = new ArrayList (2); javaPath.add (appRoot.child ("app")); javaPath.add (appRoot.child ("conf")); / / Build basic templates path templatesPath = new ArrayList (2); templatesPath.add (appRoot.child ("app/views")) / / Main route file routes = appRoot.child ("conf/routes"); … / / Load modules loadModules (); … / / Enable a first classloader classloader = new ApplicationClassloader (); / / Plugins loadPlugins (); / / Done! If (mode = = Mode.PROD | | preCompile ()) {start ();} … }
Mainly do:
Load configuration
New ApplicationClasses (); load app, views and conf paths into VirtualFile. VirtualFile is a unified file access interface within Play to facilitate subsequent reading of files.
Load route
Load the extension components of the Plugin,Play framework itself.
If you work in product mode, start Play.
The key step is new ApplicationClasses (), which executes computeCodeHashe (), which triggers a directory scan to search for .java files. The related process simplifies the code as follows:
Public ApplicationClassloader () {super (ApplicationClassloader.class.getClassLoader ()); / / Clean the existing classes for (ApplicationClass applicationClass: Play.classes.all ()) {applicationClass.uncompile ();} pathHash = computePathHash (); … } int computePathHash () {StringBuffer buf = new StringBuffer (); for (VirtualFile virtualFile: Play.javaPath) {scan (buf, virtualFile);} return buf.toString () .hashCode () } void scan (StringBuffer buf, VirtualFile current) {if (! current.isDirectory ()) {if (current.getName (). EndsWith (".java")) {Matcher matcher = Pattern.compile ("\\ s+class\\ s ([a-zA-Z0-9 _] +)\\ s +) .matcher (current.contentAsString ()); buf.append (current.getName ()) Buf.append ("("); while (matcher.find ()) {buf.append (matcher.group (1)); buf.append (",");} buf.append (")") }} else if (! current.getName (). StartsWith (".")) {for (VirtualFile virtualFile: current.list ()) {scan (buf, virtualFile);}
Start process
The simplified code is as follows:
Public static synchronized void start () {try {/ / Reload configuration readConfiguration (); / / Try to load all classes Play.classloader.getAllClasses (); / / Routes Router.detectChanges (ctxPath) / / Cache Cache.init (); / / Plugins for (PlayPlugin plugin: plugins) {try {plugin.onApplicationStart () } catch (Exception e) {if (Play.mode.isProd ()) {Logger.error (e, "Can't start in PROD mode with errors");} if (e instanceof RuntimeException) {throw (RuntimeException) e } throw new UnexpectedException (e);}}. / / Plugins for (PlayPlugin plugin: plugins) {plugin.afterApplicationStart () } catch (PlayException e) {started = false; throw e;} catch (Exception e) {started = false; throw new UnexpectedException (e);}}
The key step is to perform Play.classloader.getAllClasses () to load the types in the app directory. The simplified code is as follows:
Public List getAllClasses () {if (allClasses = = null) {allClasses = new ArrayList (); if (Play.usePrecompiled) {...} else {List all = new ArrayList () / / Let's plugins play for (PlayPlugin plugin: Play.plugins) {plugin.compileAll (all);} for (VirtualFile virtualFile: Play.javaPath) {all.addAll (getAllClasses (virtualFile));} List classNames = new ArrayList () For (int I = 0; I < all.size (); iTunes +) {if (all.get (I)! = null & &! all.get (I) .added) {classNames.add (all.get (I) .name) }} Play.classes.compiler.compile (classNames.toArray (new String [classNames.size ()]); for (ApplicationClass applicationClass: Play.classes.all ()) {Class clazz = loadApplicationClass (applicationClass.name) If (clazz! = null) {allClasses.add (clazz);}.}} return allClasses;}
Main steps:
Plugin.compileAll, give all plugin a chance to do custom compilation.
Play.classes.compiler.compile (classNames.toArray (new String [classNames.size ()])); compile all .java files. The compiled .class is stored in ApplicationClass. Eclipse's JDT compiler is used internally.
LoadApplicationClass, take the .class from the ApplicationClass and add it to the List.
This completes the loading of .java. The related object relationships are shown in the following figure:
Then new Server () starts the HTTP service and listens for requests
The simplified code is as follows:
Public Server () {... If (httpPort = =-1 & & httpsPort = =-1) {httpPort = 9000;}. InetAddress address = null; try {if (p.getProperty ("http.address")! = null) {address = InetAddress.getByName (p.getProperty ("http.address"));} else if (System.getProperties (). ContainsKey ("http.address")) {address = InetAddress.getByName (System.getProperty ("http.address")) } catch (Exception e) {Logger.error (e, "Could not understand http.address"); System.exit (- 1);} ServerBootstrap bootstrap = new ServerBootstrap (new NioServerSocketChannelFactory (Executors.newCachedThreadPool (), Executors.newCachedThreadPool () Try {if (httpPort! =-1) {bootstrap.setPipelineFactory (new HttpServerPipelineFactory ()); bootstrap.bind (new InetSocketAddress (address, httpPort)); bootstrap.setOption ("child.tcpNoDelay", true) If (Play.mode = = Mode.DEV) {if (address = = null) {Logger.info ("Listening for HTTP on port% s (Waiting a first request to start)...", httpPort) } else {Logger.info ("Listening for HTTP at% 2$ SVA% 1$ s (Waiting a first request to start)...", httpPort, address) }} else {if (address = = null) {Logger.info ("Listening for HTTP on port% s...", httpPort);} else {Logger.info ("Listening for HTTP at% 2$ Spura% 1$ s...", httpPort, address) } catch (ChannelException e) {Logger.error ("Could not bind on port" + httpPort, e); System.exit (- 1);}.}
Main steps:
Set port, address
New ServerBootstrap, create the jboss netty server. Play1.1.1 uses netty as the underlying communication server.
New HttpServerPipelineFactory (), which sets up the request processing pipeline factory required by netty. It is responsible for providing the handler when the request arrives.
Bootstrap.bind (new InetSocketAddress (address, httpPort), binding address, port.
The above is the editor for you to share how to use the Play source code to analyze the Server startup process, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.