2

I'm trying to run a service from the module but next error appears:

Exception in thread "main" java.util.ServiceConfigurationError: com.example.prototype.api.Service: Provider com.example.prototype.foo.FooService not a subtype
        at java.util.ServiceLoader.fail(ServiceLoader.java:239)
        at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
        at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
        at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
        at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
        at java.lang.Iterable.forEach(Iterable.java:74)
        at com.example.prototype.api.Main.main(Main.java:19)

Approach and architecture description:
I would like to have an application with modules where one module describes interfaces and another implements it. The idea is that the interface module shouldn't know anything about concrete implementation.
The codebase is located on github.

Notes:
api-module has a Service interface. Also, it has Main class that instantiates foo-module with calling service implementation.
foo-module has the implementation of the Service interface.

API-module Main class:

public class Main {

  public static void main(String[] args) throws Throwable {
    File root = new File("prototype");

    ModuleLoader loader = new ModuleLoader(new LocalModuleFinder(new File[]{root}));
    Module module = loader.loadModule("com.example.prototype.foo");
    ServiceLoader<Service> services = module.loadService(Service.class);

    System.out.println("Foo Service loaded");
    services.forEach(Service::sayWhatsup);
    System.out.println("Done!");
  }

}

Foo module module.xml:

<?xml version="1.0" encoding="UTF-8"?>

<module xmlns="urn:jboss:module:1.8" name="com.example.prototype.foo" version="1.0">
  <main-class name="com.example.prototype.foo.Main"/>
  <resources>
    <artifact name="com.example.prototype:foo-module:1.0"/>
  </resources>

  <dependencies>
    <module name="com.example.prototype.api"/>
  </dependencies>

  <provides>
    <service name="com.example.prototype.api.Service">
      <with-class name="com.example.prototype.foo.FooService"/>
    </service>
  </provides>

</module>

As far as I understand Service.class and FooService have different class loaders and that's because the class compatibility issue appears.

Please help to solve this. Thanks in advance!

Bullet-tooth
  • 782
  • 1
  • 10
  • 16

0 Answers0