动态装罐 [英] Dynamic loading and unloading of jar

查看:88
本文介绍了动态装罐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于Java的(jersey + grizzly)REST Server,它公开了像
foo.com/{game}/规则
foo.com/{game}/players
foo.com/{game}/比赛

We have a java based (jersey+grizzly) REST Server which exposes calls like
foo.com/{game}/rules
foo.com/{game}/players
foo.com/{game}/matches


可以有任意数量的游戏
每个游戏的规则,玩家,比赛都有不同的实现方式
出于历史原因,我们希望每个游戏实施都使用单独的jars


There can be arbitrary number of games
Each game has different implementations for rules, players, matches
For historical reasons, we would want separate jars for each game implementation


因此有REST Server
当有类似foo.com/tennis/rules之类的电话时
我们希望REST服务器动态加载网球"罐.广口瓶进行操作.然后应将罐子卸下
如果下一个电话是foo.com/football/players 我们希望REST服务器动态加载足球"罐子.广口瓶进行操作.然后应该将罐子卸载


So there is REST Server
as and when there is a call like foo.com/tennis/rules
we want the REST Server to dynamically load 'tennis' jar. The jar does its operation. Then jar should be unloaded
If the next call was for foo.com/football/players we want the REST Server to dynamically load 'football' jar. The jar does its operation. Then jar should be unloaded


有没有一种技术可以做到这一点?
显然,[this]周围有一个非常老的问题:


Is there a technique to do this ?
Apparently there is a very old question around [this]: java: is there a framework that allows dynamically loading and unloading of jars (but not osgi)?

推荐答案

代理对象 在隔离的类加载器中动态加载Java jar文件,以避免依赖冲突并启用模块化更新. [主jar文件夹]/lib/myLib/2.0/*.jar中的所有jar文件都将被加载.

Load java jar files dynamically in isolated class loader to avoid dependency conflicts and enable modular updates. All jar files in the [main jar folder]/lib/myLib/2.0/*.jar will be loaded.

代码示例

从位于myLib/2.0文件夹中的JAR文件创建对象:

Create Object from a JAR file located in the myLib/2.0 folder:

    File libDir = new File("myLib/2.0");

    ProxyCallerInterface caller = ObjectBuilder.builder()
            .setClassName("net.proxy.lib.test.LibClass")
            .setArtifact(DirArtifact.builder()
                    .withClazz(ObjectBuilderTest.class)
                    .withVersionInfo(newVersionInfo(libDir))
                    .build())
            .build();
    String version = caller.call("getLibVersion").asString();

这篇关于动态装罐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆