0

There is an intefrace MyIntefrace.

System has several EARs, each with own implementations of that intefrace. I need to find all implementations without their names (? extends MyInterface).

I decided to use java.util.ServiceLoader and created a jar with META-INF/services/com.example.MyInterface inside it, ant this jar can be deployed in any of EARs.

The jar has a servlet and following code inside int's init method:

ServiceLoader<MyInterface > loader = ServiceLoader.load(MyInterface .class);
Iterator<MyInterface > it = loader.iterator();

But iterator still empty..

I tryed to add services/com.example.MyInterface in EARs META-INF, but result is the same.

That approach works fine in jboss 6 and tomcat.

Vladimir Kishlaly
  • 1,872
  • 1
  • 16
  • 26
  • Did you find a solution to this problem? – Rhys Jan 02 '13 at 13:48
  • each EAR must has META-INF/services/com.example.MyInterface text file with enumeration of the all implementations inside that EAR – Vladimir Kishlaly Jan 02 '13 at 13:57
  • Are you saying that using the ServiceLoader architecture you were able to discover services between separate EAR deployments? i.e. EAR1 can find services in EAR2, without declaring a direct dependency on each other? (through manifest or jboss-deployment-structure.xml) – Rhys Jan 02 '13 at 14:00
  • no, each EAR has own classloader and ServiceLoader will load only those who in parent EAR – Vladimir Kishlaly Jan 02 '13 at 15:23

1 Answers1

3

When i had the same problem, it was solved by trying lazyloading

private static class LoaderHolder {
   ServiceLoader<MyInterface > loader = ServiceLoader.load(MyInterface .class);
  }

Iterator<MyInterface > it = LoaderHolder .loader.iterator();
Sunitha
  • 31
  • 2