标签:jvm 热替换 class loader dcevm 热部署
DCEVM,全称Dynamic Code Evolution,一个JVM补丁程序,其目的就是实现运行时的类替换。最新的项目地址为:https://dcevm.github.io/
下面将从安装、验证、搭建测试环境、测试等几个方面进行简要说明。
首先登录网址:https://dcevm.github.io/,站点中明确给出了Binaries(二进制程序)的下载链接,该项目分为两个版本的发布程序,一个是light版本,另一个是full,区别在于full版本支持更多的特性,但对于一般的开发来说,light版本足矣,更多内容详情请参考原文。
二进制的文件下载后能够从中解压出一个带installer的jar文件,这个就是用于安装补丁文件的安装程序,运行方式如下:
java -jar installer.jar如在Linux/Unix环境下运行,请命令最前方加sudo。程序运行后,会出现如下的安装画面:
接下来通过点击按钮“Add installation directory”选择要打上补丁的JAVA_HOME目录,选择完成后,分别点击“Replace by DCEVM”和“Install DCEVM as altjvm”来为目标JDK打上DCEVM补丁。
安装完成后的效果,应该与上图类似,接下来我们可以通过控制台来进行安装验证。打开控制台终端程序,然后键入以下命令:
java -version
如果安装无误的话,则会出现以下信息,重点是最后一句话“Dynamic Code Evolution”。
java version "1.7.0_71" Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Dynamic Code Evolution 64-Bit Server VM (build 24.71-b01-dcevmlight-2, mixed mode)
接下来我们可以通过一个具体的实验来测试动态加载的效果,在测试前,先需要准备好相应的测试环境。
到http://tomcat.apache.org去下载一个Tomcat服务器程序,这里选择的版本是7。解压后在其主目录的webapps下创建一个DCEVM目录,然后按照以下结构创建一个Web应用:
DCEVM/
├── META-INF
└── WEB-INF
└── lib
/Users/i318043/z/workspace/eclipse/ExperimentDCEVM
├── WebContent
│ └── WEB-INF
│ └── classes
│ └── com
│ └── github
│ └── dcevm
│ └── test
└── src
└── com
└── github
└── dcevm
└── test
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ExperimentDCEVM</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>auto,full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value><project>/.externalToolBuilders/ExperimentDCEVM build.xml [Builder].launch</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
<linkedResources>
<link>
<name>WebContent</name>
<type>2</type>
<location>/Users/Woody/z/apache-tomcat-7.0.61/webapps/DCEVM</location>
</link>
</linkedResources>
</projectDescription>
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.xhtml</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>welcome.jsp</welcome-file> </welcome-file-list> </web-app>welcome.jsp
<html> <head></head> <body> <h1>hi there!</h1> <jsp:useBean id="hwBean" class="com.github.dcevm.test.HelloWorld"></jsp:useBean> <jsp:getProperty property="greetings" name="hwBean"/> <jsp:useBean id="ncBean" class="com.github.dcevm.test.NewClass"></jsp:useBean> <jsp:getProperty property="word" name="ncBean"/> </body> </html>HelloWorld.java
package com.github.dcevm.test; public class HelloWorld /*4.2*/ extends AbstractHelloWorld { public String getGreetings() { /*5.0, 5.2*/ System.out.println("... ||x|| ..."); /*1*/ return "Hello tricky?".concat(" ").concat(_getGreeting1()).concat(" ").concat(_getGreeting2()).concat(getAbstractGreeting()); } /*2.1*/ private String _getGreeting1() { return "damn it!"; } /*2.2*/ private String _getGreeting2() { return "Why nobody mentions this trap!"; } } /*4.1*/ abstract class AbstractHelloWorld { protected String getAbstractGreeting() { return "<abstract Y=good=Y greeting>"; } } /*5.1*/ // ==> redefine auto-build approachNewClass.java
package com.github.dcevm.test; public class NewClass { public String getWord() { return "hmm ..."; } }
源码中添加了Ant编译,通过鼠标右键点击项目,选择“Properties”,进入“Builder”,勾选掉“ExperimentDCEVM build.xml [Builder]”可以禁用Ant编译。PS:Ant编译主要针对使用eclipse的Java Builder不太适合的项目。
http://download.csdn.net/detail/rcom10002/8612013
标签:jvm 热替换 class loader dcevm 热部署
原文地址:http://blog.csdn.net/rcom10002/article/details/45146189