Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Weld-OSGi in Action

Weld-OSGi in Action

Slides from the Devoxx 2012 'Tools in Action'
The demo code can be found here :

https://github.com/mathieuancelin/demo-devoxx2012

mathieuancelin

November 15, 2012
Tweet

More Decks by mathieuancelin

Other Decks in Programming

Transcript

  1. Speakers • Mathieu ANCELIN • Developer @ SERLI • Poitou-Charentes

    JUG • JSR-346 EG member • @TrevorReznik • Matthieu CLOCHARD • Developer @ SERLI • Poitou-Charentes JUG • @arcane86
  2. SERLI • IT company based in France • 65 people

    • 80% of business Java related • Contribute to open-source projects • 10% of work force on OSS • JSR-346 EG member • OSGi Alliance member • www.serli.com @SerliFr 4
  3. SERLI • IT company based in France • 65 people

    • 80% of business Java related • Contribute to open-source projects • 10% of work force on OSS • JSR-346 EG member • OSGi Alliance member • www.serli.com @SerliFr 4
  4. CDI

  5. • An amazing modular and dynamic platform for Java •

    Very stable and powerful but old APIs 8 Module Lifecycle Service Java environment Bundles OSGi
  6. Modules / Bundles 9 manifest manifest Bundle-SymbolicName: com.foo.bar Bundle-SymbolicName: com.sample.app

    Export-Package: com.sample.app.api; version=1.2.0 Import-Package: com.sample.app.api; version=[1.2.0-2.0.0)
  7. Modules / Bundles 9 manifest manifest Bundle-SymbolicName: com.foo.bar Bundle-SymbolicName: com.sample.app

    Export-Package: com.sample.app.api; version=1.2.0 Import-Package: com.sample.app.api; version=[1.2.0-2.0.0)
  8. Lifecycle 10 Installed Resolved Uninstalled Starting Active Stopping install update

    refresh stop start update refresh resolve uninstall uninstall
  9. Weld-OSGi • (try to be) the best of both worlds

    • dynamic, typesafe, annotations, etc ... • CDI extension to use CDI programming model inside OSGi • A JBoss Weld project • need to bootstrap Weld in an OSGi environment • Developed by SERLI R&D team • Mathieu & Matthieu • You don’t need to know OSGi • make the OSGi programming model disappear => CDI • but still compatible
  10. How does it work 13 Java Platform OSGi Platform Weld-

    OSGi bundle 1 bundle2 bundle3 beans .xml
  11. How does it work 13 Java Platform OSGi Platform Weld-

    OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml
  12. How does it work 13 Java Platform OSGi Platform Weld-

    OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml
  13. How does it work 13 Java Platform OSGi Platform Weld-

    OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml
  14. How does it work 13 Java Platform OSGi Platform Weld-

    OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml
  15. Services publication @Inject Instance<TweetPlugin> instance; @Inject ServiceRegistry registry; MyService service

    = instance.get(); Registration<TweetPlugin> reg = registry.register(service); ... reg.unregister(); @Publish @ApplicationScoped @Lang(EN) public class TweetPlugin implements Plugin { }
  16. Services injection / whiteboard @Inject Service<Plugin> services; for (MyService actualService

    : services.first()) { actualService.doSomething(); // called 0-1 service } for (MyService actualService : services) { actualService.doSomething(); // called 0-n service(s) }
  17. Services injection / whiteboard @Inject Service<Plugin> services; for (MyService actualService

    : services.first()) { actualService.doSomething(); // called 0-1 service } for (MyService actualService : services) { actualService.doSomething(); // called 0-n service(s) } services.get().doSomething(); // can fail, not dynamic services.size(); services.isUnsatisfied(); services.isAmbiguous();
  18. Filtering @Publish @Lang(EN) @Country(US) public class TweetPlugin implements Plugin {

    ... } @Inject @OSGiService @Filter("(&(lang=*)(country=US))") Plugin service;
  19. Filtering @Publish @Lang(EN) @Country(US) public class TweetPlugin implements Plugin {

    ... } @Inject @Filter("(&(lang=*)(country=US))") Service<Plugin> services;
  20. Filtering @Publish @Lang(EN) @Country(US) public class TweetPlugin implements Plugin {

    ... } @Inject @Filter("(&(lang=*)(country=US))") Service<Plugin> services; @Inject @OSGiService @Lang(EN) @Country(US) Plugin service;
  21. Filtering @Publish @Lang(EN) @Country(US) public class TweetPlugin implements Plugin {

    ... } @Inject @Filter("(&(lang=*)(country=US))") Service<Plugin> services; @Inject @Lang(EN) @Country(US) Service<Plugin> services;
  22. OSGi events • OSGi generates a lot of events to

    let you interact with bundle and service layers • Easier to handle dynamism with • bundle events • service events
  23. Bundle events • Available events • BundleInstalled • BundleResolved •

    BundleStarting • BundleStarted • BundleStopping • BundleStopped • BundleUninstalled • BundleUpdated • BundleUnresolved Installed Resolved Uninstalled Starting Active Stopping install update refresh stop start update refresh resolve uninstall uninstall
  24. Services events void bindService(@Observes ServiceArrival evt) {} • Available events

    • ServiceArrival • ServiceDeparture • ServiceChanged
  25. Services events void bindService(@Observes @Filter("(lang=US)") ServiceArrival evt) {} • Available

    events • ServiceArrival • ServiceDeparture • ServiceChanged
  26. Inter-bundle events • Communication between bundles (managed by Weld-OSGi) with

    standard CDI events Bundle A Bundle C Bundle B Weld-OSGi
  27. Inter-bundle events • Communication between bundles (managed by Weld-OSGi) with

    standard CDI events Bundle A Bundle C Bundle B Weld-OSGi fire()
  28. Inter-bundle events • Communication between bundles (managed by Weld-OSGi) with

    standard CDI events Bundle A Bundle C Bundle B Weld-OSGi fire() broadcast() broadcast()
  29. Inter-bundle events @Inject Event<InterBundleEvent> event; event.fire(new InterBundleEvent("Hello bundles")); public void

    listen(@Observes @Specification(String.class) @Sent InterBundleEvent event) {}
  30. @Inject Bundle bundle; @Inject BundleContext context; @Inject @BundleHeaders Map<String, String>

    headers; @Inject @BundleHeader("Bundle-SymbolicName") String symbolicName; @Inject @BundleDataFile("text.txt") File text; OSGi utilities
  31. If you want to give it a try • Get

    Weld-OSGi (https://github.com/weld/core) • Write an OSGi bundle • Maven module + maven-bundle-plugin • Empty beans.xml file in META-INF !!! <dependency> <groupId>org.jboss.weld.osgi</groupId> <artifactId>weld-osgi-core-api</artifactId> <version>1.2.0.Beta1</version> </dependency> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>1.0-SP4</version> </dependency> https://github.com/mathieuancelin/demo-devoxx2012
  32. The future • CDI Extension for hybrid Java EE app

    servers • using Weld-OSGi in Java EE apps • Java EE apps talking to each other through OSGi services • Java EE apps talking to OSGi bundles • work in progress (works on JBoss AS) ;-) • Integration with OSGi enterprise specs • CDI/OSGi => RFC 146 • Inspired by Weld-OSGi
  33. Conclusion • Weld-OSGi is cool • You should try it

    ;-) • Can help to change people’s mind about OSGi • Adaptative programming • Dynamic • Modularity • Don’t hesitate to give us feedback and fill issues