osgi with the spring framework
DESCRIPTION
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.TRANSCRIPT
Baden Basel Bern Brugg Lausanne Zürich Düsseldorf Frankfurt/M. Freiburg i. Br. Hamburg München Stuttgart Wien
Spring DM - OSGi with Spring Framework
Patrick Baumgartner
AD Consultant
Zürich, 10.11.2009
© 2009
Spring Dynamic Modules - OSGi with Spring Framework2
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“
© 2009
Spring Dynamic Modules - OSGi with Spring Framework3
Daten sind immer im Spiel.
Agenda
What is OSGi?
The hard-coding Way
Declarative Services
Spring DM
Blueprint Services
Demo
Conclusion
© 2009
Spring Dynamic Modules - OSGi with Spring Framework4
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)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework5
Bundle Manifest – A special JAR
Image Source: http://www.handycandy.co.uk
© 2009
Spring Dynamic Modules - OSGi with Spring Framework6
Lifecycle of a Bundle
INSTALL
STARTING
ACTIVE
STOPPING
INSTALLED
RESOLVED
UNINSTALLED
ST
OP
UN
INS
TAL
L UP
DA
TE
R
EF
RE
SH
UN
INS
TAL
L
RE
SO
LVE
START
UPDATE REFRESH
EXCEPTION
© 2009
Spring Dynamic Modules - OSGi with Spring Framework7
Service Registry
SERVICE REGISTRY
SERVICE CONSUMER
SERVICE PROVIDER
BIND
DISC
OVERR
EGIS
TER
SERVICE DESCRIPTION
© 2009
Spring Dynamic Modules - OSGi with Spring Framework8
OSGi – A Module System for Java
Clear boundaries
Dependencies
Metadata
Lifecycle
Service Registry
© 2009
Spring Dynamic Modules - OSGi with Spring Framework9
The hard-coding Way
© 2009
Spring Dynamic Modules - OSGi with Spring Framework10
The hard-coding Way – Register a Service
Register
ServiceRegistration reg = bundleContext.registerService(ChatterBoxService.class.getName(), twitterChatterbox, properties);
Unregister
reg.unregister();
© 2009
Spring Dynamic Modules - OSGi with Spring Framework11
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);
© 2009
Spring Dynamic Modules - OSGi with Spring Framework12
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();
© 2009
Spring Dynamic Modules - OSGi with Spring Framework13
Declarative Services
© 2009
Spring Dynamic Modules - OSGi with Spring Framework14
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 header
e.g. Service-Component: OSGI-INF/TwitterChatterBox.xml
© 2009
Spring Dynamic Modules - OSGi with Spring Framework15
Declarative Services
<?xml version="1.0" encoding="UTF-8"?><component name="com.trivadis.chatterbox.twitter"> <implementation
class="com.trivadis.chatterbox.twitter.service.internal.TwitterChatterboxServiceImpl"/>
<service> <provide
interface="com.trivadis.chatterbox.service.ChatterBoxService"/> </service><reference name="LOGGER" interface="org.osgi.service.log.LogService" cardinality="0..n" policy="dynamic" bind="addLogService" unbind="removeLogService"/></component>
© 2009
Spring Dynamic Modules - OSGi with Spring Framework16
Spring Dynamic Modules
© 2009
Spring Dynamic Modules - OSGi with Spring Framework17
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, …
© 2009
Spring Dynamic Modules - OSGi with Spring Framework18
The Spring DM Idea
APPLICATION CONTEXT
Imported Service
Exported Service
Spring Bean
APPLICATION CONTEXT
APPLICATION CONTEXT
SPRING & SPRING DM
OSGI FRAMEWORK
JVM
© 2009
Spring Dynamic Modules - OSGi with Spring Framework19
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>
© 2009
Spring Dynamic Modules - OSGi with Spring Framework20
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>
© 2009
Spring Dynamic Modules - OSGi with Spring Framework21
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
© 2009
Spring Dynamic Modules - OSGi with Spring Framework22
Blueprint Services
© 2009
Spring Dynamic Modules - OSGi with Spring Framework23
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, …
© 2009
Spring Dynamic Modules - OSGi with Spring Framework24
Blueprint Services
config.xml
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="twitterChatterBox"
class="com.trivadis.chatterbox.twitter.service.internal.TwitterChatterboxServiceImpl">
<property name="logService" ref="logService" /> </bean>
<service ref="twitterChatterBox" interface="com.trivadis.chatterbox.service.ChatterBoxService" />
<reference id="logService" interface="org.osgi.service.log.LogService" /></blueprint>
© 2009
Spring Dynamic Modules - OSGi with Spring Framework25
Comparison Spring DM vs. Blueprint Services
Dependency Injection
Constructor Injection
Setter Injection
Field Injection
Method Injection
Arbitrary Method Injection
Autowiring
Spring DM
YES
YES
YES
YES
YES
YES
Blueprint Services
YES
YES
NO
NO
NO
NO
Source: Spring Dynamic Modules Reference Guide 2.0.0.M1
© 2009
Spring Dynamic Modules - OSGi with Spring Framework26
Comparison Spring DM vs. Blueprint Services
Component Lifecycle
Lazy Initialization
Bean Scopes
Custom Bean Scopes
Built-in Callbacks
Custom Callbacks
Initialization Processing
Spring DM
YES
YES
YES
YES
YES
YES
Blueprint Services
YES
YES
NO
NO
YES
NO
Source: Spring Dynamic Modules Reference Guide 2.0.0.M1
© 2009
Spring Dynamic Modules - OSGi with Spring Framework27
Demo
© 2009
Spring Dynamic Modules - OSGi with Spring Framework28
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.
?www.trivadis.com
Baden Basel Bern Brugg Lausanne Zürich Düsseldorf Frankfurt/M. Freiburg i. Br. Hamburg München Stuttgart Wien
Thank you!