Simple Java service discovery framework

I would like to discover all implementations of an interface during runtime in a Java app, and load those classes dynamically from JAR files that the user can add to a folder. It's a plug-in system, basically.

I found a few solutions for this:

Are there any other widely used solutions for this?

Update: There is no need for code separation, and OSGi seems to be far too complex for my simple needs right now. I also added "Simple" to the title of this question to clarify my intentions.

Upvotes: 8

Views: 3038

Answers (4)

user469187
user469187

Reputation: 11

You could try the service discovery of Apache River (formerly Jini)

http://river.apache.org/

It discovers services by a Remote interface and downloads the proxy for you, which is topically an RMI stub.

Upvotes: 1

Partly Cloudy
Partly Cloudy

Reputation: 6923

+1 SPI -- it is lightweight, a little bit of maintenance (the services/* text files) but the way Java's classloaders work any "autodiscovery" isn't going to be 100% reliable; you can reduce the hassle of the text files with a simple program which for a given project/directory generates and/or (I recommend) tests that all implementations are included

+2 OSGi -- if you're willing to go welterweight, and note the implementations are getting lighter and easier (at least some of them!)

Upvotes: 0

Riduidel
Riduidel

Reputation: 22300

Like @skaffman told, OSGi and modern implementations built upon seems like a perfect solution. If you're on the guice bandwagon, consider iPOJO, which perfectly incorporates annotations in the OSGi stack (notice iPOJO works on any OSGi platform). If you're more in the XML/Spring bandwagon, consider using Blueprint.

Finally, considering JSPF, I won't share your opinon about its immaturity. Having collaborated a little on that project, I find it really useful in its field, as it allows easy plugin use, without the hassle and classpath separaion that OSGi provides.

Upvotes: 0

skaffman
skaffman

Reputation: 403551

It's bit on the heavy side, but you should consider Apache Felix or Eclipse Equinox, both are OSGi implementations which are very much alive and kicking, but possibly overkill for your needs. However, this is one of the very problems that OSGi is designed to solve.

Upvotes: 4

Related Questions