Difference between revisions of "Tutorial OSGi avec Apache Felix - Partie 4"

From air
Jump to navigation Jump to search
Line 37: Line 37:
   
 
=II. Pure-OSGi service=
 
=II. Pure-OSGi service=
<pre>
 
   
1. Create the API bundle "bundle.api"
+
# Create an new bundle project ''fr.erods.tp.api''.
  +
#* Activator is not needed: uncheck "Generate an activator" in step 2
- "Target Platform" > "an OSGi framework" > "standard"
 
  +
# Create the <code>fr.erods.tp.api.IScheduleService</code> interface with two methods:
- Next
 
  +
#* <code>int createJob(Runnable aRunnable, int aDelay)</code>
- Uncheck "Generate an activator"
 
  +
#* <code>void deleteJob(int aId)</code>
- Finish
 
 
# Export the API package
 
2. Create the IScheduleService interface in the package fr.erods.tp.api
+
#* Open the Manifest.mf file of ''fr.erods.tp.api''
 
#* In tab "Runtime" > "Exported Packages" > Add
 
3. Export the API package
+
#** Select the <code>fr.erods.tp.api</code> package
- Open Manifest.mf of bundle.api
+
# Implement the service in bundle 1
 
#* Create the class <code>fr.erods.tp.bundle1.ScheduleImpl</code>, implementing <code>IScheduleService</code> in the bundle 1.
- Runtime > "exported packages" > Add
 
 
# Register the service with the activator of bundle 1
 
  +
#* see [http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html#registerService%28java.lang.Class,%20S,%20java.util.Dictionary%29 registerService()]
4. Import the API package in bundle 1
 
  +
#* see [http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/ServiceRegistration.html#unregister%28%29 unregister()]
- Open Manifest.mf of bundle.api
 
  +
# Implement a service consumer in a new bundle
- Dependencies > Imported Packages > Add
 
  +
#* Create project ''fr.erods.tp.bundle2''
 
 
#* Import the API package
5. Implement the service in bundle 1
 
 
#** In the Manifest.mf file of bundle 2
- Create class ScheduleImpl implementing IScheduleService in a package in bundle 1
 
 
#** In tab "Dependencies" > "Imported Packages" > Add
 
  +
#** Select the <code>fr.erods.tp.api</code> package
6. Export the service in the Activator
 
 
#* Implement a <code>Consumer</code> class, implementing the OSGi <code>ServiceListener</code> interface
- regSvc -> start()
 
 
#* Print a message when the service is bound/unbound
- unregSvc -> stop()
 
 
# Test the bundles in Felix
 
7. Implement the client in bundle 2
+
#* What happens when the consumer is started after the service bundle ?
  +
#* Correct this behavior, using [http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html#getServiceReference%28java.lang.Class%29 getServiceReference()] and [http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html#getService%28org.osgi.framework.ServiceReference%29 getService()]
- Create project bundle.2 (with activator)
 
- Import package API
 
- Implement a Consumer class, implementing ServiceListener
 
- Print a message when a service is bound/unbound
 
 
8. Test the bundles
 
</pre>
 
   
 
=II. iPOJO: provide and consume with iPOJO=
 
=II. iPOJO: provide and consume with iPOJO=

Revision as of 17:08, 19 December 2012

Revenir au sommaire

Requirements

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.

  1. Open Eclipse preferences
  2. Plug-in Development > Target Platform
  3. Add...
    1. Create an empty target platform (check nothing...)
    2. Add the bin and bundle folders of your Felix installation to the Target Platform
  4. Set the new target platform as the current one

I. Activator

  1. 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
  2. Open the activator class and print some text when the bundle starts and stops
  3. 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
  4. Install and test the bundle using the Felix shell (start/update/stop)

II. Pure-OSGi service

  1. Create an new bundle project fr.erods.tp.api.
    • Activator is not needed: uncheck "Generate an activator" in step 2
  2. Create the fr.erods.tp.api.IScheduleService interface with two methods:
    • int createJob(Runnable aRunnable, int aDelay)
    • void deleteJob(int aId)
  3. 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
  4. Implement the service in bundle 1
    • Create the class fr.erods.tp.bundle1.ScheduleImpl, implementing IScheduleService in the bundle 1.
  5. Register the service with the activator of bundle 1
  6. 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 OSGi ServiceListener interface
    • Print a message when the service is bound/unbound
  7. Test the bundles in Felix

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

??

X. Guidelines & Good Practices ("OSGi Zen")