Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.0 1 Enabling Modularization through OSGi and SpringDynamicModules Mukul Kumar System Software Engineer India Software Labs IBM, INDIA [email protected]
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.01
Enabling Modularization through OSGi and SpringDynamicModules
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.02
Agenda
• OSGi as Service Platform• Spring Dynamic Modules• Summary• Q & A
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.03
OSGi as Service Platform
• Limitations with conventional Java applications• What is OSGi ?• OSGi Framework Architecture• SOA Infusion• Demo
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.04
Limitations with conventional Java applications
JAR Hell (limitations caused by JAR files)
• No Runtime boundaries between the JAR files.• Information hiding only at class level but not at modular
level.• No versioning• Classloading• Dependency on other JAR(s)
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.05
JSR 277 – Java Module System
• Module is a deployment abstraction.
• Defines a distribution format (i.e. Java Module) and a repository for collections of Java code and related resources. It also defines the discovery, loading, and integrity check mechanism at runtime.
• JSR 277 defines a static module system. There is no dynamic loading and unloading of modules/bundles, any change will require to reboot the JVM.
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.06
What is OSGi ?
• OSGi stands for Open Service Gateway Initiative.
• OSGi is Dynamic Module System for Java.
• OSGi framework implements a complete service oriented and dynamic module system.
• OSGi specification also enables modules/bundles to hide their implementation from other modules/bundles.
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.07
OSGi Framework Architecture
http://www.osgi.org
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.08
Module Layer
• Packaging: Reusable bundles contains application code and libraries.
• Code Security: Supports code hiding and explicit sharing.
• Versioning: Different versions of same JAR can be used in single VM.
• Classloading: Advance classloading schemes.
bundle bundle
bundle
bundle
bundle
bundle
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.09
Life Cycle Layer
• Offers an API for bundle life cycle management that manages modules at runtime.
• These dynamics are generally not part of applications and fully protected with security mechanism.
installed
resolved
uninstalled
active
stopping
starting
start
stop
install
uninstall
uninstall
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.010
Bundles
• A bundle mainly consists of a group of Java classes and a metadata descriptor, MANIFEST.MF file.
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.011
MANIFEST.MF
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.012
Activator
• Bundle is started by BundleActivator class.• Interface has 2 methods.
– start(): initialize and return immediately– stop(): clean up
• BundleContext gives access to framework functions.• Framework functions enables us to start/stop the group
of applications.
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.013
Service Layer
• Provides an In-VM service Model– Discover services
based on their interface or properties.
– Bind to one or more services.
OSGi Service Registry
Bundle A Bundle B
registers
get
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.014
SOA Infusion
• Based on contract (interface)
• Separate the contract form implementations.
• Dynamically discover and bind alternate implementations inside a Java VM.
• Components are reusable.
Service Contract
Componentprovides
uses
courtesy:
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.015
DEMO
• Hello OSGi• Service Registration• Service Tracker
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.016
• Checks for the spring enabled bundles to be installed and create application/module context for those bundles.
• Spring enabled bundle:– META-INF/spring/*.xml– Spring-Context header in
MANIFEST.MF
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.026
JVM
OSGi Service Platform
Framework Bundles Application Bundles
Spring Application Contexts
SpringDM Extender Bundle Spring enabled Bundles
Spring bean
Service
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.027
Spring Application Context
• For each spring enabled bundle, Spring Application Context (or Module Context) will be created.
• Application Context will be created when bundle is started and destroyed when bundle is stopped.
• A Spring Bean can be published as if it were an OSGi Service.
• An OSGi Service can be imported as if it were a Spring Bean in that Application Context.
• Spring Beans/ OSGi Services communicate via OSGi Service Registry.
• Saves interaction with low level OSGi Framework API.
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.028
DEMO
• Exporting a Spring Bean as OSGi Service• Importing an OSGi Service as Spring Bean• Using Service/Bean
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.029
Service Export
Service Import
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.030
Using the Service
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.031
Summary
• OSGi benefits modular application development via:– Versioning– Modularity– Life Cycle Management– Service Registry
• SpringDM supports modular application development by bringing OSGi benefits and Spring IOC support together.– No need to work with low level OSGi framework API.– Manages the service dynamics internally.
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.032
Q & A
Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.033