developing java applications with osgichariotsolutions.com/.../archive/511/...with-osgi.pdf ·...

29
Developing Java Applications with OSGi Emerging Technologies for the Enterprise 2008 Michael P. Redlich March 26, 2008

Upload: others

Post on 02-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

Developing Java Applications with OSGi

Emerging Technologies for the Enterprise 2008

Michael P. RedlichMarch 26, 2008

Page 2: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

2Emerging Technologies for the Enterprise 2008

March 26, 2008

My Background (1)

DegreeB.S. in Computer ScienceRutgers University (go Scarlet Knights!)

ExxonMobil Research & EngineeringSenior Research Technician (1988-1998, 2004-present)Systems Analyst (1998-2002)

Ai-Logix, Inc.Technical Support Engineer (2003-2004)

Amateur Computer Group of New Jersey (ACGNJ)Java Users Group Leader (2001-present)President (2007-present)Secretary (2006)

Page 3: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

3Emerging Technologies for the Enterprise 2008

March 26, 2008

My Background (2)

PublicationsJava Boutique (http://www.javaboutique.com/)

Co-authored with Barry BurdDesign Patterns

http://publications.redlich.net/

PresentationsTrenton Computer Festival (TCF) since 1998TCF IT Professional Seminars since 2006Princeton Java Users GroupCapital District Java Developers NetworkNew York Software Industry Association (NYSIA)

Page 4: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

4Emerging Technologies for the Enterprise 2008

March 26, 2008

Other OSGi-Related Seminars at ETE 2008

OSGi with Spring DMDmitry SklyutWednesday, March 265:15 – 6:15pmBehrakis Grand Hall (South)

2010: An Acronym OdysseyBrian O’Neill, Technical Architect, Gestalt LLCWednesday, March 265:15 – 6:15pmMacAlister 4011

Page 5: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

5Emerging Technologies for the Enterprise 2008

March 26, 2008

Objectives

What is OSGi?OSGi AllianceOSGi FrameworksOSGi LayersHow to interact with the framework

Develop a first OSGi bundleDevelop a more “Real-World” applicationSource code, source code, source code (yea!)

Page 6: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

6Emerging Technologies for the Enterprise 2008

March 26, 2008

What is OSGi?

Open Services Gateway initiative (OSGi)Originally designed to promote dynamic systems for embedded Java and network devicesFoundation for an enhanced service-oriented architectureDefines a standardized, component-oriented environmentApplications can be started, stopped, updated, and uninstalled

without requiring the JVM to be restarted

Page 7: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

7Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi to the Rescue!

Component issuesInstallationStart and stopManage multiple versionsUpdateUninstall

JAR file dependency issuesClass-Path attribute in MANIFEST file

Versioning in file name

Page 8: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

8Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi Alliance

A non-profit organization formed in March 1999“…a worldwide consortium of technology innovators that advances a proven and mature process to assure interoperability of applications and services based on its component integration platform.”“…provides specifications, reference implementations, test suites and certification to foster a valuable cross-industry ecosystem.”

Page 9: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

9Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi Frameworks

Eclipse Equinox (3.3.2)Apache Felix (1.0.3)Knopflerfish (2.0.5)Spring OSGi Modules (1.0.1)Newton (0.2)

Page 10: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

10Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi Framework Layers

ModulesClass loading policies

Life CycleBundle management

Service RegistryCommunication among bundlesService discovery

Security LayerSpans all other layers

Page 11: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

11Emerging Technologies for the Enterprise 2008

March 26, 2008

Modules Layer

Defines class loading policiesEvery bundle can export and import packagesA package is always exported with a unique versionA bundle can specify a range of versions that it can import

Supports multiple class spaces where multiple versions of the same class can be used simultaneouslyEnsures that bundles can bind to a new exporting package should the original one be uninstalled

Page 12: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

12Emerging Technologies for the Enterprise 2008

March 26, 2008

Life Cycle Layer

Defines how bundles are installed, updated, and uninstalledUNINSTALLEDINSTALLED

Framework has the bundleDependencies not resolved

RESOLVEDDependencies resolvedBundle can be started

STARTINGSTOPPINGACTIVE

Bundle is running

Page 13: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

13Emerging Technologies for the Enterprise 2008

March 26, 2008

Basic APIs to Interact with the Framework

Bundle

BundleActivator

BundleContext

ServiceRegistration

ServiceTracker

ServiceReference

Page 14: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

14Emerging Technologies for the Enterprise 2008

March 26, 2008

Bundles

A “module” or “plug-in”Packaged in a JAR fileJava CLASS files and other resourcesSpecial MANIFEST file

Started through a BundleActivator

Each of the frameworks support special bundlese.g., system bundle that represents the framework

Page 15: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

15Emerging Technologies for the Enterprise 2008

March 26, 2008

Manifest-Version: 1.0

Bundle-Name: Basic Movie Finder

Bundle-Activator:

org.emergingtech.BasicMovieFinderActivator

Bundle-SymbolicName: org.emergingtech.basicmoviefinder

Bundle-Version: 1.0.0

Import-Package: org.emergingtech;version=“[1.0.0,2.0.0)”

Export-Package: org.emergingtech;version=“1.0.0”

Specifies a unique, global name for this bundle

Declares imported (including version ranges) and exported

packages for this bundle

Specifies the name of the class used to start and

stop the bundle

Defines a human-readable name (that may contain spaces) for this bundle

Page 16: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

16Emerging Technologies for the Enterprise 2008

March 26, 2008

BundleActivator

An interface that may be implemented to start and stop a bundleCreated by the framework as specified through the Bundle-Activator entry in the manifest fileFully-qualified Java class name

void start(BundleContext context);void stop(BundleContext context);

Page 17: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

17Emerging Technologies for the Enterprise 2008

March 26, 2008

BundleContext

A bundle's execution context within the frameworkA “magic ticket” maintained by the framework

Created by the framework when a bundle is startedBundleContext allows a bundle to:Install new bundlesInterrogate other bundlesRegister services with the Service RegistrySubscribe to/consume registered servicesRetrieve a list of ServiceReferences from the Service Registry

Page 18: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

18Emerging Technologies for the Enterprise 2008

March 26, 2008

And Now For...

...our first OSGi example?

Are You Ready?

Page 19: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

19Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi Services

Bundles registered with the Service RegistryServices “live” in the Services Registry

Registered under a Java interface and an optional set of propertiesThe framework automatically unregisters all services from a bundle that is stoppedNotifies all of its dependents

Services can be tracked and retrieved

Page 20: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

20Emerging Technologies for the Enterprise 2008

March 26, 2008

Registering a Service

To register a service with the framework, use BundleContext.registerService() methodReturns a ServiceRegistration object

MovieFinder finder = new BasicMovieFinder();

ServiceRegistration registration = context.registerService(MovieFinder.class.getName(),finder,properties);

Register the specified service, finder, with an optional set of specified

properties under the specified class name, MovieFinder

Page 21: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

21Emerging Technologies for the Enterprise 2008

March 26, 2008

Consuming a Service

An separate bundle that is interested in using a registered serviceTo discover and retrieve services, use ServiceTracker

ServiceTracker tracker = new ServiceTracker(context,MovieFinder.class.getName(),null);

tracker.open();

MovieFinder finder = (MovieFinder)tracker.getService();

Creates a ServiceTracker object for the specified class name, i.e.,

the MovieFinder service

Start tracking instances of MovieFinder, i.e., the service, registered in the Service Registry

Returns a MovieFinder service being tracked by ServiceTracker

Page 22: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

22Emerging Technologies for the Enterprise 2008

March 26, 2008

ServiceRegistration

A registered serviceFor the private use of the registering bundleShould not be shared with other bundlesObject returned upon a successful call to BundleContext.registerService()

Used to update the properties of the service or to unregister the serviceEvery service registered in the framework has a unique ServiceRegistration object

Page 23: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

23Emerging Technologies for the Enterprise 2008

March 26, 2008

ServiceTracker

Simplifies using services from the framework's service registryAbstracts away all the gory details of dealing with the Service

Registry

BundleContext can also retrieve services, but requires additional maintenance

Page 24: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

24Emerging Technologies for the Enterprise 2008

March 26, 2008

ServiceReference

A reference to a serviceA lightweight handleProvides access to a service’s properties but not the actual service objectThe service object must be acquired through a bundle’s BundleContext

Can be queried by a bundle to assist in the selection of a service

Avoids creating unnecessary dynamic service dependencies among bundles

Page 25: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

25Emerging Technologies for the Enterprise 2008

March 26, 2008

And Now For...

...a more “real-world” example (woo-hoo!)

Are You Ready?

Page 26: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

26Emerging Technologies for the Enterprise 2008

March 26, 2008

MovieLister Application

Page 27: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

27Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi Resources (1)

Equinoxhttp://www.eclipse.org/equinox/

Apache Felixhttp://felix.apache.org/

Knopflerfishhttp://www.knopflerfish.org/

Spring OSGihttp://www.springframework.org/osgi/

Newtonhttp://newton.codecauldron.org/

Page 28: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

28Emerging Technologies for the Enterprise 2008

March 26, 2008

OSGi Resources (2)

OSGi Alliancehttp://www.osgi.org/

OSGi Technical Whitepaperhttp://www.osgi.org/wiki/uploads/Links/OSGiTechnicalWhitePaper.pdf

OSGi Best Practices Presentationhttp://www.osgi.org/wiki/uploads/Conference/OSGiBestPractices.pdf

Getting Started with OSGiNeil Bartlett’s Point-Free Bloghttp://www.neilbartlett.name/blog/osgi-articles/

Page 29: Developing Java Applications with OSGichariotsolutions.com/.../archive/511/...with-OSGi.pdf · Emerging Technologies for the Enterprise 8 2008 March 26, 2008 OSGi Alliance A non-profit

29Emerging Technologies for the Enterprise 2008

March 26, 2008

Java Resources

ACGNJ Java Users Groupfacilitated by Mike Redlichhttp://www.javasig.org/

Princeton Java Users Groupfacilitated by Yakov Fainhttp://www.myflex.org/princetonjug/

NYJavaSIGfacilitated by Frank Grecohttp://www.javasig.com/

Chariot Solutionshttp://www.chariotsolutions.com/