OSGi Service Discovery
OSGi Service Discovery
OSGi
• Introducing the OSGi Framework
– OSGi Alliance
– Components/Bundles
– Module and Life Cycle layer
• Services in OSGi
– Service Registry
– Service Patterns
– Remote Services
2
OSGi Alliance
• Open Services Gateway initiative
• OSGi Alliance is a non-profit consortium
– founded in march 1999
– specifies/maintains the OSGi standard
• specifying detailed API
• providing reference implementation / test cases
– Members: IBM, Nokia, Motorola, BMW,….
Services
Security
Bundles Life Cycle
Modules
Execution Environment
Operating System
Hardware
4
OSGi Bundle
Bundle Manifest
• Packages
JAR FILE
import org.abc…
• Own classes, interfaces
• Other resources
export org.xy….
Services
Manifest file
rt
s ol
ta
ve
s
d
Life Cycle
ll
sta
in
un
Stopped Modules
Uninstalled
Execution Environment
Bundle Bundle
Manifest Services
import … Manifest
export … import …
export … Life Cycle
Class Loader provides
Modules
Class Loader
Execution Environment
8
OSGi Service Registry
Service Registry
• register services by providing class or
interface name and optional properties
• Find by interface/class name
z
Service
g
(stateful)
• Registry is returning only compatible
versions of an object Life Cycle
Bundle yz
/ b ind x
fin d Modules
xyz.class
abc.class
Java Virtual Machine 9
Service Registry
• Problem
– Services can be provided and removed at any time
Client has to poll regularly to find an appropriate service
Client can get NullPointerException if service is removed
registe
callba
r
r xyz
listene
ck
e
regist
r
Requester
1. Implements ServiceListener interface with
Provider serviceChanged method
2. Register ServiceListener at Registry with
requested interface names/properties
3. Whenever an appropriate service is
xyz.class
registered the callback method
serviceChanged is invoked with an
ServiceEvent object as parameter
11
Service Tracker
• Problem
– ServiceListener is too complicate to implement, not
flexible enough and error prone
13
Whiteboard Pattern
Service Registry
•f ServiceListener
registe
callba
r
r xyz
listene
ck
e
regist
r
Subscriber
1. Implements ServiceListener interface with
Provider serviceChanged method
2. Register ServiceListener at Registry with
requested interface names/properties
3. Whenever an appropriate service is
xyz.class
registered the callback method
serviceChanged is invoked with an
ServiceEvent object as parameter
14