an introduction to osgi: creating highly modular java systems simon archer, ibm [email protected]...

34
An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM [email protected] 15 November 2010

Upload: cody-bailey

Post on 24-Dec-2015

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

An Introduction to OSGi:Creating Highly Modular Java Systems

Simon Archer, [email protected] November 2010

Page 2: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Who is this guy anyway?• I’m a software engineer, just like you.

• Comp. Sci. degree from the UK, where I learned Smalltalk in the early ’90s. Smalltalk developer in London and then Cary.

• Smalltalk is the perfect programming language (debate later).

• Learned Java in 1996 as demand for Smalltalk began to wane.

• Joined OTI in 1998 and started using OSGi in 1999 when it emerged as a solution for networked devices such as set-top boxes.

• At OTI we worked with automotive customers that wished to build dynamic telematics applications, for which we used OSGi.

• Been involved with Eclipse and Equinox since their inception.

Page 3: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Who is this guy anyway?• Also at OTI, worked on an RFID edge application that tracks

goods as they flow in and out of a warehouse, while communicating with back-end server. All OSGi-based.

• Since 2006 I’ve worked on the Jazz Foundation team at IBM’s Rational division in RTP.

• Jazz uses OSGi on the client (it’s an Eclipse-based application) and on the server using the Equinox Servlet Bridge.

• In February 2010 I co-authored the book “OSGi & Equinox: Creating Highly Modular Java Systems” with Jeff McAffer and Paul Vander Lei. See http://www.equinoxosgi.org.

• Talked about all things OSGi at EclipseCon 2010.

Page 4: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

OSGi is a hot topic these days; all the major Java application server vendors have adopted OSGi as their base runtime, Eclipse has been using OSGi as the basis of its modularity story and runtime for at least the past five years, and countless others have been using it in embedded and “under the covers” scenarios. All with good reason.

OSGi is a hot topic these days; all the major Java application server vendors have adopted OSGi as their base runtime, Eclipse has been using OSGi as the basis of its modularity story and runtime for at least the past five years, and countless others have been using it in embedded and “under the covers” scenarios. All with good reason.

From the Preface of “OSGi & Equinox: Creating Highly Modular Java Systems”

Page 5: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

What is OSGi?

• OSGi defines a Java framework for building and executing modular software. Leverages Java’s class loading techology.

• OSGi once stood for “Open Services Gateway initiative”, but now it’s just “OSGi”.

• The OSGi Alliance is an independent, non-profit corporation working to define open specifications.

• The OSGi Service Platform Core Specification describes a software deployment and configuration management architecture.

• On the web at http://www.osgi.org.

• There are numerous implementations:– Equinox, from Eclipse, http://eclipse.org.equinox

– Felix, from Apache, http://felix.apache.org

– Knopflerfish, http://knopflerfish.org

Page 6: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

The OSGi Framework

• Core framework is remarkably small at just 27 Java types.• In OSGi parlance a software module is called a bundle.

Application > Bundle > Package > Type > Method

• OSGi enables the implementation of loosely coupled software modules.

• OSGi enables the implementation of highly cohesive software modules.

• OSGi enables dynamic installation, update and removal of software modules; no VM restart required.

• But using the OSGi framework is not all that it takes…

Page 7: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Coupling?

• Coupling is an outward view of the number of relationships between a bundle and other bundles in the system.

• Bundles should be loosely coupled.• A loosely coupled bundle is more likely to be

used, reused and tested.• “Don’t make me inflate an entire universe!”• Loosely coupled bundles are simpler to

understand, test, debug change, etc.

Page 8: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Loose Coupling is Good

Page 9: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Tight Coupling is Bad!

Page 10: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Cohesion?

• Cohesion is an inward view of the relevance of the elements of a bundle to one other.

• In a highly cohesive bundle all parts are directly related to, and focused on, addressing a defined, narrowly focused topic.

• Sadly, low cohesive bundles are rife.

• Highly cohesive bundles require careful design to keep them small.

Page 11: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

High Cohesion is Good

Page 12: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Low Cohesion is Bad

Page 13: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

The Software Crisis

• Yes, there is still a software crisis!

• Many applications are fragile monoliths with poorly defined dependencies, collaboration points and behavioral responsibilities.

• Modularity is remarkably hard without OSGi.

• OSGi enforces good software modularity.

• Modularity, it turns out, is the lubricant of collaboration.

Page 14: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Modular Software, Right?

Page 15: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Wrong!

Page 16: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Manage Dependencies

Page 17: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Encapsulation

• Bundles should be encapsulated from each other.

• Encapsulation enables loose coupling.

• Encapsulation enables the hiding of implementation details.

• A bundle’s API should be split into internal and external.

• Encapsulation at the module level is challenging in Java due to weak visibility rules.

Page 18: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Good Fences Make Good Neighbors

Page 19: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Granularity

• Bundle size matters.• The size of a bundle affects its coupling and cohesion.• Big bundles often have low cohesion and high coupling.• Big bundles are harder to:

– Use

– reuse

– Test

– Debug

Page 20: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Size Does Matter

Page 21: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Plain Old Java Objects (POJOs)

• Bundles should be POJOs (as far as possible).

• While OSGi-defined types exist, you should not create dependencies between your bundles and OSGi types.

• Bundles that are just POJOs are easier to test, use and reuse.

Page 22: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

It's bad mojo to pollute the

POJO

Page 23: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

OSGi Services

• Bundles can collaborate via the OSGi service registry.• A service is just an instance of a POJO class.• Services are often stateless.• Services must be thread-safe.• Multiple implementations and instances of a service are

common.• Services are dynamically bound and unbound at runtime

by either OSGi-specific wiring code, or the Declarative Services runtime.

Page 24: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

UseServices

Page 25: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Not Everything Should be a Service

Page 26: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Importing and Exporting Packages

• By default a bundle can only see java.* and its own types.

• Bundles can export packages to other bundles.

• Bundles can import packages from other bundles.

• At runtime a bundle is only resolved if its imported packages can be satisfied.

Page 27: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

OSGi will make your app dynamic

Page 28: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010
Page 29: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

OSGi on the Server?

• It is possible to package the OSGi framework inside a WAR and run it in a servlet container on Tomcat, WebSphere, etc.

• This is what the Jazz Project uses.

• See: http://www.eclipse.org/equinox/server.

• This, and everything mentioned so far is discussed in the book…

Page 30: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

http://equinoxosgi.org

Page 31: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Demo Time!

Page 32: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Demo

Page 33: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Interested in JEE?• The Apache Aries Project: http://incubator.apache.org/aries

The Aries project is delivering a set of pluggable Java components enabling an enterprise OSGi application programming model. This includes implementations and extensions of application-focused specifications defined by the OSGi Alliance Enterprise Expert Group (EEG) and an assembly format for multi-bundle applications, for deployment to a variety of OSGi based runtimes.

• The Eclipse Virgo Project: http://www.eclipse.org/virgoEclipse Virgo is a modular open source application server based on OSGi. It supports standard WAR files and modular webapps comprising OSGi bundles.

Page 34: An Introduction to OSGi: Creating Highly Modular Java Systems Simon Archer, IBM sarcher@us.ibm.com 15 November 2010

Questions?