[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [equinox-dev] Service or Export-Package

On 01/04/2008, Srijith Kochunni <ksrijith@xxxxxxxxxx> wrote:

Hi All,

           This is more a generic OSGi question. I understand the difference between Require-Bundle and Import-Package. But what i`m not able to understand is when should I register my class as a service?, and when I should simply export the package, so that other packages can import and use them?

What is the advantage of registering as a service?

a few more benefits of services:

  1) you can use different implementations of a service at the same time
  2) services can have rich metadata, which you can use to filter on
  3) you can customize services per clients (see ServiceFactory interface)
  4) service implementations are POJOs, no need to create instances
      (ie. to use an imported class you may need to create an instance
       which can lead to classloading issues that you might not see with
       services, as they're created by the registering bundle...)
  5) a service has more fine-grained information about who's using it
      (as a bundle could get and unget a service during it's lifetime)

I'm sure I've missed other benefits - of course there is a management
overhead to services, but it's not too high compared to the benefits :)

I`ve gone through the OSGi best practices doc, by BJ Hargrave and Peter Kriens, and read about dynamism of using service. But let me say that i have some core bundles which I am going to always have loaded, then should I expose my classes as a service or use Export-Package alone.. Which is the better practice.?

I tend to use Export-Package for static dependencies that I know will
only have a single provider - mostly utility methods - and of course to
provide service APIs ;)

for major bundle dependencies, especially methods involving state,
I would typically use services, because of the benefits I listed above

but there's no golden rule, and a lot of this will come from experience
- you can always migrate to services over time if you keep your code
decoupled (you can use Declarative Services, Spring-DM or iPOJO
to inject services, which minimises their impact on client code)

btw, when separating code into bundles it's a good idea to minimise
dependencies between them by using something like the "CRC card"
design approach - and as part of this exercise see if they look more
like services or exports...




equinox-dev mailing list

Cheers, Stuart