JAVA程序变更自动重载而不重启服务之JAVAREBEL

 JavaRebel给Java带来了动态重新装载类特性。虽然现在还有些局限性,但它可以显著地增加开发速度。JavaRebel允许一个应用程序(独立的或运行在应用服务器上的应用)重新装载在运行过程中发生的大多数类变化,包括增加或删除方法和域。节省了很多工作量。

Javarebel是一个Jvm插件,使用非常简单,只需要两步:

  • 将javarebel.jar加入classpath(不建议将javarebel.jar放在WEB-INF/lib下,因为生产环境不需要它)
  • 命令行中加入下面的启动参数
    java -noverify -javaagent:C:/libraries/javarebel.jar -Drebel.dirs=c:/workspace/project/classes
    Drebel.dirs指向工程中.class文件的目录,不必指定具体的类名。另外,javarebel.jar的名字不能修改。

具体内容可以参考其官方网站: http://www.zeroturnaround.com/javarebel/

Installation

  • Using JavaRebel
  • Installation
    • Java 5 or later
    • Java 1.4
    • IDE configuration
    • Instructions per container
  • Advanced configuration
  • JavaRebel plugins

Using JavaRebel

JavaRebel is a developer tool that will reload changes to compiled Java classes on-the-fly saving the time that it takes to redeploy an application or perform a container restart. It is a generic solution that works for Java EE and Java standalone applications.

JavaRebel installs as a JVM plugin (-javaagent) and works by monitoring the timestamp of class files. When it is updated (e.g. when a developer saves a class from the IDE) JavaRebel will reload the changes to class code and structure while preserving all existing class instances. The loading is lazy and will happen upon usage of the class (method call on an instance, static call on the class, field lookup etc).

The following classes will be reloaded when they are changed and compiled:

  1. All ".class" classes inside the usual classpath (WEB-INF/classes, etc). Using this with exploded deployment will provide the best JavaRebel experience.
  2. All ".class" files in directories specified by -Drebel.dirs (comma-separated list) JVM command line property. With this you can deploy in unexploded development mode (EAR or WAR) and still reload classes instantly. For example -Drebel.dirs=/path/to/eclipse/project-one/bin,/path/to/eclipse/project-two/bin. However new classes (or renamed old classes) will not be loaded before they also appear in the classpath (e.g. after the build in JAR files).

Example: Java EE exploded development

You are developing a Java EE application. You are using exploded development and have deployed the root of your project to the container. The container is loading your classes from WEB-INF/classes. You have installed JavaRebel to the container.

To take advantage of this situation with JavaRebel the developer has to start compiling the class files straight to WEB-INF/classes and instead of redeploying the application JavaRebel will reload code changes.

Example: Java EE unexploded development (WAR or EAR)

You are developing a Java EE application. You are using unexploded development and have deployed a WAR archive to the container. You would like to change Java source files and see the changes propagate without redeploy. You are compiling your classes to c:/projects/app/build/classes.

To take advantage of this situation with JavaRebel the developer has to start the container with JavaRebel and also specify the location of the classes, thus -Drebel.dirs=c:/projects/app/build/classes. JavaRebel will now reload new classes that are compiled to c:/projects/app/build/classes.

Example: Java SE development

You are developing a Java SE Swing application. You are compiling and starting it from an IDE with all the compiled classes being in system classpath. In this case adding the JavaRebel installation command line to the started application will immediately enable class reloading.

More examples:

  • Developing in Exploded Format
  • Developing in Unexploded Format
  • JavaRebel Tutorial for Tomcat on Windows
  • Developing Swing with JavaRebel
  • JavaRebel and IntelliJ IDEA
  • JavaRebel Boosting Eclipse Plugin Development

Installation

Java 5 or later

Add the following to JVM command line (note that it is important that the JAR would be named "javarebel.jar")

-noverify -javaagent:/path/to/javarebel.jar

Windows examples

  • java -noverify -javaagent:C:/libraries/javarebel.jar -Drebel.dirs=c:/workspace/project/classes com.domain.Application
  • java -noverify -javaagent:C:/libraries/javarebel.jar -Drebel.dirs=c:/workspace/project/classes -jar application.jar

Linux examples

  • java -noverify -javaagent:/home/john/libs/javarebel.jar -Drebel.dirs=/home/john/workspace/project/classes com.domain.Application
  • java -noverify -javaagent:/home/john/libs/javarebel.jar -Drebel.dirs=/home/john/workspace/project/classes -jar application.jar

Java 1.4

The installation for Java 1.4 has two parts:

  • Generating a bootstrap jar
  • Adding commandline options to the JVM

Generating the bootstrap jar

Running java -jar /path/to/javarebel.jar will generate a javarebel-bootstrap.jar. The file is generated to the folder where javarebel.jar resides.

IMPORTANT! javarebel-bootstrap.jar is JVM and JavaRebel version specific! If you upgrade your JVM (even a minor version) or upgrade JavaRebel you have to regenerate javarebel-bootstrap.jar.

IMPORTANT! Make sure that the JVM version used to generate javarebel-bootstrap.jar is the same your application will run with!

Example

  • java -jar C:/libraries/javarebel.jar - will generate a javarebel-bootstrap.jar into the folder C:/libraries/
  • java -jar /home/john/lib - will generate a javarebel-bootstrap.jar into the folder /home/john/lib

NOTE The easiest way to ensure JVM and JavaRebel version consistency is to add the javarebel-bootstrap.jar generation to the startup script of your application server. This means that on every startup the javarebel-bootstrap.jar is generated with the JVM that is used to start the server. This is an inexpensive invocation and will not be noticeable.

Adding commandline options to the JVM

JavaRebel requires the javarebel-bootstrap.jar and javarebel.jar to be prepended to the Java bootclasspath as well as the -noverify JVM flag:

-noverify -Xbootclasspath/p:/path/to/javarebel-bootstrap.jar:/path/to/javarebel.jar

Windows examples

  • java -noverify -Xbootclasspath/p:C:/libraries/javarebel-bootstrap.jar;C:/libraries/javarebel.jar -Drebel.dirs=c:/workspace/project/classes com.domain.Application
  • java -noverify -Xbootclasspath/p:C:/libraries/javarebel-bootstrap.jar;C:/libraries/javarebel.jar -jar -Drebel.dirs=c:/workspace/project/classes application.jar

Linux examples

  • java -noverify -Xbootclasspath/p:/home/john/libs/javarebel-bootstrap.jar:/home/john/libs/javarebel.jar -Drebel.dirs=/home/john/workspace/project/classes com.domain.Application
  • java -noverify -Xbootclasspath/p:/home/john/libs/javarebel-bootstrap.jar:/home/john/libs/javarebel.jar -Drebel.dirs=/home/john/workspace/project/classes -jar application.jar

Successful installation check

On successful installation you should see the following message in standard output.

##########################################################
                                                        
 ZeroTurnaround JavaRebel x.y.z                         
 ...                                                    
##########################################################

See Features for supported JVM and server versions.

IDE configuration

If you are using IntelliJ IDEA you should install this plugin to enable debugging with JavaRebel. You should also configure your IDE for better debugging experience.

Instructions per container

Further you can find specific instructions for different operating systems, application servers and so on. To keep the instructions concise we don't include the -Drebel.dirs option in the following installation strings.

你可能感兴趣的:(java,application,jvm,class,debugging,swing)