I am trying to load modules dynamically from a folder.
Suppose I have a “Module A” that has its own set of dependencies and “Module B” that has its own set of dependencies + it also relies on some functionality from “Module A”. Each set of dependencies must be independent, to avoid the conflicts if multiple modules rely have the same dependency but of different versions. Then, the layer tree will look like this (each box is a separate layer):
ModuleLayers Tree
(I do not have enough reputation to post images)
However, with that Module Layers configuration if I try to run a ServiceLoader
:
ServiceLoader.load(allModulesLayer, MyService.class);
I get the following array: [ moduleA.ServiceImpl@..., moduleB.ServiceImpl@..., moduleA.ServiceImpl@... ]
, so it seems like the ServiceLoader simply walks the layers from the given layer until it reaches the boot layer, resulting in two paths, where it encounters moduleA
(allModules -> moduleA -> moduleADeps -> boot
and allModules -> moduleB -> moduleA -> moduleADeps -> boot
).
The same is true if I do not create the allModulesLayer
, and just directly call ServiceLoader
on each module layer:
ServiceLoader.load(layerA, MyService.class); // -> moduleA.ServiceImpl
ServiceLoader.load(layerB, MyService.class); // -> moduleB.ServiceImpl, moduleA.ServiceImpl
How can I make it so that ServiceLoader
only loads services non-recursively, only from the given layer, that way I can just run ServiceLoader
on each moduleX
layer separately (like in the latter example), and join the Lists. Or somehow make it so that ServiceLoader
does not load duplicate classes (and use the common allModulesLayer
like in the former example).
(Edit: layerB.modules()
only shows moduleB
, so the layer does not contain moduleA
, yet ServiceLoader
somehow discovers it)
Assuming that the finder
is a ModuleFinder
of a folder, where all the module extensions are, to load module A, I do:
// Dependencies for "moduleA" are located at "./moduleA/dependencies":
ModuleFinder finderADeps = ModuleFinder.of(Paths.get("moduleA", "dependencies"));
// Get all the dependencies from the folder:
List<String> allADeps = finderADeps.findAll().stream().map(ModuleReference::descriptor).map(ModuleDescriptor::name).toList();
// Resolve all the dependencies using the previously defined finder:
Configuration configADeps = ModuleFinder.boot().configuration().resolve(finderADeps, ModuleFinder.of(), allADeps);
// Load the modules into the ["Module A" Dependencies Layer]:
ModuleLayer layerADeps = ModuleFinder.boot().defineModulesWithOneLoader(configADeps, ClassLoader.getSystemClassLoader());
// Define the configuration for ["Module A" Layer] that is a child of the Dependencies Layer Configuration:
Configuration configA = Configuration.resolveAndBind(finder, List.of(configADeps), ModuleFinder.of(), Set.of("moduleA"));
// Load the modules into the ["Module A" Layer] that is a child of the Dependencies Layer:
ModuleLayer layerA = ModuleLayer.defineModulesWithOneLoader(configA, List.of(layerADeps), ClassLoader.getSystemClassLoader()).layer();
Loading module B is almost identical, but when we form the “Module B” Layer:
// Add "configA" as a parent too:
Configuration configB = Configuration.resolveAndBind(finder, List.of(configA, configBDeps), ModuleFinder.of(), Set.of("moduleB"));
// Add "layerA" as a parent too:
ModuleLayer layerB = ModuleLayer.defineModulesWithOneLoader(configB, List.of(layerA, layerBDeps), ClassLoader.getSystemClassLoader()).layer();
module-info
are as follows:
moduleA:
import ...;
module moduleA {
requires core.api;
exports com.module_a;
provides MyService with ServiceImpl;
... other dependencies
}
moduleB:
import ...;
module moduleB {
requires core.api;
exports com.module_b;
requires static moduleA;
provides MyService with ServiceImpl;
... other dependencies
}
Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.