weld-osgi, injecting easiness in osgi

Click here to load reader

Post on 10-May-2015

3.111 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

Presentation about Weld-OSGi project for JAXLondon and JUDCon 2011.

TRANSCRIPT

  • 1.Weld-OSGiInjecting easiness in OSGi

2. Mathieu ANCELINSoftware engineer @SERLIJava & OSS guy JOnAS, GlassFish, Weld, etc ... Poitou-Charentes JUG crew memberCDI 1.1 (JSR-346) expert group memberWhat else?@TrevorReznik 3. A few words about SERLISoftware engineering company based in France65 people80% of the business is Java-relatedSmall company working for big onesOSS contribution : 10% of workforcewww.serli.com @SerliFr 4. Before we start#judcon #weld-osgi 5. AgendaCDI, the best part of Java EE 6OSGi, deep dive into modularity and dynamismMeet Weld-OSGi How does it work? Features and programming model Pros and consBack to the futureDemoConclusion 6. CDI 7. CDI@Any@ApplicationScoped @Model@Default@Inject @Observes @Dispose@SessionScoped @Named@RequestScoped @Produces@Typed@Singleton@Stereotype@Qualier @Scope@New 8. CDI@Any@ApplicationScoped @Model@Default@Inject @Observes @Dispose@SessionScoped @Named@RequestScoped @Produces@Typed@Singleton@Stereotype@Qualier @Scope@New 9. CDIThe best part of Java EE 6 (coolest) #annotationsEverywhereBasically there is no limite of what you can do if you can think about it, you can do it standard extensions :-)JBoss Weld is the reference implementation pretty mature, good communityLimited to Java EE 6? well, not necessarily ... 10. Environnements for CDI/WeldYou can boostrap Weld very easily outside Java EE environment You can bootstrap it anywhere :-)For instance Weld-ServletJettyTomcat 6/7 Weld-SEGood old Desktop Java apps. You name it ? 11. OSGiAn amazing modular and dynamic platform for JavaVery stable and powerful but old APIsBundles ServiceLifecycleModuleJava environment 12. Modules / BundlesBundle-SymbolicName: com.sample.appBundle-SymbolicName: com.foo.barmanifest manifestExport-Package: com.sample.app.api;version=1.2.0Import-Package: com.sample.app.api; version=[1.2.0-2.0.0) 13. LifecycleupdaterefreshinstallInstalled Startingupdatestartresolve refreshuninstall ActiveResolved stopuninstallStoppingUninstalled 14. Services notifylistener registerOSGIlookupBundle Aservice Bundle Bregistry 15. Weld-OSGi(try to be) the best of both worlds dynamic, typesafe, annotations, etc ...CDI extension to use CDI programming model inside OSGiA JBoss Weld project need to bootstrap Weld in an OSGi environmentDeveloped by SERLI R&D team Mathieu & MatthieuYou dont need to know OSGi make the OSGi programming model disappear in favor of standard CDI but still compatible 16. How does it work ? 17. FeaturesUse Weld/CDI inside OSGi environmentOSGi injection utilitiesDynamic services publicationDynamic services injectionIntegration with OSGi events APIInter-bundles events 18. Using Weld / CDI in OSGiInstall a bundle with META-INF/beans.xml leIf you dont need automatic startup Specify that Weld-OSGi must not handle the bundleentry in the bundle manifest : Embedded- CDIContainer: true Specication of an embedded mode in CDI 1.1 Special Weld-OSGi eventspublic void start(@Observes BundleContainerInitialized event) {}public void stop(@Observes BundleContainerShutdown event) {} 19. Embedding CDIEmbeddedCDIContainer cdi =new EmbeddedContainer(bundleContext).initialize();MyService service =cdi.instance().select(MyService.class).get();service.doSomething();cdi.stop();orWeldContainerweld =new WeldContainer(bundleContext).initialize();MyService service =weld.instance().select(MyService.class).get();service.doSomething();weld.stop(); 20. OSGi injection utilitiesEasier access to OSGi APIs (if needed) Injection of the current bundle Injection of the current bundleContext Injection of the current metadata Injection bundle les (inside OSGi container)Other utilities are added while moving forward 21. OSGi injection utilities Easier access to OSGi APIs (if needed)Injection of the current bundleInjection of the current bundleContextInjection of the current metadataInjection bundle les (inside OSGi container) Other utilities are added while moving forward@Inject Bundle bundle;@Inject BundleContext context;@Inject @BundleHeaders Map headers;@Inject@BundleHeader("Bundle-SymbolicName") StringsymbolicName;@Inject@BundleDataFile("text.txt")Filetext; 22. Services publication 23. Services publicationDeclarative publication @Publish @ApplicationScoped @Lang(EN) public class MyServiceImpl implements MyService { ... } 24. Services publicationDeclarative publication @Publish @ApplicationScoped @Lang(EN) public class MyServiceImpl implements MyService { ... }Dynamic publication @Inject Instance instance; @Inject ServiceRegistry registry; MyService service = instance.get(); Registration reg = registry.register(service); ... reg.unregister(); 25. Services injection 26. Services injectionDynamic injection@Inject @OSGiService MyService service;service.doSomething(); // fail if no services available 27. Services injection Dynamic injection@Inject @OSGiService MyService service;service.doSomething(); // fail if no services availableP get()create()R@Inject @OSGiServiceMyService service;O ProviderInjectionPointXY 28. Services injectionservice.doSomething() get()PRO doSomething() OSGiX actual service serviceYregistry unget() 29. Services injectionProgrammatic injection - whiteboard pattern (like Instance) @Inject Service service; for (MyService actualService : service.first()) { actualService.doSomething(); // called on 0-1 service } for (MyService actualService : service) { actualService.doSomething(); // called on 0-n service(s) } service.get().doSomething(); // can fail, not dynamic service.size(); service.isUnsatisfied(); service.isAmbiguous(); 30. Services injection - lters@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...} 31. Services injection - lters@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}@Inject @OSGiService @Filter("(&(lang=*)(country=US))") MyService service; 32. Services injection - lters@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}@Inject@Filter("(&(lang=*)(country=US))") Service service; 33. Services injection - lters@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}@Inject@Filter("(&(lang=*)(country=US))") Service service;@Inject @OSGiService @Lang(EN) @Country(US)MyService service; 34. Services injection - lters@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}@Inject@Filter("(&(lang=*)(country=US))") Service service;@Inject @Lang(EN) @Country(US)Service service; 35. Required servicesWhen you absolutely need specic service(s) at runtimeWeld-OSGi tell you when required services are available can work in an atomic fashion for the whole bundle can target specic types of services 36. Required services@Inject @OSGiServiceBean A@RequiredMyService service; public void start( @Observes Validrequired service evt) {}Bean Bregistration public void stop(dependencies validation events Weld-OSGi serviceregistry listener OSGi service services registryregistrations/unregistrations 37. Required services@Inject @OSGiService @Required MyService service;@Inject @OSGiService @Required MyBean bean;public void start(@Observes Valid evt) { System.out.println("services are available"); service.doSomething();}public void stop(@Observes Invalid evt) { System.out.println("services are unavailable");} 38. Required services@Inject @Required Service service;@Inject @Required Service bean;public void start(@Observes Valid evt) { System.out.println("services are available"); service.get().doSomething();}public void stop(@Observes Invalid evt) { System.out.println("services are unavailable");} 39. Required services led@Inject @Required Service service; n@Inject @Required Service bean; b upublic void start(@Observes Valid evt) { System.out.println("services are available");le service.get().doSomething();} hopublic void stop(@Observes Invalid evt) { w System.out.println("services are unavailable");} 40. Required services@Inject @OSGiService @Required MyService service;public void start(@Observes @Specification(MyService.class) ServiceAvailable evt) { System.out.println("service is available"); service.doSomething();}public void stop(@Observes @Specification(MyService.class) ServiceUnavailable evt) { System.out.println("service is unavailable");} 41. Required services@Inject @Required Service service;public void start(@Observes @Specification(MyService.class)ServiceAvailable evt) { System.out.println("service is available"); service.get().doSomething();}public void stop(@Observes @Specification(MyService.class) ServiceUnavailable evt) { System.out.println("service is unavailable");} 42. OSGi events APIOSGi generates a lot of events to let you interact with bundle and service layersEasier to handle dynamism with bundle events service events 43. OSGi events - Bundles updateAvailable eventsrefreshinstall BundleInstalled BundleResolvedInstalledStarting BundleStartingresolveupdate startrefreshuninstall BundleStarted ActiveResolved BundleStopping stopuninstall BundleStoppedStopping BundleUninstalledUninstalled BundleUpdated BundleUnresolved 44. OSGi events - Bundlespublic void bindBundle(@Observes BundleInstalled evt) {} 45. OSGi events - Bundlespublic void bindBundle(@Observes @BundleVersion("1.2.3")BundleInstalled evt) {} 46. OSGi events - Bundlespublic void bindBundle(@Observes @BundleName("com.foo.bar")BundleInstalled evt) {} 47. OSGi events - Bundlespublic void bindBundle(@Observes @BundleName("com.foo.bar")@BundleVersion("1.2.3") BundleInstalled evt) {} 48. OSGi events - ServicesAvailable events ServiceArrival ServiceDeparture ServiceChanged 49. OSGi events - Services Available eventsServiceArrivalServiceDepartureServiceChangedvoid bindService(@Observes ServiceArrival evt) {} 50. OSGi events - Services Available eventsServiceArrivalServiceDepartureServiceChangedvoid bindService(@Observes @Filter("(lang=US)") ServiceArrival evt) {} 51. OSGi events - Services Available eventsServiceArrivalServiceDepartur