OSGi with the Spring Framework
-
Upload
patrick-baumgartner -
Category
Technology
-
view
4.189 -
download
5
description
Transcript of OSGi with the Spring Framework
![Page 1: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/1.jpg)
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
![Page 2: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/2.jpg)
© 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“
![Page 3: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/3.jpg)
© 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
![Page 4: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/4.jpg)
© 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)
![Page 5: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/5.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework5
Bundle Manifest – A special JAR
Image Source: http://www.handycandy.co.uk
![Page 6: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/6.jpg)
© 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
![Page 7: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/7.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework7
Service Registry
SERVICE REGISTRY
SERVICE CONSUMER
SERVICE PROVIDER
BIND
DISC
OVERR
EGIS
TER
SERVICE DESCRIPTION
![Page 8: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/8.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework8
OSGi – A Module System for Java
Clear boundaries
Dependencies
Metadata
Lifecycle
Service Registry
![Page 9: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/9.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework9
The hard-coding Way
![Page 10: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/10.jpg)
© 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();
![Page 11: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/11.jpg)
© 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);
![Page 12: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/12.jpg)
© 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();
![Page 13: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/13.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework13
Declarative Services
![Page 14: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/14.jpg)
© 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
![Page 15: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/15.jpg)
© 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>
![Page 16: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/16.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework16
Spring Dynamic Modules
![Page 17: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/17.jpg)
© 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, …
![Page 18: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/18.jpg)
© 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
![Page 19: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/19.jpg)
© 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>
![Page 20: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/20.jpg)
© 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>
![Page 21: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/21.jpg)
© 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
![Page 22: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/22.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework22
Blueprint Services
![Page 23: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/23.jpg)
© 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, …
![Page 24: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/24.jpg)
© 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>
![Page 25: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/25.jpg)
© 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
![Page 26: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/26.jpg)
© 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
![Page 27: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/27.jpg)
© 2009
Spring Dynamic Modules - OSGi with Spring Framework27
Demo
![Page 28: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/28.jpg)
© 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.
![Page 29: OSGi with the Spring Framework](https://reader036.fdocuments.us/reader036/viewer/2022062303/554f4398b4c90572088b54a1/html5/thumbnails/29.jpg)
?www.trivadis.com
Baden Basel Bern Brugg Lausanne Zürich Düsseldorf Frankfurt/M. Freiburg i. Br. Hamburg München Stuttgart Wien
Thank you!