动态加载和卸载Java类

在开发Java服务器应用时,我们最希望开发的应用能够支持热部署,即不需要重启系统就可以用新的应用替换旧的应用。
如果使用动态语言,这些功能比较容易实现。但Java是静态语言,是否也可实现动态热部署呢?

首先,我们要深入了解一下Java的类装载(Class Loading)机制,和垃圾回收(Garbage Collection)机制。其中class loading 将负责装载新的应用包;GC将负责卸载旧的应用包。
装载新应用包的方法比较简单,只需要定制一个ClassLoader,从指定路径装载.jar文件即可。
要卸载一个ClassLoader,则必须要同时卸载通过该ClassLoader 载入的类的所有实例, 简单将ClassLoader的引用置为null并希望GC回收的做法是无效的。然而,要想统计并记录所有该ClassLoader载入的类实例是不现实的。而且,应用的装载和卸载功能,是服务器Framework的一部分,而不是应用业务功能的一部分。因此,framework也无法知道业务应用中何时载入和创建了多少对象。

解决方法还是有的。因为GC回收Heap中那些unreachable的对象,追根溯源,所有对象的创建都是由活动线程中发起的, 即所谓的Root set of references. 因此一旦活动线程结束运行,则可以说,线程中所有对象的根引用不可用了,则由根应用创建的所有对象也变为unreachable.

因此可以做如下设计:
[img]http://oursleepless.iteye.com/upload/picture/pic/94103/b64fa3b4-4cbe-34c0-bd7f-f344c02a17b5.png[/img]

其中Server负责service的deploy和undeploy。
其中deploy的过程可简单描述为:创建一个daemon线程,设置其context classloader为Service Jar包的ClassLoader,起动该Daemon线程。
undeploy的过程可简单描述为:停止服务(service daemon thread),设置线程context classloader为null, 等带线程彻底结束后手动执行GC来回收对象和ClassLoader.

Service接口如下:

public interface Service {

public final static String ATTR_SERVICE_ID = "SERVICE-ID";
public final static String ATTR_SERVICE_CLASS = "SERVICE-CLASS";

public void install() throws ServiceException;

public void start() throws ServiceException;

public void stop() throws ServiceException;

public void uninstall() throws ServiceException;

public String getId();
}


服务管理器如下:

/**
*
* @author less
* Responsible for deploy and undeploy Services.
*/
public class ServiceManager {

private final static HashMap<String, ServiceThread> installedServices = new HashMap<String, ServiceThread>();

public final synchronized static String install(File jarService) throws Exception {
MyJarLoader loader = new MyJarLoader(jarService, MyJarLoader.class.getClassLoader());
Manifest manifest = loader.getManifest();
Attributes attrs = manifest.getAttributes("Service");
String svcId = attrs.getValue(Service.ATTR_SERVICE_ID);
if (installedServices.containsKey(svcId)) {
uninstall(svcId);
}
ServiceThread t = new ServiceThread();
t.setContextClassLoader(loader);
t.setDaemon(true);
t.start();
loader = null;
return svcId;
}

public final static void uninstall(File jarService) throws Exception {
MyJarLoader loader = new MyJarLoader(jarService, MyJarLoader.class.getClassLoader());
Manifest manifest = loader.getManifest();
Attributes attrs = manifest.getAttributes("Service");
String svcId = attrs.getValue(Service.ATTR_SERVICE_ID);
loader = null;
uninstall(svcId);
}

public final static void uninstall(String svcId) throws Exception {
ServiceThread t = installedServices.get(svcId);
if (t.getStatus() == ServiceThread.Status.RUNNING) {
t.stopService();
}
ServiceManager.getInstalledServices().remove(svcId);
t.destroyService();
t.setContextClassLoader(null);
while (t.isAlive()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
t = null;
System.gc();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
System.gc();
}

static HashMap<String, ServiceThread> getInstalledServices() {
return installedServices;
}

}

/**
*
* @author less
* A Customer Service carrier.
*/
class ServiceThread extends Thread {

public static enum Status {
RUNNING, STOPPED
}

private Status status = Status.STOPPED;
private Service service = null;

public Status getStatus() {
return this.status;
}

@Override
public void run() {
try {
Manifest manifest = ((MyJarLoader) getContextClassLoader()).getManifest();
Attributes attrs = manifest.getAttributes("Service");
String svcId = attrs.getValue(Service.ATTR_SERVICE_ID);
this.setName(svcId);
String svcClass = attrs.getValue(Service.ATTR_SERVICE_CLASS);
Class<Service> c = (Class<Service>) getContextClassLoader().loadClass(svcClass);
service = c.newInstance();
c = null;
service.install();
ServiceManager.getInstalledServices().put(svcId, this);
startService();
stopService();
} catch (Exception e) {
e.printStackTrace();
}
}

public void stopService() {
if (this.status != Status.STOPPED) {
try {
service.stop();
this.status = Status.STOPPED;
} catch (Exception e) {
e.printStackTrace();
this.status = Status.RUNNING;
}
}
}

public void startService() {
if (this.status == Status.STOPPED) {
this.status = Status.RUNNING;
try {
service.start();
} catch (Exception e) {
e.printStackTrace();
this.status = Status.STOPPED;
}
}
}

public void destroyService() {
try {
service.uninstall();
service = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
*
* @author less
*
*/
class MyJarLoader extends JarLoader {

public MyJarLoader(File file, ClassLoader parent) throws Exception {
super(file, parent);
System.out.println(this + " is created.");
}

@Override
protected void finalize() throws Throwable {
destroy();
System.out.println(this + " is finalized.");
super.finalize();
}
}



示例服务代码,测试修改编译后重新部署:

/**
* @author less
*
*/
public class ExampleService1 implements Service {
private boolean bRun = true;

@Override
public void install() throws ServiceException {
System.out.println("== " + this + " is installed.");
}

@Override
public void start() throws ServiceException {
System.out.println("== " + this + " is started.");

//int i = 10000;
int i=0;
while (bRun) {
//System.out.println(this + " " + i--);
System.out.println(this + " " + i++);
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
}
}
System.out.println("== " + this + " is stopped.");
}

@Override
public void stop() throws ServiceException {
System.out.println("== Trying to stop " + this);
bRun = false;
Thread.currentThread().interrupt();
}

@Override
public void uninstall() throws ServiceException {
System.out.println("== " + this + " is uninstalled.");
}

@Override
public String getId() {
// TODO Auto-generated method stub
return null;
}

@Override
protected void finalize() throws Throwable {
System.out.println(this + " - is finalized.");
super.finalize();
}
}


控制台输出如下:
Server started.
org.lucky.server.MyJarLoader@27b15692 is created.
== org.lucky.service.example.ExampleService1@4e76fba0 is installed.
== org.lucky.service.example.ExampleService1@4e76fba0 is started.
org.lucky.service.example.ExampleService1@4e76fba0 10000
org.lucky.service.example.ExampleService1@4e76fba0 9999
org.lucky.service.example.ExampleService1@4e76fba0 9998
org.lucky.service.example.ExampleService1@4e76fba0 9997
org.lucky.server.MyJarLoader@2f833eca is created.
== Trying to stop org.lucky.service.example.ExampleService1@4e76fba0
== org.lucky.service.example.ExampleService1@4e76fba0 is uninstalled.
== org.lucky.service.example.ExampleService1@4e76fba0 is stopped.
org.lucky.service.example.ExampleService1@4e76fba0 - is finalized.
org.lucky.server.MyJarLoader@27b15692 is finalized.
== org.lucky.service.example.ExampleService1@7b36a43c is installed.
== org.lucky.service.example.ExampleService1@7b36a43c is started.
org.lucky.service.example.ExampleService1@7b36a43c 0
org.lucky.service.example.ExampleService1@7b36a43c 1
org.lucky.service.example.ExampleService1@7b36a43c 2
org.lucky.service.example.ExampleService1@7b36a43c 3

由上述测试可见,通过这种方式,可以实现类动态加载和卸载以及热部署。

这个方法为Java类的动态加载/卸载提供了一个思路。由于它需要为每一个需要部署的服务起动一个线程,虽然该线程只负责装载和卸载服务,服务运行时并不消耗CPU,但会固定占用一定大小的内存。测试值为每线程约占用350k内存。因此服务多时,存在StackOverflowError的风险。

在JDK7中据说提供了anonymous classloading 机制来支持动态类加载/卸载。需要使用的同学可以关注一下。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SPI(Service Provider Interface)是Java提供的一种服务发现机制,它允许第三方服务提供者在不需要修改代码的情况下,向程序动态地添加服务实现。 在Spring Boot中,SPI可以用来加载卸载插件。Spring Boot提供了一个自动配置`SpringFactoriesLoader`,它可以自动加载classpath下的`META-INF/spring.factories`文件,并根据其中定义的`EnableAutoConfiguration`进行自动配置。 要实现SPI的加载卸载,可以按照以下步骤进行: 1. 定义接口和接口实现 定义一个接口,例如: ```java public interface Plugin { void doSomething(); } ``` 定义一个或多个实现,例如: ```java public class PluginA implements Plugin { @Override public void doSomething() { System.out.println("Plugin A is doing something."); } } public class PluginB implements Plugin { @Override public void doSomething() { System.out.println("Plugin B is doing something."); } } ``` 2. 创建`META-INF/services`目录 在classpath下创建`META-INF/services`目录。 3. 创建服务提供者配置文件 在`META-INF/services`目录下创建一个以接口全限定名为文件名的文件,例如: ``` com.example.Plugin ``` 在文件中写入实现的全限定名,例如: ``` com.example.PluginA com.example.PluginB ``` 4. 加载插件 使用`ServiceLoader`加载插件,例如: ```java ServiceLoader<Plugin> plugins = ServiceLoader.load(Plugin.class); for (Plugin plugin : plugins) { plugin.doSomething(); } ``` 5. 卸载插件 可以将实现从服务提供者配置文件中删除,再重新加载插件即可完成卸载。或者在程序中对实现进行过滤,例如: ```java ServiceLoader<Plugin> plugins = ServiceLoader.load(Plugin.class); for (Plugin plugin : plugins) { if (plugin instanceof PluginA) { continue; } plugin.doSomething(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值