OSGi Component Programming By Peter Kriens, OSGi Evangelist.
Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep...
-
Upload
phungduong -
Category
Documents
-
view
220 -
download
2
Transcript of Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep...
![Page 1: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/1.jpg)
![Page 2: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/2.jpg)
Weld-OSGiInjecting easiness in OSGi
![Page 3: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/3.jpg)
Mathieu ANCELIN
•Software engineer @SERLI•Java & OSS guy
•JOnAS, GlassFish, Weld, etc ...•Poitou-Charentes JUG crew member
•CDI 1.1 (JSR-346) expert group member•What else?•@TrevorReznik
![Page 4: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/4.jpg)
A few words about SERLI
•Software engineering company based in France•65 people•80% of the business is Java-related•Small company working for big ones•OSS contribution : 10% of workforce•www.serli.com @SerliFr
![Page 5: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/5.jpg)
Before we start
#judcon
#weld-osgi
![Page 6: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/6.jpg)
Agenda
•CDI, the best part of Java EE 6•OSGi, deep dive into modularity and dynamism•Meet Weld-OSGi
•How does it work?•Features and programming model•Pros and cons
•Back to the future•Demo•Conclusion
![Page 7: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/7.jpg)
CDI
![Page 8: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/8.jpg)
CDI
@Inject
@Named
@Qualifier
@Singleton
@Scope
@ApplicationScoped
@SessionScoped
@RequestScoped
@Observes
@Any
@Default
@Dispose
@Model
@Stereotype
@New
@Produces @Typed
![Page 9: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/9.jpg)
CDI
@Inject
@Named
@Qualifier
@Singleton
@Scope
@ApplicationScoped
@SessionScoped
@RequestScoped
@Observes
@Any
@Default
@Dispose
@Model
@Stereotype
@New
@Produces @Typed
![Page 10: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/10.jpg)
CDI
•The best part of Java EE 6 (coolest)•#annotationsEverywhere
•Basically 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 community
•Limited to Java EE 6?•well, not necessarily ...
![Page 11: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/11.jpg)
Environnements for CDI/Weld
•You can boostrap Weld very easily outside Java EE environment•You can bootstrap it anywhere :-)
•For instance•Weld-Servlet
•Jetty•Tomcat 6/7
•Weld-SE•Good old Desktop Java apps.
•You name it ?
![Page 12: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/12.jpg)
OSGi
•An amazing modular and dynamic platform for Java•Very stable and powerful but old APIs
Module
Lifecycle
Service
Java environment
Bundles
![Page 13: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/13.jpg)
Modules / Bundles
manifestmanifest
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)
![Page 14: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/14.jpg)
Lifecycle
Installed
Resolved
Uninstalled
Starting
Active
Stopping
install
updaterefresh
stop
startupdaterefreshresolve
uninstall
unin
stal
l
![Page 15: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/15.jpg)
Services
OSGIservice registry
Bundle A Bundle B
listener
register
notify
lookup
![Page 16: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/16.jpg)
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 in favor of standard CDI
•but still compatible
![Page 17: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/17.jpg)
How does it work ?
![Page 18: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/18.jpg)
Features
•Use Weld/CDI inside OSGi environment•OSGi injection utilities•Dynamic services publication•Dynamic services injection•Integration with OSGi events API•Inter-bundles events
![Page 19: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/19.jpg)
Using Weld / CDI in OSGi•Install a bundle with META-INF/beans.xml file•If you don’t need automatic startup
•Specify that Weld-OSGi must not handle the bundle•entry in the bundle manifest : Embedded-CDIContainer: true
•Specification of an embedded mode in CDI 1.1•Special Weld-OSGi events
public void start(@Observes BundleContainerInitialized event) {}
public void stop(@Observes BundleContainerShutdown event) {}
![Page 20: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/20.jpg)
Embedding CDIEmbeddedCDIContainer cdi = new EmbeddedContainer(bundleContext).initialize();MyService service = cdi.instance().select(MyService.class).get();service.doSomething();cdi.stop();
or
WeldContainer weld = new WeldContainer(bundleContext).initialize();MyService service = weld.instance().select(MyService.class).get();service.doSomething();weld.stop();
![Page 21: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/21.jpg)
OSGi injection utilities•Easier access to OSGi APIs (if needed)
•Injection of the current bundle•Injection of the current bundleContext•Injection of the current metadata•Injection bundle files (inside OSGi container)
•Other utilities are added while moving forward
![Page 22: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/22.jpg)
OSGi injection utilities
@Inject Bundle bundle; @Inject BundleContext context; @Inject @BundleHeaders Map<String, String> headers; @Inject @BundleHeader("Bundle-SymbolicName") String symbolicName; @Inject @BundleDataFile("text.txt") File text;
•Easier access to OSGi APIs (if needed)•Injection of the current bundle•Injection of the current bundleContext•Injection of the current metadata•Injection bundle files (inside OSGi container)
•Other utilities are added while moving forward
![Page 23: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/23.jpg)
Services publication
![Page 24: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/24.jpg)
Services publication•Declarative publication@Publish@ApplicationScoped@Lang(EN)public class MyServiceImpl implements MyService { ...}
![Page 25: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/25.jpg)
Services publication
•Dynamic publication
•Declarative publication
@Inject Instance<MyService> instance;@Inject ServiceRegistry registry;MyService service = instance.get();Registration<MyService> reg = registry.register(service);...reg.unregister();
@Publish@ApplicationScoped@Lang(EN)public class MyServiceImpl implements MyService { ...}
![Page 26: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/26.jpg)
Services injection
![Page 27: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/27.jpg)
Services injection
•Dynamic injection@Inject @OSGiService MyService service;service.doSomething(); // fail if no services available
![Page 28: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/28.jpg)
Services injection
•Dynamic injection@Inject @OSGiService MyService service;service.doSomething(); // fail if no services available
@Inject @OSGiService MyService service;
PROXY
Provider InjectionPoint
create()get()
![Page 29: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/29.jpg)
Services injection
service.doSomething()
PROXY
get()
OSGiserviceregistry
unget()
actualservice
doSomething()
![Page 30: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/30.jpg)
Services injection•Programmatic injection - whiteboard pattern (like Instance<T>)@Inject Service<MyService> 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 dynamicservice.size();service.isUnsatisfied();service.isAmbiguous();
![Page 31: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/31.jpg)
Services injection - filters@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}
![Page 32: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/32.jpg)
@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}
@Inject @OSGiService @Filter("(&(lang=*)(country=US))") MyService service;
Services injection - filters
![Page 33: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/33.jpg)
@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}
@Inject @Filter("(&(lang=*)(country=US))") Service<MyService> service;
Services injection - filters
![Page 34: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/34.jpg)
@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}
@Inject @Filter("(&(lang=*)(country=US))") Service<MyService> service;
@Inject @OSGiService @Lang(EN) @Country(US) MyService service;
Services injection - filters
![Page 35: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/35.jpg)
@Publish@Lang(EN) @Country(US)public class MyServiceImpl implements MyService { ...}
@Inject @Filter("(&(lang=*)(country=US))") Service<MyService> service;
@Inject @Lang(EN) @Country(US) Service<MyService> service;
Services injection - filters
![Page 36: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/36.jpg)
Required services
•When you absolutely need specific service(s) at runtime•Weld-OSGi tell you when required services are available•can work in an atomic fashion for the whole bundle•can target specific types of services
![Page 37: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/37.jpg)
Required services@Inject @OSGiService @Required MyService service;
Weld-OSGi serviceregistry
public void start( @Observes Valid evt) {}public void stop(
OSGi serviceregistry
listener
servicesregistrations/unregistrations
required serviceregistration
dependencies validation events
Bean B
Bean A
![Page 38: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/38.jpg)
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");}
![Page 39: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/39.jpg)
Required services@Inject @Required Service<MyService> service;@Inject @Required Service<MyBean> 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");}
![Page 40: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/40.jpg)
Required services@Inject @Required Service<MyService> service;@Inject @Required Service<MyBean> 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");}whol
e bund
le
![Page 41: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/41.jpg)
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");}
![Page 42: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/42.jpg)
Required services@Inject @Required Service<MyService> 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");}
![Page 43: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/43.jpg)
OSGi events API•OSGi generates a lot of events to let you interact with bundle and service layers
•Easier to handle dynamism with•bundle events•service events
![Page 44: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/44.jpg)
OSGi events - Bundles•Available events
•BundleInstalled•BundleResolved•BundleStarting•BundleStarted•BundleStopping•BundleStopped•BundleUninstalled•BundleUpdated•BundleUnresolved
Installed
Resolved
Uninstalled
Starting
Active
Stopping
install
updaterefresh
stop
startupdaterefreshresolve
uninstall
unin
stal
l
![Page 45: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/45.jpg)
OSGi events - Bundles
public void bindBundle(@Observes BundleInstalled evt) {}
![Page 46: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/46.jpg)
OSGi events - Bundles
public void bindBundle(@Observes @BundleVersion("1.2.3") BundleInstalled evt) {}
![Page 47: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/47.jpg)
OSGi events - Bundles
public void bindBundle(@Observes @BundleName("com.foo.bar") BundleInstalled evt) {}
![Page 48: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/48.jpg)
OSGi events - Bundles
public void bindBundle(@Observes @BundleName("com.foo.bar") @BundleVersion("1.2.3") BundleInstalled evt) {}
![Page 49: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/49.jpg)
OSGi events - Services
•Available events•ServiceArrival•ServiceDeparture•ServiceChanged
![Page 50: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/50.jpg)
OSGi events - Services
•Available events•ServiceArrival•ServiceDeparture•ServiceChanged
void bindService(@Observes ServiceArrival evt) {}
![Page 51: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/51.jpg)
OSGi events - Services
•Available events•ServiceArrival•ServiceDeparture•ServiceChanged
void bindService(@Observes @Filter("(lang=US)") ServiceArrival evt) {}
![Page 52: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/52.jpg)
OSGi events - Services
•Available events•ServiceArrival•ServiceDeparture•ServiceChanged
void bindService(@Observes @Specification(MyService.class) ServiceArrival evt) {}
![Page 53: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/53.jpg)
OSGi events - Services
•Available events•ServiceArrival•ServiceDeparture•ServiceChanged
void bindService(@Observes @Specification(MyService.class) @Filter("(lang=US)") ServiceArrival evt) {}
![Page 54: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/54.jpg)
Inter-bundles events•Communication between bundles (managed by Weld-OSGi) with standard CDI events
Bundle A
Bundle C
Bundle B
Weld-OSGi
fire()
broadcast()
broadcast()
![Page 55: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/55.jpg)
Inter-bundles events@Inject Event<InterBundleEvent> event;event.fire(new InterBundleEvent("Hello bundles"));
![Page 56: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/56.jpg)
Inter-bundles events@Inject Event<InterBundleEvent> event;event.fire(new InterBundleEvent("Hello bundles"));
![Page 57: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/57.jpg)
Inter-bundles events@Inject Event<InterBundleEvent> event;event.fire(new InterBundleEvent("Hello bundles"));
public void listen(@Observes InterBundleEvent event) {}
![Page 58: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/58.jpg)
Inter-bundles events@Inject Event<InterBundleEvent> event;event.fire(new InterBundleEvent("Hello bundles"));
public void listen(@Observes @Sent InterBundleEvent event) {}
![Page 59: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/59.jpg)
Inter-bundles events@Inject Event<InterBundleEvent> event;event.fire(new InterBundleEvent("Hello bundles"));
public void listen(@Observes @Specification(String.class) InterBundleEvent event) {}
![Page 60: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/60.jpg)
Inter-bundles events@Inject Event<InterBundleEvent> event;event.fire(new InterBundleEvent("Hello bundles"));
public void listen(@Observes @Specification(String.class) @Sent InterBundleEvent event) {}
![Page 61: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/61.jpg)
Getting started !•Get Weld-OSGi =>•Write an OSGi bundle
•Maven module + maven-bundle-plugin•bnd file for manifest customization
•Empty beans.xml file in META-INF<dependency> <groupId>org.jboss.weld.osgi</groupId> <artifactId>weld-osgi-core-api</artifactId> <version>1.1.3-SNAPSHOT</version></dependency> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>1.0-SP4</version></dependency>
![Page 62: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/62.jpg)
Pros and cons•Pros
•CDI programming model•really simplify OSGi (service layer)
•don’t hide it though•fully compatible with existing OSGi bundles
•mixed app (legacy, weld-osgi)•one Weld container per bundle
•Cons•one Weld container per bundle•new API to learn
![Page 63: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/63.jpg)
Back to the future !
•Integration in Weld core (in progress)•Forge plugin
•integration with Weld-OSGi features•simplifying OSGi tests (arquillian OSGi)•generation of sample OSGi containers
•CDI Extension for hybrid Java EE app servers•using Weld-OSGi in Java EE apps•work in progress ;-)
•Integration with OSGi enterprise specs
![Page 64: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/64.jpg)
DemoMurphy’s law in action
![Page 65: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/65.jpg)
Demo : the story•Hotel booking webapp
•business partners with hotels•Avoid redeploying the app
•when new partner is added•using OSGi dynamism
•Provide an API to partners •Hotel POJO•HotelProvider service contract•partners will provide an OSGi bundle to deal with their booking system
![Page 66: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/66.jpg)
Conclusion
•Weld-OSGi is cool•let’s try it :-)
•Can help to change OSGi in people minds•Enlarge CDI action scope
•it’s not only about Java EE•Don’t hesitate to give us feedback and fill issues
![Page 67: Weld-OSGi - JBoss · PDF fileAgenda •CDI, the best part of Java EE 6 •OSGi, deep dive into modularity and dynamism •Meet Weld-OSGi •How does it work? •Features and programming](https://reader033.fdocuments.us/reader033/viewer/2022051508/5aaaa6477f8b9a7c188e58c1/html5/thumbnails/67.jpg)
Questions?