I use a working example derived from CodeFx's book and code sample (here), which I stripped to the essentials:
All three are visible on Github here.
The goal is to get the Monitor module to make use of the Mock Observed Service. The 2 should not be coupled, as the Mock service could be replaced at run time by another service (using Update4J to download and add this new service on the module path for example).
But I am stuck at a basic level:
In the Monitor module, this line returns an empty List: no ServiceObserverFactory
has been found:
List<ServiceObserverFactory> observerFactories = ServiceLoader
.load(ServiceObserverFactory.class).stream()
.map(Provider::get)
.collect(toList());
I see from codefx's readme that it is probably due to the Maven build: the Monitor module does not 'see' the other two modules. I should add a reference to them in Maven. But if I do so, I reintroduce some coupling, preventing me to add another service implementation at run time.
Is there a way I can get this code example to work without reintroducing any tight coupling?
[last note: I see the notion of "layers" elsewhere: is it the way to go?]
Module layers need to be involved. A full working example is given in this code repo by Paul Bakker (companion site to their book: "Java 9 Modularity").