Share
Embed
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
Transcript
71 slides
-
Speakers • Mathieu ANCELIN • Developer @ SERLI • Poitou-Charentes
JUG • JSR-346 EG member • @TrevorReznik • Matthieu CLOCHARD • Developer @ SERLI • Poitou-Charentes JUG • @arcane86 -
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 -
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 -
CDI @Inject @Named @Qualifier @Scope @ApplicationScoped @SessionScoped @RequestScoped @Observes @Any
@Default @Dispose @Model @Stereotype @New @Produces @Typed -
CDI @Inject @Named @Qualifier @Scope @ApplicationScoped @SessionScoped @RequestScoped @Observes @Any
@Default @Dispose @Model @Stereotype @New @Produces @Typed -
• An amazing modular and dynamic platform for Java •
Very stable and powerful but old APIs 8 Module Lifecycle Service Java environment Bundles OSGi -
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 -
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) -
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) -
Lifecycle 10 Installed Resolved Uninstalled Starting Active Stopping install update
refresh stop start update refresh resolve uninstall uninstall -
Services 11 OSGI service registry Bundle A Bundle B listener
register lookup -
Services 11 OSGI service registry Bundle A Bundle B listener
register notify -
Services 11 OSGI service registry Bundle A Bundle B listener
register notify lookup -
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 -
How does it work 13 Java Platform OSGi Platform Weld-
OSGi bundle 1 bundle2 bundle3 -
How does it work 13 Java Platform OSGi Platform Weld-
OSGi bundle 1 bundle2 bundle3 beans .xml -
How does it work 13 Java Platform OSGi Platform Weld-
OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml -
How does it work 13 Java Platform OSGi Platform Weld-
OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml -
How does it work 13 Java Platform OSGi Platform Weld-
OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml -
How does it work 13 Java Platform OSGi Platform Weld-
OSGi bundle 1 bundle2 bundle3 beans .xml beans .xml -
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 { } -
Service injection @Inject @OSGiService Plugin plugin; plugin.doSomething(); // fail if
no services available -
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 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(); -
Filtering @Publish @Lang(EN) @Country(US) public class TweetPlugin implements Plugin {
... } @Inject @OSGiService @Filter("(&(lang=*)(country=US))") Plugin service; -
Filtering @Publish @Lang(EN) @Country(US) public class TweetPlugin implements Plugin {
... } @Inject @Filter("(&(lang=*)(country=US))") Service<Plugin> services; -
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; -
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; -
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 -
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 -
Services events void bindService(@Observes ServiceArrival evt) {} • Available events
• ServiceArrival • ServiceDeparture • ServiceChanged -
Services events void bindService(@Observes @Specification(Plugin.class) ServiceArrival evt) {} • Available
events • ServiceArrival • ServiceDeparture • ServiceChanged -
Services events void bindService(@Observes @Filter("(lang=US)") ServiceArrival evt) {} • Available
events • ServiceArrival • ServiceDeparture • ServiceChanged -
Services events void bindService(@Observes @Specification(Plugin.class) @Filter("(lang=US)") ServiceArrival evt) {} •
Available events • ServiceArrival • ServiceDeparture • ServiceChanged -
Inter-bundle events • Communication between bundles (managed by Weld-OSGi) with
standard CDI events Bundle A Bundle C Bundle B Weld-OSGi -
Inter-bundle events • Communication between bundles (managed by Weld-OSGi) with
standard CDI events Bundle A Bundle C Bundle B Weld-OSGi fire() -
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() -
Inter-bundle events @Inject Event<InterBundleEvent> event; event.fire(new InterBundleEvent("Hello bundles")); public void
listen(@Observes InterBundleEvent event) {} -
Inter-bundle events @Inject Event<InterBundleEvent> event; event.fire(new InterBundleEvent("Hello bundles")); public void
listen(@Observes @Specification(String.class) InterBundleEvent event) {} -
Inter-bundle events @Inject Event<InterBundleEvent> event; event.fire(new InterBundleEvent("Hello bundles")); public void
listen(@Observes @Sent InterBundleEvent event) {} -
Inter-bundle events @Inject Event<InterBundleEvent> event; event.fire(new InterBundleEvent("Hello bundles")); public void
listen(@Observes @Specification(String.class) @Sent InterBundleEvent event) {} -
@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 -
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 -
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 -
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