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