Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Play Framework hotswap with source code

2025-01-15 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 the source code to analyze Play Framework hotswap. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The selling point of Play Framework hotswap is hotswap, as it claims:

Reach your maximum productivity . Play! Allow developers to modify the java file, save it, and then refresh the browser, and you can see the effect immediately. No compilation is required, and there is no need to restart the server.

If Java wants to update class files dynamically, there are only two ways: replace classloader and replace JVM. Because it is more expensive to replace JVM, it is necessary to maintain the heap, stack and other running information of JVM, so hot swap usually chooses to replace classloader. For example, grails chooses to replace classloader, which maintains a thread of its own and periodically polls the source file for changes to replace the original classloader. So play! How did the claimed hot swap come true?

Let's take a look at play! Internal process of:

1. Play! I use Apache Mina as the underlying http server, and then use my own implementation of the Mina IoHandler interface-- HttpHandler

two。 When the browser initiates a request:

2.1 Mina Server generates a Mina Request and forwards it to the messageReceived method of HttpHandler

2.2 play! Parse Mina Request and Mina Session and wrap them into their own Request objects

Request request = parseRequest (minaRequest, session)

2.3 play! Detect the modification of the Route file and assign the information of Route/Action to the Request object according to the Route configuration information

Router.detectChanges (); Router.route (request)

2.4 play! Use different policies to call Action to manage Request according to the currently configured development mode

If (Play.mode = = Play.Mode.DEV) {Invoker.invokeInThread (new MinaInvocation (session, minaRequest, minaResponse, request, response);} else {Invoker.invoke (new MinaInvocation (session, minaRequest, minaResponse, request, response);}

2.5 if play! Currently in DEV mode, the invokeInThread method causes the invocation object to proxy the run () method

Public void run () {try {before (); execute (); after ();} catch (Throwable e) {onException (e);} finally {_ finally ();}}

Let's look at the before () method:

Public static void before () {Thread.currentThread (). SetContextClassLoader (Play.classloader); if (! Play.id.equals ("test")) {Play.detectChanges (); if (! Play.started) {Play.start ();}} / /}

In the detectChanges () method of the Play class, there is a sentence like this:

Classloader.detectChanges ()

Haha, play! After modifying the source file, this is the secret that refreshing the browser will work. Go in and take a look at play! Customize the detectChanges () method of classloader:

Public void detectChanges () {/ / Now check for file modification List modifieds = new ArrayList (); for (ApplicationClass applicationClass: Play.classes.all ()) {if (applicationClass.timestamp < applicationClass.javaFile.lastModified ()) {applicationClass.refresh (); modifieds.add (applicationClass);}} List newDefinitions = new ArrayList (); Map annotationsHashes = new HashMap (); for (ApplicationClass applicationClass: modifieds) {annotationsHashes.put (applicationClass.javaClass, computeAnnotationsHash (applicationClass.javaClass)) If (applicationClass.compile () = = null) {Play.classes.classes.remove (applicationClass.name);} else {applicationClass.enhance (); BytecodeCache.cacheBytecode (applicationClass.enhancedByteCode, applicationClass.name, applicationClass.javaSource); newDefinitions.add (new ClassDefinition (applicationClass.javaClass, applicationClass.enhancedByteCode));} try {HotswapAgent.reload (new ClassDefinition [newDefinitions.size ()]);} catch (ClassNotFoundException e) {throw new UnexpectedException (e) } catch (UnmodifiableClassException e) {throw new UnexpectedException (e);} / Check new annotations for (Class clazz: annotationsHashes.keySet ()) {if (annotationsHashes.get (clazz)! = computeAnnotationsHash (clazz)) {throw new RuntimeException ("Annotations change!");}} / / Now check if there is new classes or removed classes int hash = computePathHash (); if (hash! = this.pathHash) {/ / Remove class for deleted files! For (ApplicationClass applicationClass: Play.classes.all ()) {if (! applicationClass.javaFile.exists ()) {Play.classes.classes.remove (applicationClass.name);} if (applicationClass.name.contains ("$")) {Play.classes.classes.remove (applicationClass.name);}} throw new RuntimeException ("Path has changed");}}

The reload method of the HotswapAgent class is as follows:

Public static void reload (ClassDefinition definitions) throws UnmodifiableClassException, ClassNotFoundException {instrumentation.redefineClasses (definitions);}

After reading this, you will have a clear understanding of play! How to implement the principle of hot swap, or call the classes and methods in the java.lang.instrument directory to implement hot swap. There is no magic, play! You still choose to replace classloader, but the replacement occurs when dealing with http request, so the developer can use it to "refresh the browser and see the effect."

The above is the editor for you to share how to use the source code to analyze Play Framework hotswap, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report