gpt4 book ai didi

Java 9 ServiceLoader 运行时模块加载和替换

转载 作者:搜寻专家 更新时间:2023-11-01 00:54:22 27 4
gpt4 key购买 nike

我刚读到有关 Java 9 模块系统的信息,我想问一下 ServiceLoader .当应用程序已经启动时,有什么方法可以添加服务实现吗?删除一些服务实现怎么样?

用例:我将有一些计算某些东西的应用程序。计算算法将在某些服务(Java 9 模块)中定义。是否有任何步骤可以在不停止应用程序的情况下替换此算法?我什么时候更换 jars 只是因为计算会失败,我需要捕获错误并在模块加载完成后重新启动?

是否有任何其他项目可以支持此类用例?

最佳答案

假设 BankController 是您的服务。如果你想从动态路径动态加载它的实现,创建一个新的模块层并加载实现。

private final BankController loadController(final BankConfig config) {
System.out.println("Loading bank with config : " + JSON.toJson(config));
try {
//Curent ModuleLayer is usually boot layer. but it can be different if you are using multiple layers
ModuleLayer currentModuleLayer = this.getClass().getModule().getLayer(); //ModuleLayer.boot();
final Set<Path> modulePathSet = Set.of(new File("path of implementation").toPath());
//ModuleFinder to find modules
final ModuleFinder moduleFinder = ModuleFinder.of(modulePathSet.toArray(new Path[0]));
//I really dont know why does it requires empty finder.
final ModuleFinder emptyFinder = ModuleFinder.of(new Path[0]);
//ModuleNames to be loaded
final Set<String> moduleNames = moduleFinder.findAll().stream().map(moduleRef -> moduleRef.descriptor().name()).collect(Collectors.toSet());
// Unless you want to use URLClassloader for tomcat like situation, use Current Class Loader
final ClassLoader loader = this.getClass().getClassLoader();
//Derive new configuration from current module layer configuration
final Configuration configuration = currentModuleLayer.configuration().resolveAndBind(moduleFinder, emptyFinder, moduleNames);
//New Module layer derived from current modulee layer
final ModuleLayer moduleLayer = currentModuleLayer.defineModulesWithOneLoader(configuration, loader);
//create new instance of Implementation, in this case org.util.npci.coreconnect.CoreController implements org.util.npci.api.BankController
final BankController bankController = ServiceLoader.load(moduleLayer, BankController.class);
return bankController;
} catch (Exception e) {BootLogger.info(e);}
return null;
}

关于Java 9 ServiceLoader 运行时模块加载和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49644752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com