Tutorial OSGi avec Apache Felix - Partie 4
Requirements
- Felix Framework Distribution (voir Tutorial OSGi avec Apache Felix - Partie 1)
- Eclipse.
- To support OSGi development, Eclipse needs the PDE (Plug-in Development Environment) plug-in, bundled in Eclipse IDE for Java EE Developers
- iPOJO Nature plug-in for Eclipse as an archive distribution
- Install the iPOJO Project Nature and iPOJO Nature Dependencies plug-ins
Eclipse Configuration
By default, Eclipse uses its installation directory as the Target Platform, i.e. the group of bundles accessible by your bundle project. You should declare the installation folder of the Felix Framework as the target platform to avoid compilation and access rights errors.
- Open Eclipse preferences
- Plug-in Development > Target Platform
- Add...
- Create an empty target platform (check nothing...)
- Add the bin and bundle folders of your Felix installation to the Target Platform
- Set the new target platform as the current one
I. Activator
- Create an Eclipse Plug-in project fr.erods.tp.bundle1
- New > Plug-in project
- Select the option "Target Platform" > "an OSGi framework" > standard
- Next
- Check "Generate an activator"
- Finish
- Open the activator class and print some text when the bundle starts and stops
- Export the bundle JAR file with iPOJO Nature
- Right click on the bundle project > Export > iPOJO Bundle
- Choose an output folder (the Felix installation folder)
- Finish
- Install and test the bundle using the Felix shell (start/update/stop)
II. Pure-OSGi service
- Create an new bundle project fr.erods.tp.api.
- Activator is not needed: uncheck "Generate an activator" in step 2
- Create the
fr.erods.tp.api.IScheduleService
interface with two methods:int createJob(Runnable aRunnable, int aDelay)
void deleteJob(int aId)
- Export the API package
- Open the Manifest.mf file of fr.erods.tp.api
- In tab "Runtime" > "Exported Packages" > Add
- Select the
fr.erods.tp.api
package
- Select the
- Implement the service in bundle 1
- Create the class
fr.erods.tp.bundle1.ScheduleImpl
, implementingIScheduleService
in the bundle 1.
- Create the class
- Register the service with the activator of bundle 1
- see registerService()
- see unregister()
- Implement a service consumer in a new bundle
- Create project fr.erods.tp.bundle2
- Import the API package
- In the Manifest.mf file of bundle 2
- In tab "Dependencies" > "Imported Packages" > Add
- Select the
fr.erods.tp.api
package
- Implement a
Consumer
class, implementing the OSGiServiceListener
interface - Print a message when the service is bound/unbound
- Test the bundles in Felix
- What happens when the consumer is started after the service bundle ?
- Correct this behavior, using getServiceReference() and getService()
II. iPOJO: provide and consume with iPOJO
1. Either use new projects or rewrite existing ones -> Import package API 2. Set iPOJO Nature to the project, with annotations 3. Implement ConsumerIpojo and ProviderIpojo 4. Test
III. Whiteboard pattern
1. Create interface IJobListener in package API (onJobCreation, onJobDeletion) 2. Create a new bundle, with iPOJO annoations 3. Import package API 4. Create a component providing IJobListener 5. Modify the schedule component to require the listeners, as optional requirement, and notify them on job creation/deletion 6. Test
IV. Config Admin with iPOJO
V. JMX MBean with iPOJO
1. Create a new bundle, with iPOJO annotations to list current jobs status
VI. Gogo Command with iPOJO
1. Create a new bundle, with iPOJO annotations to list current jobs status
VII. Event Admin with iPOJO
1. Create a new bundle, with iPOJO annotations to send events on jobs status changes
IX. Extender with iPOJO
??