best practices for enterprise osgi applications - emily jiang

32
© 2010 IBM Corporation OSGi Application Best Practices Emily Jiang, [email protected] WebSphere Application Server OSGi Developer, Apache Aries Committer

Upload: mfrancis

Post on 19-Aug-2015

9 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Best Practices for Enterprise OSGi Applications - Emily Jiang

© 2010 IBM Corporation

OSGi Application Best Practices

Emily Jiang, [email protected]

WebSphere Application Server OSGi Developer, Apache Aries Committer

Page 2: Best Practices for Enterprise OSGi Applications - Emily Jiang

© 2011 IBM Corporation

2

AGENDA

> Why OSGi?

> What is OSGi?

> How to best use OSGi?

Page 3: Best Practices for Enterprise OSGi Applications - Emily Jiang

© 2011 IBM CorporationIBM ConfidentialOctober 18, 2012

33

> Jars have no modularization characteristics– No “jar scoped” access modifiers.– No means for a jar to declare its

dependencies.– No versioning.

JarJar

PackagePackage

ClassClassClassClassClassClass

PackagePackage

ClassClassClassClassClassClass

PackagePackage

ClassClassClassClassClassClass

Modularization in Java

Page 4: Best Practices for Enterprise OSGi Applications - Emily Jiang

4

Problems with Global Java ClassPath

J a va V M

lo g 4 j

ba rc o de 4 j

a xis

ba t ik

c o m m o ns

de rby

fo p

e zm o rph

fre e m a rke r

httpunit

ja ka rta

jc l

js o n

jdbm

jdo m

je nks

jpo s 1 8

jytho n

lo o ks

luc e ne

m a il

m x4 j

na m ing

je tty

po i

re s o lve r

ro m e

s e ria lize r

s e rv le ts

to m c a t

ve lo c ity

w s -c o m m o ns

xa la n

w s dl4 j

xe rc e s

xm lg ra phic s

xm lrpc

xm la pis

..

g e ro nim o

bs h

bs f

g uia pp

hhfa c ility

m a nufa c t .

m a rke ting

m ine rva

a c c o unting

a s s e tm a int

ba s e

bi

c a ta lina

c o m m o n

o a g is

o rde r

e ba y

c o nte nt

da ta file

e c o m m e rc e

e ntity

g o o g le b a s e

o fbiz

w idg e t

m inila ng

pa rty

po s .

pro duc t

w o rke ffo rt

w o rkflo w

s unjc e _pro v.

plug in

js s e

jc e

rt

dns ns

..

…C la s s

N o tF o und

E xc e ptio n

B e g inB e g inHe reHe re

Page 5: Best Practices for Enterprise OSGi Applications - Emily Jiang

Problems with EARs/WARs

Enterprise Applications have isolated classpaths but…

> No Sharing– Common libraries/frameworks in apps

and memory

> Version conflicts

webA.war

WEB-INF/classes/servletA.class

WEB-INF/lib/spring.jar

WEB-INF/lib/commons-logging.jar

WEB-INF/lib/junit.jar…webB.war

WEB-INF/classes/servletB.class

WEB-INF/lib/spring.jar

WEB-INF/lib/commons-logging.jar

WEB-INF/lib/junit.jar…webC.war

WEB-INF/classes/servletC.class

WEB-INF/lib/spring.jar

WEB-INF/lib/commons-logging.jar

WEB-INF/lib/junit.jar…

plankton.v1

plankton.v2

Page 6: Best Practices for Enterprise OSGi Applications - Emily Jiang

AGENDA

> Why OSGi?

> What is OSGi?

> How to best use OSGi?

Page 7: Best Practices for Enterprise OSGi Applications - Emily Jiang

© 2011 IBM Corporation7

JarJar

77

What is OSGi?

“The dynamic module system for Java”> Mature 10-year old technology> Governed by OSGi Alliance: http://www.osgi.org> Used inside just about all Java-based middleware

– IBM WebSphere, Oracle WebLogic, Red Hat JBoss, Sun GlassFish, Paremus Service Fabric, Eclipse Platform, Apache Geronimo, (non-exhaustive list)http://www.osgi.org/wiki/uploads/News/2008_09_16_worldwide_market.pdf

Package

ClassClassClassClassClassClass

ClassClassClassClassClassClassPackage

ClassClassClassClassClassClass

ClassClassClassClassClassClass

Explicit exports

Explicit dependencies

Page 8: Best Practices for Enterprise OSGi Applications - Emily Jiang

8 31/10/12

OSGi Bundles and Class Loading

OSGi Bundle – A jar containing:Classes and resources.OSGi Bundle manifest.

What’s in the manifest:Bundle-Version: Multiple versions of

bundles can live concurrently.Import-Package: What packages

from other bundles does this bundle depend upon?

Export-Package: What packages from this bundle are visible and reusable outside of the bundle?

Class LoadingEach bundle has its own loader.No flat or monolithic classpath.Class sharing and visibility decided

by declarative dependencies, not by class loader hierarchies.

OSGi framework works out the dependencies including versions.

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: MyService bundleBundle-SymbolicName: com.sample.myserviceBundle-Version: 1.0.0Bundle-Activator: com.sample.myservice.ActivatorImport-Package: com.something.i.need;version="1.1.2"Export-Package: com.myservice.api;version="1.0.0"

Bundle

Page 9: Best Practices for Enterprise OSGi Applications - Emily Jiang

9

OSGi Bundle

Bundles have a dynamic lifecycle

Can come and go independently

Bundle

Page 10: Best Practices for Enterprise OSGi Applications - Emily Jiang

• Service• An object associated with a list of classes (usually interfaces) it

provides• Dynamic (can come and go), framed by bundle lifecycle• Services are the primary means of collaboration between bundles.

S

OSGi Service Registry

Page 11: Best Practices for Enterprise OSGi Applications - Emily Jiang

AGENDA

> Why OSGi?

> What is OSGi?

> How to best use OSGi?

Page 12: Best Practices for Enterprise OSGi Applications - Emily Jiang

12 31 October 2012

BP1 - Use Import-Package not Require-Bundle Require-Bundle

– Tightly coupled with a particular bundle with the specified symbolic name and version

– High coupling between bundles– Import all packages – Bundle version management

• Import-Package – Can wire to any bundles exporting the specified package– Loose coupling between bundles– Only import the package you need– Package version management

MANIFEST.MF

...

Require-Bundle: com.ibm.ws.service;bundle-version=2.0.0

MANIFEST.MF

...

Import-Package: com.ibm.ws.service.api;version=2.0.0

Page 13: Best Practices for Enterprise OSGi Applications - Emily Jiang

13 31 October 2012

BP2 - Avoid split packages

• Split package– A package is exported by two bundles at the same version and

the set of classes provided by each bundle differs.

• Why?– Leads to the use of Require-Bundle, compromising the extent to

which systems using the bundles can be extended and maintained.

• How?– Keep all of the classes from any one package in a single bundle

Page 14: Best Practices for Enterprise OSGi Applications - Emily Jiang

14 31 October 2012

BP2 - Split Package examples

API A

org.hal.api 1.0.0

API B

org.hal.api 1.0.0

Implementation C

org.hal.a 1.0.0

Bundle C has to use 'Require-Bundle' to

ensure that it has classes from both parts of the API

Figure 1. Consumers of split packages need to use the Require-Bundle header

Page 15: Best Practices for Enterprise OSGi Applications - Emily Jiang

15 31 October 2012

BP2 - Split Package examples

API A

org.hal.api 1.0.0

Implementation C

org.hal.a 1.0.0

Figure 2. A complete package exported from a single bundle maintains high bundle cohesion

Page 16: Best Practices for Enterprise OSGi Applications - Emily Jiang

16 31 October 2012

BP3 - Version bundles and packages

• What is semantic versioning?– Uses a major.minor.micro.qualifier numbering scheme

• Major - Packages with versions that have different major parts are not compatible both for providers as well as consumers.

• Minor – API enhancement, e.g. adding methods to an API• Micro – bug fixing• Qualifier – identifier such as timestamp

– Changes in major: a binary incompatible, minor: enhanced API, micro: no API changes

• Why?– Clients can protect themselves against API changes that might break

them.

Page 17: Best Practices for Enterprise OSGi Applications - Emily Jiang

17 31 October 2012

BP3 - Version bundles and packages examples

Figure 3: A client and an implementation can use either of two equivalently versioned packages

Implementation A

Imports:org.hal.a [1.0, 1.1)

API A

org.hal.a 1.0.0

API Borg.hal.a 1.0.0

Client A

Imports:org.hal.a [1.0, 2.0)

API Borg.hal.a 1.1.0

API A

org.hal.a 1.0.0

Client A

Imports:org.hal.a [1.0, 2.0)

Client B

Imports:org.hal.a [1.1, 2.0)

Implementation B

Imports:org.hal.a [1.1, 1.2)

Implementation A

Imports:org.hal.a [1.0, 1.1)

Figure 4: How a client and implementation are affected differently by a minor API version change

Page 18: Best Practices for Enterprise OSGi Applications - Emily Jiang

18 31 October 2012

BP3 - Version bundles and packages examples

Figure 5. How clients and implementations are similarly affected by a major API version change

org.hal.a 1.0.0

org.hal.a 2.0.0

API B

Client A

Imports:org.hal.a [1.0, 2.0)

Client B

Imports:org.hal.a [2.0, 3.0)

Implementation B

Imports:org.hal.a [2.0, 2.1)

Implementation A

Imports:org.hal.a [1.0, 1.1)

API A

Page 19: Best Practices for Enterprise OSGi Applications - Emily Jiang

19 31 October 2012

BP4 - Separate API from Implementations

• Why?– Great flexibility– Many implementation bundles → enable more services provided– Reduce package dependencies → reduce circular

dependencies

• How?– Put API classes in one bundle– Put implementation classes in a separate bundle

Page 20: Best Practices for Enterprise OSGi Applications - Emily Jiang

20 31 October 2012

BP4 - Separate API and implementation examples

Figure 6. Badly designed provider bundle where the API and implementation classes are in the same bundle

API + Implementation

org.hal.myapi

org.hal.a.impl

Client

org.hal.b.client

Both API and implementation packages

imported by client

Page 21: Best Practices for Enterprise OSGi Applications - Emily Jiang

21 31 October 2012

BP5 - Share services not implementations• Use the OSGi service registry to construct instances• Why?

– Able to obtain an instance of an implementation without knowing which one

– Achieve a loosely coupling of client, API and implementation

• How?

– Register an instance of the API interface in the OSGi service registry

– Register an implementation of the OSGi ServiceFactory interface in the OSGi service registry.

s

Page 22: Best Practices for Enterprise OSGi Applications - Emily Jiang

22 31 October 2012

BP4 & 5 - examples

API

org.hal.myapi

Client

org.hal.b.client

API and implementation packages in different bundles

but still imported by client

Implementation

org.hal.a.impl

Figure 7. Badly designed provider bundles where the API and implementation classes have been separated

Page 23: Best Practices for Enterprise OSGi Applications - Emily Jiang

23 31 October 2012

BP4 & 5 - examples

API

org.hal.myapi

Client

org.hal.b.client

Client and implementation in separate bundles, both import API. Client uses implementation through a service

defined by the API.

Implementation

org.hal.a.impl

Figure 8: Well designed provider bundles where the API and implementation classes have been separated

Page 24: Best Practices for Enterprise OSGi Applications - Emily Jiang

24 31 October 2012

BP6 – Make Bundles Loosely Coupled & Highly Cohesive

'Hairball' effect – one bundle has many package

dependencies

org.hal.log.impl

Implementation

org.hal.log.impl

SomeotherAPI

DBAPI

TransactionsAPI

FileSystemAPI

JPAAPI

API

org.hal.log.api org.hal.fslog.impl

Figure 9. Poorly purposed system where a single bundle provides multiple implementations of the same API

Page 25: Best Practices for Enterprise OSGi Applications - Emily Jiang

25 31 October 2012

BP6 – Make Bundles Loosely Coupled & Highly Cohesive

Implementation

org.hal.DBlog.implDBAPI

API

org.hal.log.api

Implementation

org.hal.fslog.impl

FileSystemAPI

Implementation

org.hal.DBlog.impl

DBAPI

Implementation

org.hal.DBlog.impl

DBAPI

Figure 10. A well purposed system where each implementation of an API is provided by a separate bundle

Page 26: Best Practices for Enterprise OSGi Applications - Emily Jiang

26 31 October 2012

Using services can be hard!

@Overridepublic void serviceChanged(ServiceEvent event) {ServiceReference ref = event.getServiceReference();

if (ls.get() == null && event.getType() == ServiceEvent.REGISTERED) {

ls.set((LogService) ctx.getService(ref));} else if (ls.get() != null && event.getType() ==

ServiceEvent.UNREGISTERING &&ref == lr.get()) {ref = ctx.getServiceReference(LogService.class.getName());if (ref != null) {ls.set((LogService) ctx.getService(ref));lr.set(ref);}}}

private BundleContext ctx;

private AtomicReference<LogService> ls = new AtomicReference<LogService>();private AtomicReference<ServiceReference> lr = new AtomicReference<ServiceReference>();

public void start(BundleContext ctx) throws InvalidSyntaxException{this.ctx = ctx;ctx.addServiceListener(this, "(objectClass=org.osgi.service.log.LogService)");ServiceReference ref = ctx.getServiceReference(LogService.class.getName());if (ref != null) {ls.set((LogService) ctx.getService(ref));lr.set(ref);}}

Page 27: Best Practices for Enterprise OSGi Applications - Emily Jiang

27 31 October 2012

BP7 - Use Blueprint

• Specifies a Dependency Injection container, standardizing established Spring conventions

• Configuration and dependencies declared in XML “module blueprint”, which is a standardization of Spring “application context” XML.

– Extended for OSGi: publishes and consumes components as OSGi services• Simplifies unit test outside either Java EE or OSGi r/t.• The Blueprint DI container is a part of the server runtime (compared

to the Spring container which is part of the application.)

dependencies injectedpublishesservice consumes

service

A static assembly and configuration of

components (POJOs)Blueprint bundle

OSGI-INF/blueprint/blueprint.xml

Page 28: Best Practices for Enterprise OSGi Applications - Emily Jiang

28 31 October 2012

BP7 - Blueprint service-bundle examples

public interface BillingService {void bill(Order o);

}

Billing

<blueprint> <service ref=”service” interface =

”org.example.bill.BillingService” /> <bean id=”service” scope=”prototype”

class=”org.example.bill.impl.BillingServiceImpl” /></blueprint>

Billing service bundle

-“prototype” scope indicates a new instance is created by the container for each use.-“singleton” scope is the default.

Page 29: Best Practices for Enterprise OSGi Applications - Emily Jiang

29 31 October 2012

BP7- Blueprint client-bundle examples

public class ShopImpl {private BillingService billingService;void setBillingService(BillingService srv) {

billingService = srv;}

void process(Order o) {billingService.bill(o);}

}

e-Commerce

<blueprint> <bean id=”shop” class=”org.example.ecomm.ShopImpl”> <property name=”billingService” ref=”billingService” /> </bean> <reference id=”billingService”

interface=”org.example.bill.BillingService” /></blueprint>

e-Commerce bundle

-injected service reference-service can change over time-can be temporarily absent without the bundle caring-managed by Blueprint container

Page 30: Best Practices for Enterprise OSGi Applications - Emily Jiang

30 31 October 2012

OSGi Application Best Practices

– BP1 - Use Import-Package instead of Require-Bundle– BP2 - Avoid split packages– BP3 - Version bundles and packages– BP4 - Separate API from implementations– BP5 - Share services not implementations– BP6 – Make bundles loosely coupled & highly cohesive– BP7 - Use Blueprint

Summary

Page 31: Best Practices for Enterprise OSGi Applications - Emily Jiang

31

Quiz – What best practices are used?

BloggingService Blog

PersistenceService

blog-servlet

Web application bundle

META-INF/persistence.xml

WEB-INF/

web.xml OSGI-INF/blueprint/blueprint.xml

OSGI-INF/blueprint/blueprint.xml

JNDI EM

blog

blog-persistence

blog-api

Page 32: Best Practices for Enterprise OSGi Applications - Emily Jiang

32 31 October 2012

Questions?