os gi introduction made by ly minh phuong-soc team

20
OSGi Introduction Ly Minh Phuong – Social

Upload: thuydang

Post on 10-May-2015

703 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Os gi introduction made by Ly MInh Phuong-SOC team

OSGi Introduction

Ly Minh Phuong – Social Team

Page 2: Os gi introduction made by Ly MInh Phuong-SOC team

2

Objective

OSGi architecture overview Bundles Services Lifecycle

DemoQ&A

OSGi framework Motivation

Page 3: Os gi introduction made by Ly MInh Phuong-SOC team

3

Why OSGi?

Java not support natively dynamic module system.(@2011)

JAR dependences management is missing.

No way to restrict using class in JAR file.

Page 4: Os gi introduction made by Ly MInh Phuong-SOC team

4

Why OSGi?

Java not support natively dynamic module system.(@2011)

Page 5: Os gi introduction made by Ly MInh Phuong-SOC team

5

Why OSGi?

JAR dependences management is missing.

What happened when two jar file have duplicate packages and classes ?

What happened when your classes only work with a library which version < 2.3.0 ?

Can it be prevent in run time ? No way to restrict using class in JAR file.

When writing a lib/component can we control what user can import or not ?

Page 6: Os gi introduction made by Ly MInh Phuong-SOC team

6

Why OSGi?

Resolve in OSGi way.

Page 7: Os gi introduction made by Ly MInh Phuong-SOC team

7

What is OSGi

OSGi alliance

Open Service Gateway initiative

Founded 1999

Specification first released in 2000

Newest version of specification is 4.3 (April 2011)

Target: embedded, desktop, enterprise application.

Widely adopted: Eclipse Equinox, Apache Felix, Glassfish, Knopflerfish, ProSyst, Hitachi SuperJ Engine, …

Page 8: Os gi introduction made by Ly MInh Phuong-SOC team

8

Basic architect

Page 9: Os gi introduction made by Ly MInh Phuong-SOC team

9

Bundle

Bundle

Basic deployment entity ( app, component, library)

Version convention ( major.minor.micro.qualifier )

Specify the dependence, export package, activator

Package like JAR file and added the bundle meta info

Bundle-ManifestVersion: 2Bundle-Name: Greeting ClientBundle-SymbolicName: org.foo.hello.clientBundle-Version: 1.2.1.SNAPSHOT

Page 10: Os gi introduction made by Ly MInh Phuong-SOC team

10

Bundle Lifecycle

Page 11: Os gi introduction made by Ly MInh Phuong-SOC team

11

Bundle dependency resolution The framework prefer already resolved bundle

If both are resolved the framework prefer highest matching version.

If the version is equal it's prefer the older one.

Page 12: Os gi introduction made by Ly MInh Phuong-SOC team

12

Bundle Activator Bundle Activator

Define by Bundle-Activator header

Handle when bundle start/stop

public final class Activator implements BundleActivator{ public void start( BundleContext bc ) throws Exception{ .... } public void stop( BundleContext bc ) throws Exception{ .... }}

Page 13: Os gi introduction made by Ly MInh Phuong-SOC team

13

Bundle Mainifest example

Bundle-ManifestVersion: 2Bundle-Name: Greeting ClientBundle-SymbolicName: org.foo.hello.clientBundle-Version: 1.2.1.SNAPSHOTBundle-Activator: org.foo.hello.client.Activator Import-Package: org.foo.hello;version="[1.0,2.0)"Export-Package: org.org.foo.hello.api;version="1.0"Bundle-ClassPath: .,WEB-INF/classes,WEB-INF/lib/a.jar

Page 14: Os gi introduction made by Ly MInh Phuong-SOC team

14

Service

Service

Bundle can register/unregister service using bundle activator.

One service name can have multi provider

Service can have properties to describe the service info

Page 15: Os gi introduction made by Ly MInh Phuong-SOC team

15

Registering a service

Programmatically using Activator

Dictionary properties = new Properties();properties.setProperty("BookStorageType", "MySQL");

bundleContext.registerService(BookStorage.class.getName(), new BookStorageImpl(), properties );

Page 16: Os gi introduction made by Ly MInh Phuong-SOC team

16

Consuming a service

Programatically using bundleContext.getSerivce()ServiceReference bookStorageRef = bc.getServiceReference(BookStorage.class.getName());if(bookStorageRef != null){

bookStorage = (BookStorage) bc.getService(bookStorageRef);System.out.println("Got BookStorage Service");

}

Use Service ListenerActivator.javaString filter = "(" + Constants.OBJECTCLASS + "=" + BookStorage.class.getName() + ")";context.addServiceListener(new BookStorageListener(), filter);

BookStorageListener.javaclass BookStorageListener implements ServiceListener { public void serviceChanged(ServiceEvent event) { .... }}

Page 17: Os gi introduction made by Ly MInh Phuong-SOC team

17

Service consuming

Use Service Tracker

public class XyzServiceTracker extends ServiceTracker { public XyzServiceTracker(BundleContext context) { super(context, ServiceToTrack.class.getName(), null); ... } public Object addingService(ServiceReference reference) { ... } public void removedService(ServiceReference reference, Object service) { context.ungetService(reference); ... }}

Page 18: Os gi introduction made by Ly MInh Phuong-SOC team

18

Problems about OSGi

OSGi look great but tool and development process not widely public yet.

Not every lib is bundle.

Version management can be nightmare.

Runtime dynamic can result untested situation.

Page 19: Os gi introduction made by Ly MInh Phuong-SOC team

19

DEMO

Page 20: Os gi introduction made by Ly MInh Phuong-SOC team

20

Q&A