Amrit
Amrit

Reputation: 2333

Should each bundle be a service if it has to be imported in another bundle?

I have been studying OSGi for a week now. I studied OSGi services but can't find out if every bundle which needs to be imported in another bundle has to be a service. Whether I can use them as simple JARs rather than as services?

If yes, then in what case I should make a bundle as a service?

I hope I made my question clear.

Upvotes: 0

Views: 74

Answers (1)

Peter Kriens
Peter Kriens

Reputation: 15372

Services are the meeting points between bundles. In general, a bundle depends on a number of services and provides 0 or more services. Since services only specify the contracts, you are ONLY depending on the contracts. This way you separate your actual dependencies (the contracts) from the providers of those dependencies (the bundles).

You will find that your software becomes much more stable over time since it is not affected by all kinds of changes in the runtime environment: refactoring bundles, different implementations, different configurations. You should see services as the hinges of your architecture.

That said, you're not forced to use services. Bundle can import the classes of other bundles and provide classes to other bundles.

So when to use services? Usually a service is an abstraction: a Log service, an Event Admin service, a Geo Service, etc. You know what it should do for you, but you're not interested in the details. In general services can be implemented in different ways.

If the API and implementation collapse into one, it is not a service. A library like ASM (bytecode engineering) or Guava are not services since the API is the implementation and neither library holds state.

Upvotes: 2

Related Questions