Spring DM - OSGi with Spring Framework<br />Patrick Baumgartner<br />AD Consultant<br />patrick.baumgartner@trivadis.com<b...
About me<br />Application Development Consultant<br />Web development with Spring Framework<br />OSGi with Spring DM &  Sp...
Agenda<br />What is OSGi?<br />The hard-coding Way<br />Declarative Services<br />Spring DM<br />Blueprint Services<br />D...
What is OSGi?<br />The OSGi framework is a module system for Java that implements a complete and dynamic component model, ...
Bundle Manifest – A special JAR<br />Spring Dynamic Modules - OSGi with Spring Framework<br />5<br />Image Source: http://...
Lifecycle of a Bundle<br />Spring Dynamic Modules - OSGi with Spring Framework<br />6<br />UPDATE REFRESH<br />INSTALL<br ...
Service Registry<br />Spring Dynamic Modules - OSGi with Spring Framework<br />7<br />SERVICE REGISTRY<br />REGISTER<br />...
OSGi – A Module System for Java<br />Clear boundaries<br />Dependencies<br />Metadata<br />Lifecycle<br />Service Registry...
The hard-coding Way<br />Spring Dynamic Modules - OSGi with Spring Framework<br />9<br />
The hard-coding Way – Register a Service<br />Register<br />	ServiceRegistration reg = bundleContext.registerService(	Chat...
The hard-coding Way – Consume a Service<br />Get Service<br />	ServiceReference ref = bundleContext.getServiceReference(	C...
The hard-coding Way – Consume a Service<br />Get Service with ServiceTracker<br />	ServiceTracker tracker = new ServiceTra...
Declarative Services<br />Spring Dynamic Modules - OSGi with Spring Framework<br />13<br />
Declarative Services<br />Declarative Services (DS) are part of OSGi R4 Specification – Service Compendium<br />Declaratio...
Declarative Services<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;component name=&quot;c...
Spring Dynamic Modules<br />Spring Dynamic Modules - OSGi with Spring Framework<br />16<br />
Spring Dynamic Modules<br />Integration of Springs Dependency Injection and OSGi<br />Formerly known as Spring OSGi<br />X...
The Spring DM Idea<br />Spring Dynamic Modules - OSGi with Spring Framework<br />18<br />APPLICATION CONTEXT<br />APPLICAT...
Spring Dynamic Modules<br />bundle-context.xml<br />&lt;beans … &gt;<br />&lt;bean id=&quot;twitterChatterBox&quot;class=&...
Spring Dynamic Modules<br />bundle-context-osgi.xml<br />&lt;beans:beans …&gt;<br />   &lt;service ref=&quot;twitterChatte...
Spring Dynamic Modules<br />Also supports listeners, filters, and collections<br />Dynamics are handled by the framework<b...
Blueprint Services<br />Spring Dynamic Modules - OSGi with Spring Framework<br />22<br />
Blueprint Services<br />Blueprint Services are a Standard since OSGi R 4.2 and based on the Ideas of Spring DM 1.0<br />Sp...
Blueprint Services<br />config.xml<br />&lt;blueprint xmlns=&quot;http://www.osgi.org/xmlns/blueprint/v1.0.0&quot;&gt;<br ...
Comparison Spring DM vs. Blueprint Services<br />Dependency Injection<br />Spring Dynamic Modules - OSGi with Spring Frame...
Comparison Spring DM vs. Blueprint Services<br />Component Lifecycle<br />Spring Dynamic Modules - OSGi with Spring Framew...
Demo<br />Spring Dynamic Modules - OSGi with Spring Framework<br />27<br />
Conclusion<br />Should I use Declarative Services, Spring DM or Blueprint Services?<br />DS, DM or Blueprint Services are ...
Thank you!<br />
Upcoming SlideShare
Loading in...5
×

OSGi with the Spring Framework

4,333

Published on

This slides covers the programmatic and declarative way to handle services in the OSGi container. Spring DM, Blueprint Services and Declared Services are presented in an overview.

Published in: Technology, Education

OSGi with the Spring Framework

  1. 1. Spring DM - OSGi with Spring Framework<br />Patrick Baumgartner<br />AD Consultant<br />patrick.baumgartner@trivadis.com<br />Zürich, 10.11.2009<br />
  2. 2. About me<br />Application Development Consultant<br />Web development with Spring Framework<br />OSGi with Spring DM & Spring Framework<br />Agile Software Development<br />Certified ScrumMaster<br />Co-Author of &quot;OSGi in der Praxis“<br />Spring Dynamic Modules - OSGi with Spring Framework<br />2<br />
  3. 3. Agenda<br />What is OSGi?<br />The hard-coding Way<br />Declarative Services<br />Spring DM<br />Blueprint Services<br />Demo<br />Conclusion<br />Spring Dynamic Modules - OSGi with Spring Framework<br />3<br />
  4. 4. What is OSGi?<br />The OSGi framework is a module system for Java that implements a complete and dynamic component model, something that does not exist in standalone Java/VM environments. […] (Source: Wikipedia)<br />Spring Dynamic Modules - OSGi with Spring Framework<br />4<br />
  5. 5. Bundle Manifest – A special JAR<br />Spring Dynamic Modules - OSGi with Spring Framework<br />5<br />Image Source: http://www.handycandy.co.uk<br />
  6. 6. Lifecycle of a Bundle<br />Spring Dynamic Modules - OSGi with Spring Framework<br />6<br />UPDATE REFRESH<br />INSTALL<br />STARTING<br />INSTALLED<br />START<br />UPDATE REFRESH<br />RESOLVE<br />EXCEPTION<br />ACTIVE<br />RESOLVED<br />UNINSTALL<br />UNINSTALL<br />STOP<br />STOPPING<br />UNINSTALLED<br />
  7. 7. Service Registry<br />Spring Dynamic Modules - OSGi with Spring Framework<br />7<br />SERVICE REGISTRY<br />REGISTER<br />DISCOVER<br />SERVICE DESCRIPTION<br />BIND<br />SERVICE CONSUMER<br />SERVICE PROVIDER<br />
  8. 8. OSGi – A Module System for Java<br />Clear boundaries<br />Dependencies<br />Metadata<br />Lifecycle<br />Service Registry<br />Spring Dynamic Modules - OSGi with Spring Framework<br />8<br />
  9. 9. The hard-coding Way<br />Spring Dynamic Modules - OSGi with Spring Framework<br />9<br />
  10. 10. The hard-coding Way – Register a Service<br />Register<br /> ServiceRegistration reg = bundleContext.registerService( ChatterBoxService.class.getName(), twitterChatterbox, properties);<br />Unregister<br /> reg.unregister();<br />Spring Dynamic Modules - OSGi with Spring Framework<br />10<br />
  11. 11. The hard-coding Way – Consume a Service<br />Get Service<br /> ServiceReference ref = bundleContext.getServiceReference( ChatterBoxService.class.getName());<br /> ChatterBoxService chatterbox = (ChatterBoxService)bundleContext.getService(ref);<br />Unget Service<br /> bundleContext.ungetService(ref);<br />Spring Dynamic Modules - OSGi with Spring Framework<br />11<br />
  12. 12. The hard-coding Way – Consume a Service<br />Get Service with ServiceTracker<br /> ServiceTracker tracker = new ServiceTracker( bundleContext, LogService.class.getName(), serviceTrackerCustomizer);<br /> tracker.open();LogService logService = (LogService) tracker.getService();<br />Spring Dynamic Modules - OSGi with Spring Framework<br />12<br />
  13. 13. Declarative Services<br />Spring Dynamic Modules - OSGi with Spring Framework<br />13<br />
  14. 14. Declarative Services<br />Declarative Services (DS) are part of OSGi R4 Specification – Service Compendium<br />Declaration of components in XML<br />OSGI-INF/&lt;component&gt;.xml<br />Components provides and depend on other Services <br />Components need a special bundle manifest headere.g. Service-Component: OSGI-INF/TwitterChatterBox.xml<br />Spring Dynamic Modules - OSGi with Spring Framework<br />14<br />
  15. 15. Declarative Services<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;component name=&quot;com.trivadis.chatterbox.twitter&quot;&gt;<br /> &lt;implementation class=&quot;com.trivadis.chatterbox.twitter.service.internal.TwitterChatterboxServiceImpl&quot;/&gt;<br /> &lt;service&gt;<br /> &lt;provide interface=&quot;com.trivadis.chatterbox.service.ChatterBoxService&quot;/&gt;<br /> &lt;/service&gt;<br />&lt;reference name=&quot;LOGGER&quot;<br /> interface=&quot;org.osgi.service.log.LogService&quot;<br /> cardinality=&quot;0..n&quot;<br /> policy=&quot;dynamic&quot;<br /> bind=&quot;addLogService&quot;<br /> unbind=&quot;removeLogService&quot;/&gt;<br />&lt;/component&gt;<br />Spring Dynamic Modules - OSGi with Spring Framework<br />15<br />
  16. 16. Spring Dynamic Modules<br />Spring Dynamic Modules - OSGi with Spring Framework<br />16<br />
  17. 17. Spring Dynamic Modules<br />Integration of Springs Dependency Injection and OSGi<br />Formerly known as Spring OSGi<br />XML files are located in META-INF/spring<br />Very similar approach compared to DS<br />Uses Spring DI for references to other Services and POJOs<br />(Almost) no dependencies on OSGi APIs<br />Components need a special bundle manifest headere.g. Spring-Context: META-INF/spring/bundle-context.xml, …<br />Spring Dynamic Modules - OSGi with Spring Framework<br />17<br />
  18. 18. The Spring DM Idea<br />Spring Dynamic Modules - OSGi with Spring Framework<br />18<br />APPLICATION CONTEXT<br />APPLICATION CONTEXT<br />APPLICATION CONTEXT<br />Imported Service<br />Exported Service<br />Spring Bean<br />SPRING & SPRING DM<br />OSGI FRAMEWORK<br />JVM<br />
  19. 19. Spring Dynamic Modules<br />bundle-context.xml<br />&lt;beans … &gt;<br />&lt;bean id=&quot;twitterChatterBox&quot;class=&quot;com.trivadis.chatterbox.twitter.service.internal.TwitterChatterboxServiceImpl&quot;&gt;<br /> &lt;property name=&quot;logService&quot; ref=&quot;logServiceOsgi&quot; /&gt;<br />&lt;/bean&gt;<br />&lt;/beans&gt;<br />Spring Dynamic Modules - OSGi with Spring Framework<br />19<br />
  20. 20. Spring Dynamic Modules<br />bundle-context-osgi.xml<br />&lt;beans:beans …&gt;<br /> &lt;service ref=&quot;twitterChatterBox&quot;<br /> interface=&quot;com.trivadis.chatterbox.service.ChatterBoxService&quot;/&gt;<br /> &lt;reference id=&quot;logServiceOsgi&quot; interface=&quot;org.osgi.service.log.LogService&quot; /&gt;<br />&lt;/beans:beans&gt;<br />Spring Dynamic Modules - OSGi with Spring Framework<br />20<br />
  21. 21. Spring Dynamic Modules<br />Also supports listeners, filters, and collections<br />Dynamics are handled by the framework<br />Proxies for service instances and collections<br />Method calls are buffered<br />Configurable timeouts<br />Annotation-Based Injection with @ServiceReference<br />Spring Dynamic Modules - OSGi with Spring Framework<br />21<br />
  22. 22. Blueprint Services<br />Spring Dynamic Modules - OSGi with Spring Framework<br />22<br />
  23. 23. Blueprint Services<br />Blueprint Services are a Standard since OSGi R 4.2 and based on the Ideas of Spring DM 1.0<br />Spring DM 2.0 is the Reference Implementation (RI)<br />Apache Aries Blueprint is an other implementation<br />Extremely similar to Spring DM but a standard<br />XML files are located in META-INF/blueprint<br />Components need a special bundle manifest headere.g. Bundle-Blueprint: OSGI-INF/blueprint/config.xml, …<br />Spring Dynamic Modules - OSGi with Spring Framework<br />23<br />
  24. 24. Blueprint Services<br />config.xml<br />&lt;blueprint xmlns=&quot;http://www.osgi.org/xmlns/blueprint/v1.0.0&quot;&gt;<br /> &lt;bean id=&quot;twitterChatterBox&quot; class=&quot;com.trivadis.chatterbox.twitter.service.internal.TwitterChatterboxServiceImpl&quot;&gt;<br /> &lt;property name=&quot;logService&quot; ref=&quot;logService&quot; /&gt;<br /> &lt;/bean&gt;<br /> &lt;service ref=&quot;twitterChatterBox&quot; <br /> interface=&quot;com.trivadis.chatterbox.service.ChatterBoxService&quot; /&gt;<br /> &lt;reference id=&quot;logService&quot;<br /> interface=&quot;org.osgi.service.log.LogService&quot; /&gt;<br />&lt;/blueprint&gt;<br />Spring Dynamic Modules - OSGi with Spring Framework<br />24<br />
  25. 25. Comparison Spring DM vs. Blueprint Services<br />Dependency Injection<br />Spring Dynamic Modules - OSGi with Spring Framework<br />25<br />Spring DM<br />Blueprint Services<br />Constructor Injection<br />Setter Injection<br />Field Injection<br />Method Injection <br />Arbitrary Method Injection<br />Autowiring<br />YES<br />YES<br />YES<br />YES<br />YES<br />YES<br />YES<br />YES<br />NO<br />NO<br />NO<br />NO<br />Source: Spring Dynamic Modules Reference Guide 2.0.0.M1<br />
  26. 26. Comparison Spring DM vs. Blueprint Services<br />Component Lifecycle<br />Spring Dynamic Modules - OSGi with Spring Framework<br />26<br />Spring DM<br />Blueprint Services<br />Lazy Initialization<br />Bean Scopes<br />Custom Bean Scopes<br />Built-in Callbacks<br />Custom Callbacks<br />Initialization Processing<br />YES<br />YES<br />YES<br />YES<br />YES<br />YES<br />YES<br />YES<br />NO<br />NO<br />YES<br />NO<br />Source: Spring Dynamic Modules Reference Guide 2.0.0.M1<br />
  27. 27. Demo<br />Spring Dynamic Modules - OSGi with Spring Framework<br />27<br />
  28. 28. Conclusion<br />Should I use Declarative Services, Spring DM or Blueprint Services?<br />DS, DM or Blueprint Services are better than program the life cycles and service infrastructure by hand<br />If you already use Spring, use Spring DM<br />If you want to use Standards use DS or Blueprint<br />To switch from Blueprint Services to Spring DM you need just a few changes in the XML configuration.<br />Spring Dynamic Modules - OSGi with Spring Framework<br />28<br />
  29. 29. Thank you!<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×