ServiceLoader – modularization for free

Posted in java by pedro | Wednesday, April 1st, 2009 at 11:53 pm

Another outcome of NetBeans training I was mention in previous post was information about java.util.ServiceLoader class. Current application are very large, and to provide maintenance and easy of development we can build it from modules. There are so much information about Dependency Injection, that probably everyone knows about it, frameworks such as Spring Framework, Google Guice, Pico Container etc.  OSGi with  Equinox, Apache Felix and Knopflerfish implementation and NetBeans Module API and even maven and ivy tool. This frameworks and tools help us to keep our application modular. But … Did you know that by using Java version 6 we have DI for free. ServiceLoader class allows that, and now we can write modular application without using framework with many megabytes of classes. So how it works. Assume that we have business interface :

public interface BusinessInterface {
    void businessMethod(String data);
}

And we have two implementation in different modules.

public class BusinessFirstImpl implements BusinessInterface {
   public void businessMethod(String data) {
     System.out.println("First processing " + data);
   }
}
public class BusinessAnotherImpl implements BusinessInterface {
   public void businessMethod(String data) {
     System.out.println("A pnother processing " + data);
   }
}

Two make this working, every jar must have META-INF/services directory which contains file which names is exactly the same as FQN of the interface.

metainf1

In this file we should have single line per contract implementation

info.pietrowski.first.BusinessFirstImpl
info.pietrowski.another.BusinessAnotherImpl
...

We can discover our service using ServiceLoader class. This is the main class.

public class DependencyMain {
  public static void main(String[] args) {
    ServiceLoader implementation = ServiceLoader.load(BusinessInterface.class);
    for (BusinessInterface impl : implementation) {
       impl.businessMethod(impl.getClass().getName());
    }
  }
}

Depending on which jars you have in classpath we can have one of  outcome.

>java -cp main.jar info.pietrowski.DependencyMain
>java -cp main.jar;first.jar info.pietrowski.DependencyMain
First processinginfo.pietrowski.first.BusinessFirstImpl
>java -cp main.jar;another.jar info.pietrowski.DependencyMain
Another processinginfo.pietrowski.another.BusinessAnotherImpl
>java -cp main.jar;another.jar;first.jar info.pietrowski.DependencyMain
Another processinginfo.pietrowski.another.BusinessAnotherImpl
First processinginfo.pietrowski.first.BusinessFirstImp

Download full code source and jar files has also sources. Next time you can use this mechanism in simple and light application.

One Response to “ServiceLoader – modularization for free”

  1. Brush says:

    Impressive overview Pedro. It was a nice weekend reading.

Leave a Reply

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.

Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.

My Yoda's motto: Do, or do not. There is no try.