Top Banner
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]
34
Welcome message from author
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
Page 1: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.01

Enabling Modularization through OSGi and SpringDynamicModules

Mukul KumarSystem Software EngineerIndia Software LabsIBM, [email protected]

Page 2: Enabling modularization through OSGi and SpringDM

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

Page 3: Enabling modularization through OSGi and SpringDM

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

Page 4: Enabling modularization through OSGi and SpringDM

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)

Page 5: Enabling modularization through OSGi and SpringDM

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.

Page 6: Enabling modularization through OSGi and SpringDM

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.

Page 7: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.07

OSGi Framework Architecture

http://www.osgi.org

Page 8: Enabling modularization through OSGi and SpringDM

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

Page 9: Enabling modularization through OSGi and SpringDM

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

Page 10: Enabling modularization through OSGi and SpringDM

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.

Page 11: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.011

MANIFEST.MF

Page 12: Enabling modularization through OSGi and SpringDM

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.

Page 13: Enabling modularization through OSGi and SpringDM

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

Page 14: Enabling modularization through OSGi and SpringDM

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:

Page 15: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.015

DEMO

• Hello OSGi• Service Registration• Service Tracker

Page 16: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.016

Service Registration

• Exporting Service– MANIFEST.MF

• Export-Package: com.eclipseday.osgi.employee.service

• Importing Service– MANIFEST.MF

• Import-Package: com.eclipseday.osgi.employee.service

• This is also called Bundle Dependency Management, facilitated by OSGi Framework Module Layer.

Page 17: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.017

Service Registration

Page 18: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.018

Service Tracker

• Extend from class org.osgi.util.tracker.ServiceTracker

Page 19: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.019

Service Tracker

Page 20: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.020

Spring Dynamic Modules

• Spring Framework• What is SpringDM ?• Extender Pattern• Spring Application Context• Demo

Page 21: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.021

Spring Framework

• Lightweight Container– Manages through the bean lifecycle

• Provide support for IOC– Declarative support for beans wiring

• Provide support for AOP– Advice can be applied declaratively

• Integration with Web frameworks – Spring MVC, Struts, JSF…

• Spring JDBC support and ORM integration

Page 22: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.022

Without Dependency Injection

Page 23: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.023

With Dependency Injection

• Based on Interface, not coupled to implementation.• Reusable• Easily testable

Page 24: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.024

What is SpringDM ?

• SpringDM brings OSGi benefits in to Java EE application development.

• An OSGi Service can be imported and exported as if it were a Spring bean.

• SpringDM is also at the heart of springsource dm server.

• Download SpringDM from:http://www.springframework.org/osgi

Page 25: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.025

Extender Pattern: org.springframework.osgi.bundles.extender

• 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

Page 26: Enabling modularization through OSGi and SpringDM

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

Page 27: Enabling modularization through OSGi and SpringDM

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.

Page 28: Enabling modularization through OSGi and SpringDM

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

Page 29: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.029

Service Export

Service Import

Page 30: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.030

Using the Service

Page 31: Enabling modularization through OSGi and SpringDM

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.

Page 32: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.032

Q & A

Page 33: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.033

Further Queries, Feedback, Suggestions …

are welcome @

[email protected]

Page 34: Enabling modularization through OSGi and SpringDM

Enabling Modularization through OSGi and SpringDynamicModules | by Mukul Kumar | available under EPL v1.034

Thank you !