introduction to osgi (tokyo jug)

98
Introduction to OSGi Neil Bartlett – 東京 20091223

Upload: njbartlett

Post on 16-Nov-2014

3.486 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Introduction to OSGi (Tokyo JUG)

Introduction to OSGiNeil Bartlett – 東京 2009年12月23日

Page 2: Introduction to OSGi (Tokyo JUG)

Brief Intro to OSGi

Page 3: Introduction to OSGi (Tokyo JUG)

OSGi: The Module System for Java

Page 4: Introduction to OSGi (Tokyo JUG)

Standard Java

ABC.JAR

JARs are Deployment

Units

They are Not Modules

Page 5: Introduction to OSGi (Tokyo JUG)

Meaningful name

Version

Vendor

Exports

Dependencies

What’s Missing?

ABC.JAR

Page 6: Introduction to OSGi (Tokyo JUG)

Dependencies

JARs do have dependencies

They are implicit.

A dependency is an assumption.

“I assume module X (version Y) is on the

classpath. If not I will crash and burn.”

Page 7: Introduction to OSGi (Tokyo JUG)

Module = “Bundle”

Page 8: Introduction to OSGi (Tokyo JUG)

Meaningful name

Version

Vendor

Exports

Dependencies

Just a JAR + Metadata

org.foo.mylibManifest-Version: 1.0Bundle-SymbolicName: com.mylibBundle-Name: My Library BundleBundle-Vendor: Neil BartlettBundle-Version: 1.0.0Import-Package: javax.swing, org.w3c.domExport-Package: com.mylib1.ui;version=“1.0.0”, com.mylib1.util;version=“1.0.0”Bundle-RequiredExecutionEnvironment: J2SE-1.5

Page 9: Introduction to OSGi (Tokyo JUG)

MANIFEST.MF

Manifest-Version: 1.0Bundle-SymbolicName: com.mylibBundle-Name: My Library BundleBundle-Vendor: Neil BartlettBundle-Version: 1.0.0Import-Package: javax.swing, org.w3c.domExport-Package: com.mylib.ui;version=“1.0.0”, com.mylib.util;version=“1.0.0”Bundle-RequiredExecutionEnvironment: J2SE-1.5

Page 10: Introduction to OSGi (Tokyo JUG)

Works Outside OSGi

Page 11: Introduction to OSGi (Tokyo JUG)

Dependency Graphs

Page 12: Introduction to OSGi (Tokyo JUG)
Page 13: Introduction to OSGi (Tokyo JUG)

com.foo.bar

javax.swing

org.wibble

Imports..... and Exports

com.mylib

com.mylib.ui

com.mylib.util} {

Page 14: Introduction to OSGi (Tokyo JUG)

Package Resolution

com.foo.bar

A

Page 15: Introduction to OSGi (Tokyo JUG)

Package Resolution

com.foo.bar

A

com.foo.bar

B

Page 16: Introduction to OSGi (Tokyo JUG)

Package Resolution

com.foo.bar

A

com.foo.bar

B

Page 17: Introduction to OSGi (Tokyo JUG)

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) A

Page 18: Introduction to OSGi (Tokyo JUG)

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) A

com.foo.bar

1.4.5B

Page 19: Introduction to OSGi (Tokyo JUG)

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) A

com.foo.bar

1.4.5B ✘

Page 20: Introduction to OSGi (Tokyo JUG)

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) A

com.foo.bar

1.3.12B’

Page 21: Introduction to OSGi (Tokyo JUG)

Versioned Dependency

com.foo.bar

[1.2.0,1.4.0) A

com.foo.bar

1.3.12B’

Page 22: Introduction to OSGi (Tokyo JUG)

Side-by-Side Versions

com.foo.bar

1.4.5

com.foo.bar

1.3.12

com.foo.bar

[1.2.0,1.4.0)

com.foo.bar

[1.4.0,1.5.0)B

B’

X

Y

Page 23: Introduction to OSGi (Tokyo JUG)

Side-by-Side Versions

com.foo.bar

1.4.5

com.foo.bar

1.3.12

com.foo.bar

[1.2.0,1.4.0)

com.foo.bar

[1.4.0,1.5.0)B

B’

X

Y

Page 24: Introduction to OSGi (Tokyo JUG)

How it Works

Bundle-SymbolicName: com.mylib1Bundle-Version: 1.2.0Export-Package: com.mylib1.ui;version=“1.2.0”, com.mylib1.util;version=“1.2.0”

Bundle-SymbolicName: com.app1Bundle-Version: 2.2.3.alphaImport-Package: com.mylib1.ui;version=“[1.2.0,1.3.0)” com.mylib1.util;version=“[1.2.0,1.3.0)”

Page 25: Introduction to OSGi (Tokyo JUG)

Private Internals

Exports must be stated explicitly

Packages not listed in “Export-Package” are not

available to other bundles

Page 26: Introduction to OSGi (Tokyo JUG)

Versions

Standard numbering scheme with well-defined

ordering.

major.minor.micro.qualifier

First three numeric, last alphanumeric

Eg 1.0.0.beta2

Unspecified ! 0.0.0

Page 27: Introduction to OSGi (Tokyo JUG)

Version Ranges

Open, closed or implicit

[1.0.0, 2.0.0] ! 1.0.0 ! version ! 2.0.0

[1.0.0, 2.0.0) ! 1.0.0 ! version < 2.0.0

Informally “1.*”

1 ! [1.0.0, ")

Unspecified ! [0.0.0, ")

Page 28: Introduction to OSGi (Tokyo JUG)

Our Promise*: No More

NoClassDefFoundErrors!

Page 29: Introduction to OSGi (Tokyo JUG)

Dynamic!Dynamic!

Page 30: Introduction to OSGi (Tokyo JUG)

Dynamic

Install Bundles

Update Bundles

Uninstall Bundles

... all “on the fly”

Page 31: Introduction to OSGi (Tokyo JUG)

OSGi in Infrastructure

Page 32: Introduction to OSGi (Tokyo JUG)

OSGi is the King of Infrastructure

All Major JEE Application Servers use OSGi

Most ESBs use OSGi

2 of 3 Open Source IDEs use OSGi

Even Build Tools (Maven and Hudson) Moving to OSGi

Page 33: Introduction to OSGi (Tokyo JUG)

Where are the “Business” Apps?

Page 34: Introduction to OSGi (Tokyo JUG)

OSGi for Applications

Until Recently, Application Servers used OSGi “on the inside”

Now SpringSource dm Server, Paremus Infiniflow, WAS 7, GlassFish v3 and WebLogic DM all expose OSGi

Application Developers can Finally Deploy OSGi bundles to their OSGi servers!

Page 35: Introduction to OSGi (Tokyo JUG)

Why is OSGi Attractive for Application Development?

Page 36: Introduction to OSGi (Tokyo JUG)

The Failure of Object Oriented Programming

Page 37: Introduction to OSGi (Tokyo JUG)

OOP Was Meant to Enable Reuse

Page 38: Introduction to OSGi (Tokyo JUG)

HARDER THAN EXPECTED!

Page 39: Introduction to OSGi (Tokyo JUG)

“Building a new system would be a snap. Just get a

few classes, bunch them together... and voila!”

– Peter Kriens

Page 40: Introduction to OSGi (Tokyo JUG)

What Went Wrong??

Page 41: Introduction to OSGi (Tokyo JUG)

Tight Coupling

Page 42: Introduction to OSGi (Tokyo JUG)

COMPLEXITY

Page 43: Introduction to OSGi (Tokyo JUG)

Classes coupled to Other Classes

Page 44: Introduction to OSGi (Tokyo JUG)

Packages coupled to Other Packages

Page 45: Introduction to OSGi (Tokyo JUG)

Packages coupled to JARs

Page 46: Introduction to OSGi (Tokyo JUG)

JARs coupled to More JARs

Page 47: Introduction to OSGi (Tokyo JUG)

Which are coupled to yet more JARs...

Page 48: Introduction to OSGi (Tokyo JUG)
Page 49: Introduction to OSGi (Tokyo JUG)

Dependency Injection?

Page 50: Introduction to OSGi (Tokyo JUG)

DI Frameworks

Provide “Late Binding” of Implementations to Interfaces

But Not Late Enough!

Page 51: Introduction to OSGi (Tokyo JUG)

Spring Framework

<bean id="MyBean" class="org.example.MyBean"> <property name="dataSource" ref="OracleDataSource"/></bean>

<bean id="OracleDataSource" class="..."> <property name="..." /></bean>

Page 52: Introduction to OSGi (Tokyo JUG)

Traditional Spring

Page 53: Introduction to OSGi (Tokyo JUG)

Static DI

“Beans” are wired together once, at start-up

They cannot be rewired

Limited support for optional dependencies

Page 54: Introduction to OSGi (Tokyo JUG)

FRAGILE

Page 55: Introduction to OSGi (Tokyo JUG)

One Solution: Give Up!

Page 56: Introduction to OSGi (Tokyo JUG)

Reuse is Hard, So Don’t Try!

Page 57: Introduction to OSGi (Tokyo JUG)

Leave Code in One Place, and Call Remotely

(...also known as “SOA”)

Page 58: Introduction to OSGi (Tokyo JUG)

Component Oriented Programming

Page 59: Introduction to OSGi (Tokyo JUG)

What is a “Component”?

Page 60: Introduction to OSGi (Tokyo JUG)

The Usual Analogy:

Page 61: Introduction to OSGi (Tokyo JUG)

LEGO?

Page 62: Introduction to OSGi (Tokyo JUG)

Not Really...

Dead Lumps of Plastic

Many Copies of the Same Thing

Page 63: Introduction to OSGi (Tokyo JUG)

BIOLOGY

Page 64: Introduction to OSGi (Tokyo JUG)

BIOLOGY

Page 65: Introduction to OSGi (Tokyo JUG)

Dr Alan Kay

Inventor of Object Oriented Programming & Smalltalk

“I thought of objects being like biological cells...”

Page 66: Introduction to OSGi (Tokyo JUG)

Components

Do Something, or Provide Something

Aware of and Adapt to their Environment

Have a Life-cycle (birth to death).

Page 67: Introduction to OSGi (Tokyo JUG)

“Do” Something

Open a socket

Monitor a device

Poll a queue

Display a GUI

.... etc.

Page 68: Introduction to OSGi (Tokyo JUG)

“Provide” Something

Publish a Service – may be used by other components

Page 69: Introduction to OSGi (Tokyo JUG)

The “Environment”

The “Environment” of a Component means:

Services provided by other Components

Resources, Devices, Network etc

Page 70: Introduction to OSGi (Tokyo JUG)

Components Adapt to their Environment

Page 71: Introduction to OSGi (Tokyo JUG)

Most services are available

The component flourishes

Good Environment

Page 72: Introduction to OSGi (Tokyo JUG)

Harsh Environment

Some non-essential services are unavailable

Component adapts and survives

Page 73: Introduction to OSGi (Tokyo JUG)

Very Harsh Environment

Essential services are unavailable

Component hibernates or dies

Page 74: Introduction to OSGi (Tokyo JUG)

Composition

Components Use other Components

In this way, whole Systems are Composed

Resilient, Elastic Systems

Page 75: Introduction to OSGi (Tokyo JUG)

Developing Components

Page 76: Introduction to OSGi (Tokyo JUG)

Services

Components provide Services

Registered with a Service Registry

Services are POJOs!

Looked up by Java interface name

Page 77: Introduction to OSGi (Tokyo JUG)

In-Process SOA

ServiceProviderService

Consumer

ServiceBroker

Client Service

RegisterFind

Bind

ServiceContract

Page 78: Introduction to OSGi (Tokyo JUG)

Plain Old Java Objects

Page 79: Introduction to OSGi (Tokyo JUG)

The Glue Between Components

Page 80: Introduction to OSGi (Tokyo JUG)

Dynamics Make Services Slippery

Page 81: Introduction to OSGi (Tokyo JUG)

We Don’t Code Directly against Services (it’s too hard!)

Page 82: Introduction to OSGi (Tokyo JUG)

Let a Framework Handle the Hard Stuff

Page 83: Introduction to OSGi (Tokyo JUG)

Choice of Frameworks

Declarative Services (DS)

Blueprint (from Spring-DM)

iPOJO

Guice Peaberry

Page 84: Introduction to OSGi (Tokyo JUG)

Framework Interop

Perfect Interoperability of these Frameworks!

No need to choose “The One True Framework”

Use 3rd-party components implemented with other frameworks.

Page 85: Introduction to OSGi (Tokyo JUG)

Examining DS

Dynamic dependency injection (DI)

... and “uninjection”

Page 86: Introduction to OSGi (Tokyo JUG)

Starting Point...

import javax.sql.DataSource;

public class DbMailbox implements Mailbox {

private DataSource dataSource; void setDataSource(DataSource ds) { /*...*/ } void unsetDataSource(DataSource ds) { /*...*/ }

public long[] getAllMessages() { // ... }}

Page 87: Introduction to OSGi (Tokyo JUG)

Starting Point...

import javax.sql.DataSource;

public class DbMailbox implements Mailbox {

private DataSource dataSource; void setDataSource(DataSource ds) { /*...*/ } void unsetDataSource(DataSource ds) { /*...*/ }

public long[] getAllMessages() { // ... }}

No OSGi API Used

Page 88: Introduction to OSGi (Tokyo JUG)

Starting Point...

import javax.sql.DataSource;

public class DbMailbox implements Mailbox {

private DataSource dataSource; void setDataSource(DataSource ds) { /*...*/ } void unsetDataSource(DataSource ds) { /*...*/ }

public long[] getAllMessages() { // ... }}

No OSGi API Used

JavaBeans Style => Testable

Page 89: Introduction to OSGi (Tokyo JUG)

Make it a Componentimport javax.sql.DataSource;

@Componentpublic class DbMailbox implements Mailbox {

private DataSource dataSource; @Reference(service = DataSource.class) void setDataSource(DataSource ds) { /*...*/ } void unsetDataSource(DataSource ds) { /*...*/ }

public long[] getAllMessages() { // ... }}

Page 90: Introduction to OSGi (Tokyo JUG)

Annotations

@Component – Create a Component of this type.

Service is automatically published

@Reference – Use the specified service

Page 91: Introduction to OSGi (Tokyo JUG)

Add Life-cycle

@Activate void start() { thread = new Thread(); thread.start(); } @Deactivate void stop() { thread.interrupt(); }

Page 92: Introduction to OSGi (Tokyo JUG)

Make it Configurable @Activate void start(Map<String, Object> configuration) { thread = new Thread(); thread.start(); } @Modified void modify(Map<String, Object> newConfiguration) { // This method is optional } @Deactivate void stop() { thread.interrupt(); }

Page 93: Introduction to OSGi (Tokyo JUG)

Optional Reference

@Reference(service = LogService.class, optional = true)void setLogService(LogService log) { /* ... */ }void unsetLogService(LogService log) { /* ... */ }

Page 94: Introduction to OSGi (Tokyo JUG)

Multiple Reference

@Reference(service = MailboxListener.class, multiple = true)void addListener(MailboxListener listener) { /* ... */ }void removeListener(MailboxListener listener) { /* ... */ }

Page 95: Introduction to OSGi (Tokyo JUG)

DEMO

Page 96: Introduction to OSGi (Tokyo JUG)

Mailbox Reader

Fixed

Mailbox

Mailbox

Page 97: Introduction to OSGi (Tokyo JUG)

Mailbox Reader

GrowingMailbox

MailboxListener

Mailbox

Page 98: Introduction to OSGi (Tokyo JUG)

Mailbox Reader

TradesMailbox

MailboxListener

Mailbox

EventHandler

Event Broker

EventAdmin

Trade MatchingEngine

Order Entry

MatchingService