Intro To OSGi

63
www.bejug.org Introduction in OSGi 2009

description

OSGi has gained popularity over the last two years. The platform has some very interesting features like versioning, dynamic updates and it's service oriented nature. These characteristics however come with a price. A firm understanding of how and why OSGi works how it works, is a necessity if you plan on getting into OSGi.This talk will start with some basic principals on the java platform and will gradually move towards the OSGi infrastructure explaining the OSGi fundamentals. The following topics will be covered: * Classloading in OSGi * Lifecycle management of OSGi bundles * OSGi Service, the service registry and service composition modelsAfterwards, we will explain the generally accepted best practices and OSGi design patterns.

Transcript of Intro To OSGi

Page 1: Intro To OSGi

www.bejug.org

Introduction in OSGi

2009

Page 2: Intro To OSGi

www.bejug.org

Speaker’s qualifications

Lead architect & project manager @ Ebit 10 years experience in JSE/JEE

development Steering member of the Devoxx conference

and BeJUG Talks at Bejug & SpringOne Prince2 and Scrum certified

Page 3: Intro To OSGi

www.bejug.org

Imtech ICT Belgium

Page 4: Intro To OSGi

www.bejug.org

3 Companies – 1 Mindset

Infrastructure

Solutions and

Services

Development of

Software Solutions

JavaOpen Source

Data Warehousi

ng

BusinessIntelligenc

e

ICT Solutions and Services - Since 1996 Specific and in-depth knowledge – 78

employees

Page 5: Intro To OSGi

www.bejug.org

Agenda

Modularity in Java OSGi Basics OSGi Best Practices Conclusion

Page 6: Intro To OSGi

www.bejug.org

Modularity in Java

Modularity Matters– Component reuse– Consistency– Efficiency– Robustness– Maintainability– Scalability

Page 7: Intro To OSGi

www.bejug.org

Modularity in Java

Different approaches:– OSGi– HK2 (Apache Harmony)– JSR 277/294– JSR 232/291– Spar– Jigsaw project (only JDK)– …

Page 8: Intro To OSGi

www.bejug.org

Modularity in Java

Common problems in Java– JAR HELL– No versioning– The Classpath– Class visibility– Hot deployment– Write once, run anywhere

Page 9: Intro To OSGi

www.bejug.org

Agenda

Modularity in Java OSGi Basics OSGi Best Practices Conclusion

Page 10: Intro To OSGi

www.bejug.org

OSGi Basics

What is OSGi Classloading in OSGi The Bundle Lifecycle OSGi Services Demo

Page 11: Intro To OSGi

www.bejug.org

What is OSGi

OSGi is a service oriented platform On top of a unique classloading mechanism Enabling features like:

– Versioning– Encapsulation– Runtime Dynamics

Page 12: Intro To OSGi

www.bejug.org

What is OSGi

Page 13: Intro To OSGi

www.bejug.org

OSGi Basics

What is OSGi Classloading in OSGi

Welcome to the World ofClassNotFoundExceptions and

ClassDefNotFoundErrors

The Bundle Lifecycle OSGi Services Demo

Page 14: Intro To OSGi

www.bejug.org

Classloading in OSGi

Classloading 1 O 1– What is a class?

Compile time Runtime

– A classloader defines an isolated class space.

Page 15: Intro To OSGi

www.bejug.org

Classloading in OSGi

How can a classloader share it’s class space with other classloaders? Classloader hierarchy

• Linear hierarchy• Tree hierarchy

Classloader delegation model• Parent first delegation model• Child first delegation model

Page 16: Intro To OSGi

www.bejug.org

Classloading in OSGi

Default linear parent first delegation model

Page 17: Intro To OSGi

www.bejug.org

Classloading in OSGi

Linear parent first delegation model no versioning Breaking the linear structure to allow

versioning public static void main(String[] args) throws Exception {

URL[] urls = ((URLClassLoader)TestApp.class.getClassLoader()).getURLs();

URLClassLoader urlCL = new URLClassLoader(urls, null);

Class personClass = urlCL.loadClass("be.ebit.bejug.model.Person");

Object personInstance = personClass.newInstance();

Person p = (Person) personInstance;

}

Page 18: Intro To OSGi

www.bejug.org

Classloading in OSGi

Tree parent delegation model:

Page 19: Intro To OSGi

www.bejug.org

Classloading in OSGi

Moving towards OSGi: public static void main(String[] args) throws Exception {

//Path to model.jar version 1

URL[] urls = new URL[]{new URL(“<path>/modelv1.jar”)}

//Path to model.jar version 2

URL[] urls = new URL[]{new URL(“<path>/modelv2.jar”)}

URLClassLoader urlCL1 = new URLClassLoader(urls,null);

URLClassLoader urlCL2 = new URLClassLoader(urls,null);

Class personClassV1 = urlCL1.loadClass("be.ebit.springone.model.Person");

Class personClassV2 = urlCL2.loadClass("be.ebit.springone.model.Person");

}

Page 20: Intro To OSGi

www.bejug.org

Classloading in OSGi

Page 21: Intro To OSGi

www.bejug.org

Classloading in OSGi

How does OSGi fit in?– ModelV1.jar Bundle A– ModelV2.jar Bundle B Each bundle with its own ClassLoader (and thread)

The non-linear classloader structure allows versioning within one JVM.

Basic principle behind the versioning capabilities in OSGi.

Cfr. Servlet containers

Page 22: Intro To OSGi

www.bejug.org

Classloading in OSGi

Page 23: Intro To OSGi

www.bejug.org

Classloading in OSGi

The parent first delegation model does NOT allow communication between bundle classloaders– The FrontV1 bundle classloader cannot get a

Person class Solution?

– Extending the classloading mechanism

Page 24: Intro To OSGi

www.bejug.org

Classloading in OSGi

Page 25: Intro To OSGi

www.bejug.org

Classloading in OSGi

Bundle classloaders can delegate classloading to:– The parent classloader

parent first delegation– Other bundle classloaders

bundle delegation

The problem is choice:– Parent or bundle delegation?– If bundle delegation: to which bundle classloader?

Page 26: Intro To OSGi

www.bejug.org

Classloading in OSGi

OSGi Delegation Rules:– Parent Delegation Model is used if:

– Otherwise Bundle Delegation Model is used

Page 27: Intro To OSGi

www.bejug.org

Classloading in OSGi

The Bundle Delegation Model:– To which bundle classloader to delegate?– Metadata in the Bundle Manifest to the rescue:

1 Import/export-packages– what packages are needed by the bundle.– what packages are provided by the bundle.

2 Required bundles 3 Bundle classpath 4 Fragment bundles 5 Dynamic imports

Page 28: Intro To OSGi

www.bejug.org

Classloading in OSGi

How to wire the classloaders? Classloader infrastructure General wiring resolution rules:

– Ensures that only one version of a class is visible within a classloader:

class space consistency– Uses the highest version within the constraints

specified Version ranges Custom qualifiers

Page 29: Intro To OSGi

www.bejug.org

Classloading in OSGi

Page 30: Intro To OSGi

www.bejug.org

Classloading in OSGi

Constraint solving: an example

Page 31: Intro To OSGi

www.bejug.org

Classloading in OSGi

For each imported package a wire exists to the providing bundle classloader

The wires are calculated at bundle resolution This wiring is NOT static:

– Bundles can come and go– Dynamic updates

This wiring process is repeated (partially) with each refresh without a JVM restart.

Page 32: Intro To OSGi

www.bejug.org

Classloading in OSGi

Bundle Delegation Model (Runtime)

Page 33: Intro To OSGi

www.bejug.org

Classloading in OSGi

The total picture:

Page 34: Intro To OSGi

www.bejug.org

Classloading in OSGi

Fine-tuning the choice of delegation model:– Bootdelegation:

Additional classes to be loaded by the parent classloader System property: org.osgi.framework.bootdelegation No versioning

– System packages: Exported by the system bundle (bundle 0) System property: org.osgi.framework.system.packages Versioning Takes part in the wiring process

Last option is preferred

Page 35: Intro To OSGi

www.bejug.org

Classloading in OSGi

Page 36: Intro To OSGi

www.bejug.org

Classloading in OSGi

Parent first delegation model:– No versioning– Static ‘wiring’– See-all approach

Bundle delegation model:– Versioning– By default, only the exports are visible– Support for dynamic updates/dynamic wiring

Page 37: Intro To OSGi

www.bejug.org

OSGi Basics

What is OSGi Classloading in OSGi The Bundle Lifecycle OSGi Services Demo

Page 38: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

Page 39: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

The Bundle Activator– Start() & Stop()– Resource Mgnt, Thread Mgnt, Services Lifecycle Mgnt

Events:– Bundle events– Framework events

The Bundle Context– Environment information– Installing bundles– Installing services

Page 40: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

Bundle updates, uninstalls & refreshes– Update: migrate from one version to another

New exported packages are made available Old exported packages remain when used

– Uninstall New exported packages are made avialable Old exported packages remain when used

– RefreshPackages: The effective update & removal A package dependency graph is created The exporting bundle & all dependent bundles are stopped

and restarted.

Page 41: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

Uninstall Model V1 Bundle

Page 42: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

Model V1 Bundle is marked for deleting

Page 43: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

Install & Start SecondFrontV1 Bundle – (imports be.ebit.bejug.model)

Page 44: Intro To OSGi

www.bejug.org

The Bundle Lifecycle

RefreshPackages (or restart framework)– Resolution (wiring) process is redone

Page 45: Intro To OSGi

www.bejug.org

OSGi Basics

What is OSGi Classloading in OSGi The Bundle Lifecycle OSGi Services Demo

Page 46: Intro To OSGi

www.bejug.org

OSGi Services

Page 47: Intro To OSGi

www.bejug.org

OSGi Services

Services Service interface Service implementation

ServiceFactories ServiceRegistry

Registration/Unregistration ServiceReference: search base

Service Events/Listeners

Page 48: Intro To OSGi

www.bejug.org

OSGi Services

Service lookup is text-based– Filter (interface + criteria)

Services can come and go Stale references

Services are fully loaded before registration Multi-threading

OSGi development is NOT trivial!

Page 49: Intro To OSGi

www.bejug.org

OSGi Services

Extension Points:– Not part of the specification– Extension points and extensions– One-to-many– Lazy loading– Less support for service dynamics (restart of

eclipse) Extension Points doesn’t solve all problems!

Page 50: Intro To OSGi

www.bejug.org

OSGi Services

Easing OSGi Component Development:– Declarative Services– Spring DM– iPOJO

Seperation of responsibilities:– Implementation of the service– Publication of the service

Two component models in the specification:– Declarative Services – RFC 124 (Spring DM-based).

Page 51: Intro To OSGi

www.bejug.org

OSGi Services

Common characteristics:– Declaration of services– Creation of services externalized (outside the

bundle)– Lazy loading– Service composition– Service dependencies & lifecycle management– Dynamic dependencies

Page 52: Intro To OSGi

www.bejug.org

OSGi Basics

What is OSGi Classloading in OSGi The Bundle Lifecycle OSGi Services Demo

Page 53: Intro To OSGi

www.bejug.org

Demo

Page 54: Intro To OSGi

www.bejug.org

Demo

Page 55: Intro To OSGi

www.bejug.org

Agenda

Modularity in Java OSGi Basics OSGi Best Practices Conclusion

Page 56: Intro To OSGi

www.bejug.org

OSGi Patterns & Best Practices

OSGi Patterns– Extender Pattern– Configuration Fragment Bundles– Whiteboard Pattern

Page 57: Intro To OSGi

www.bejug.org

Extender Pattern

Page 58: Intro To OSGi

www.bejug.org

Configuration Fragment Bundles

Examples: – Log4J configuration, – Custom configuration of the spring extender bundle

Page 59: Intro To OSGi

www.bejug.org

Whiteboard Pattern

Page 60: Intro To OSGi

www.bejug.org

OSGi Patterns & Best Practices

Best Practices:– Use imports instead of the parent classloader

(except java.*)– Minimize dependencies– Hide implementation details– Start ordering dependencies– Thread safety– Framework Callbacks– Classloader hierarchy dependencies– OSGi Framework coupling

Page 61: Intro To OSGi

www.bejug.org

Conclusion

OSGi is a platform full of potential:– Enhances encapsulation– Versioning– Runtime dynamics– Service-oriented

A different mindset is needed!

Page 62: Intro To OSGi

www.bejug.org

Q&A

– http://belgium.osgiusers.org– www.ouforum.be

Page 63: Intro To OSGi

www.bejug.org

Links

http://www.osgi.org http://www.springsource.org/osgi http://www.dynamicjava.org http://belgium.osgiusers.org