It ninja turtles use jreloader to dynamically reload changed classes without restarting JVM

Source: Internet
Author: User

In tomcat, you can configure reloadable = "true" to change the class and then reload tomcat. In fact, this process also means that when Tomcat finds a changed class, it will restart a new application to reload all classes to serve new requests, you do not need to manually execute shutdown. sh (. BAT), and then startup. sh (. BAT ). This method is applicable to the ancient JSP program, because the web. in XML, there is almost nothing to start with the application and it takes a long time to Code. However, the current framework is rampant, Web. the degree to which XML is started along with the application is scrambling, so relying only on reloadable = "true" cannot meet the requirements. Every time you change a class (whether it is modifying the code in the method body or changing the structure of the class, it is accurate to say that any file in the WEB-INF/classes directory) You may be in

Jan 28,201 1 7:19:42 org. Apache. Catalina. Core. applicationcontext log
Info: initializing spring root webapplicationcontext

This step is later than half a day. Besides, the changes to the reloadable = "true" class structure are non-dynamic. For example, different interfaces are implemented and different classes are inherited, or, when adding or deleting methods or properties, Tomcat must be shut down, but then startup.

Frequent restart is hard to debug, so we hope to find a tool that does not need to restart the current application when the class is changed. Of course, we do not want to restart tomcat. Tools that can implement this function include javarebel and jreloader. The former is charged, and the latter is open-source, but the latter is actually good.

This section describes how to configure and use the latter in the eclipse WTP Tomcat environment.

The current version is a jreloader-0.4.zip with jreloader. jar and jreloader-src.zip, which is then a very simple and adequate install.txt file:

0102030405060708091011 You need only to add a couple of vm arguments, as in the example below: java -noverify -javaagent:c:\tools\jreloader.jar -Djreloader.dirs=c:\project\target\classes com.foo.Main or java -noverify -javaagent:c:\tools\jreloader.jar -Djreloader.dirs=c:\project\target\classes -jar foo.jar If you have multiple modules, you can add more than one class dir:  -Djreloader.dirs=c:\project\target\classes,c:\project2\target\classes,c:\project2\target\classes

Like javarebel, Java agent is used for control. It uses-djreloader. dirs to monitor the classpath that may change. Multiple classpath can be separated by commas. For web programs we only need to monitor the/WEB-INF/classes directory. In fact, no matter what the program is, we only need to find a way to set the-javaagent parameter. You can also change it to Catalina. BAT (. Sh) by yourself.

Now let's talk about how to set the above JVM parameters for the created Tomcat server in eclipse WTP. Go to the tomcat configuration overview page in eclipse and click Open launch configuration to bring up the edit Configuration window, go to the arguments tab, and add VM arguments to the input box:

-Noverify-javaagent: D :\ jreloader-0.4 \ jreloader. jar-djreloader. dirs = D: \ workspaces \ J2EE \. metadata \. plugins \ Org. eclipse. WST. server. core \ tmp0 \ wtpwebapps \ testjavaweb \ WEB-INF \ Classes

Note: I used jreloader. jar is placed in the D: \ jreloader-0.4 directory, WTP plug-ins usually publish applications in D: \ workspaces \ J2EE \. metadata \. plugins \ Org. eclipse. WST. server. in a location like core \ tmp0 \ wtpwebapps \ testjavaweb, you only need djreloader. dirs points to the WEB-INF/classes subdirectory in the directory after the application is published. Every time you change a class, WTP will be published to this classes directory, so you need to pay attention to the place where classes are loaded during actual running.

In addition to the VM arguments above, you must set the reloadable of the application in Tomcat to false to prevent Tomcat from managing the application. Instead, jreloader is responsible for hot loading of classes.

Eclispe WTP's tomcat configuration will reflect D: \ workspaces \ J2EE \. metadata \. plugins \ Org. eclipse. WST. server. core \ tmp0 \ conf \ Server. XML, so directly modify the server. XML. If you change reloadable to false, it will still be overwritten to true.

However, we still have a way to create the context. xml file in the project's META-INF directory and write:

<Context antiresourcelocking = "false" privileged = "true" reloadable = "false"/>

The configuration above will be merged into D: \ workspaces \ J2EE \ by WTP \. metadata \. plugins \ Org. eclipse. WST. server. core \ tmp0 \ conf \ Server. in the XML file.

Now you can try to start Tomcat. First, you can see:

+ --------------------------------------------------- +
| Jreloadagent 0.4 |
| Copyright 2011-2009 Antonio S. R. Gomes |
| See LICENSE-2.0.txt details |
+ --------------------------------------------------- +

If there is a class change, the console will see:

D: \ workspaces \ J2EE \. metadata \. plugins \ Org. eclipse. WST. server. core \ tmp0 \ wtpwebapps \ footnotedpro \ WEB-INF \ Classes \ CC \ unmi \ struts \ model \ elementitem. class
[Jreloader: info] reloading class cc. unmi. strtus. model. elementitem

Now you can see the effect in your browser. You no longer need to read tomcat or other frameworks over and over again (think about the face of other countries that have been pulled to endure China's national conditions over and over again in the Asian Games:

Jan 28,201 1 7:19:42 org. Apache. Catalina. Core. applicationcontext log
Info: initializing spring root webapplicationcontext

There should be nothing to say about the independent application to be started. Add the startup parameter-noverify-javaagent: C: \ tools \ jreloader. jar-djreloader. dirs = c: \ project \ target \ classes. This is useful for long-time service programs or daemon programs.

Compared with javarebel, jreloader does not write many articles on bytecode In the debugging window. The class name is still the original class name, breakpoint debugging is also convenient.

It ninja turtles use jreloader to dynamically reload changed classes without restarting JVM

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.