SlideShare a Scribd company logo
1 of 32
Download to read offline
© 2010 IBM Corporation
OSGi Application Best Practices
Emily Jiang, emijiang@uk.ibm.com
WebSphere Application Server OSGi Developer, Apache Aries Committer
© 2011 IBM Corporation
2
AGENDA
> Why OSGi?
> What is OSGi?
> How to best use OSGi?
© 2011 IBM CorporationIBM ConfidentialOctober 18, 2012
> Jars have no modularization
characteristics
– No “jar scoped” access modifiers.
– No means for a jar to declare its
dependencies.
– No versioning.
JarJar
PackagePackage
ClassClass
ClassClass
ClassClass
PackagePackage
ClassClass
ClassClass
ClassClass
PackagePackage
ClassClass
ClassClass
ClassClass
Modularization in Java
4
Problems with Global Java ClassPath
J a va V M
log 4 j
ba rc ode 4 j
a xis
ba tik
c om m ons
de rby
fop
e zm orph
fre e m a rke r
httpunit
ja ka rta
jc l
js on
jdbm
jdom
je nks
jpos 1 8
jython
looks
luc e ne
m a il
m x4 j
na m ing
je tty
poi
re s olve r
rom e
s e ria lize r
s e rvle ts
tom c a t
ve loc ity
w s -c om m ons
xa la n
w s dl4 j
xe rc e s
xm lg ra phic s
xm lrpc
xm la pis
..
g e ronim 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 ounting
a s s e tm a int
ba s e
bi
c a ta lina
c om m on
oa g is
orde r
e ba y
c onte nt
da ta file
e c om m e rc e
e ntity
g oog le ba s e
ofbiz
w idg e t
m inila ng
pa rty
pos .
produc t
w orke ffort
w orkflow
…
s unjc e _prov.
plug in
js s e
jc e
rt
dns ns
..
…
C la s s
N ot
F ound
E xc e ption
B e g inB e g in
He reHe re
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
AGENDA
> Why OSGi?
> What is OSGi?
> How to best use OSGi?
© 2011 IBM Corporation7
JarJar
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
831/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 Loading
Each 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.0
Bundle-ManifestVersion: 2
Bundle-Name: MyService bundle
Bundle-SymbolicName: com.sample.myservice
Bundle-Version: 1.0.0
Bundle-Activator: com.sample.myservice.Activator
Import-Package: com.something.i.need;version="1.1.2"
Export-Package: com.myservice.api;version="1.0.0"
Bundle
9
OSGi Bundle
Bundles have a dynamic lifecycle
Can come and go independently
Bundle
• 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
AGENDA
> Why OSGi?
> What is OSGi?
> How to best use OSGi?
1231 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
1331 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
1431 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
1531 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
1631 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.
1731 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 B
org.hal.a 1.0.0
Client A
Imports:
org.hal.a [1.0, 2.0)
API B
org.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
1831 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
1931 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
2031 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
2131 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
2231 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
2331 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
2431 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
2531 October 2012
BP6 – Make Bundles Loosely Coupled & Highly
Cohesive
Implementation
org.hal.DBlog.impl
DBAPI
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
2631 October 2012
Using services can be hard!
@Override
public 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);
}
}
2731 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 injected
publishes
service
consumes
service
A static assembly and
configuration of
components (POJOs)
Blueprint bundle
OSGI-INF/blueprint/
blueprint.xml
2831 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.
2931 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
3031 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
31
Quiz – What best practices are used?
Blogging
Service
Blog
Persistence
Service
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
3231 October 2012
Questions?

More Related Content

What's hot

Travelling Light for the Long Haul - Ian Robinson
Travelling Light for the Long Haul -  Ian RobinsonTravelling Light for the Long Haul -  Ian Robinson
Travelling Light for the Long Haul - Ian Robinson
mfrancis
 
RIA with Flex & PHP - Tulsa TechFest 2009
RIA with Flex & PHP  - Tulsa TechFest 2009RIA with Flex & PHP  - Tulsa TechFest 2009
RIA with Flex & PHP - Tulsa TechFest 2009
Jason Ragsdale
 
Xm Lmessagingwith Soap
Xm Lmessagingwith SoapXm Lmessagingwith Soap
Xm Lmessagingwith Soap
LiquidHub
 

What's hot (20)

Moved to https://slidr.io/azzazzel/leveraging-osgi-to-create-extensible-plugi...
Moved to https://slidr.io/azzazzel/leveraging-osgi-to-create-extensible-plugi...Moved to https://slidr.io/azzazzel/leveraging-osgi-to-create-extensible-plugi...
Moved to https://slidr.io/azzazzel/leveraging-osgi-to-create-extensible-plugi...
 
Ecom lec3 16_ej_bs
Ecom lec3 16_ej_bsEcom lec3 16_ej_bs
Ecom lec3 16_ej_bs
 
Travelling Light for the Long Haul - Ian Robinson
Travelling Light for the Long Haul -  Ian RobinsonTravelling Light for the Long Haul -  Ian Robinson
Travelling Light for the Long Haul - Ian Robinson
 
CERTIFICATE_TDS
CERTIFICATE_TDSCERTIFICATE_TDS
CERTIFICATE_TDS
 
Laravel - A Trending PHP Framework
Laravel - A Trending PHP FrameworkLaravel - A Trending PHP Framework
Laravel - A Trending PHP Framework
 
What's New in Java 9 KCDC
What's New in Java 9  KCDCWhat's New in Java 9  KCDC
What's New in Java 9 KCDC
 
Lamp Zend Security
Lamp Zend SecurityLamp Zend Security
Lamp Zend Security
 
Apache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application DevelopmentApache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application Development
 
Top spring interview question and answer
Top spring interview question and answerTop spring interview question and answer
Top spring interview question and answer
 
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
 
Ibm db2 10.5 for linux, unix, and windows developing perl, php, python, and...
Ibm db2 10.5 for linux, unix, and windows   developing perl, php, python, and...Ibm db2 10.5 for linux, unix, and windows   developing perl, php, python, and...
Ibm db2 10.5 for linux, unix, and windows developing perl, php, python, and...
 
Smart Internationalization assistance and resource translation tools
Smart Internationalization assistance and resource translation toolsSmart Internationalization assistance and resource translation tools
Smart Internationalization assistance and resource translation tools
 
RIA with Flex & PHP - Tulsa TechFest 2009
RIA with Flex & PHP  - Tulsa TechFest 2009RIA with Flex & PHP  - Tulsa TechFest 2009
RIA with Flex & PHP - Tulsa TechFest 2009
 
Comparison Of Open Source App Servers
Comparison Of Open Source App ServersComparison Of Open Source App Servers
Comparison Of Open Source App Servers
 
Flexible delivery options
Flexible delivery options Flexible delivery options
Flexible delivery options
 
Xm Lmessagingwith Soap
Xm Lmessagingwith SoapXm Lmessagingwith Soap
Xm Lmessagingwith Soap
 
Ibm db2 10.5 for linux, unix, and windows developing ado.net and ole db app...
Ibm db2 10.5 for linux, unix, and windows   developing ado.net and ole db app...Ibm db2 10.5 for linux, unix, and windows   developing ado.net and ole db app...
Ibm db2 10.5 for linux, unix, and windows developing ado.net and ole db app...
 
AD201 - IBM Domino Application Development Today And Tomorrow
AD201 - IBM Domino Application Development Today And TomorrowAD201 - IBM Domino Application Development Today And Tomorrow
AD201 - IBM Domino Application Development Today And Tomorrow
 
Getting Started with SQL Server Compact Edition 3.51
Getting Started with SQL Server Compact Edition 3.51Getting Started with SQL Server Compact Edition 3.51
Getting Started with SQL Server Compact Edition 3.51
 
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
DPC2007 CodeGear, Delphi For PHP (Pawel Glowacki)
 

Viewers also liked

Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...
Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...
Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...
mfrancis
 
Platinum Keynote - The Expanding Role of the OSGi Alliance - C Hayman
Platinum Keynote - The Expanding Role of the OSGi Alliance - C HaymanPlatinum Keynote - The Expanding Role of the OSGi Alliance - C Hayman
Platinum Keynote - The Expanding Role of the OSGi Alliance - C Hayman
mfrancis
 
Opening OSGi to the world- Simple integration of services not written in Java...
Opening OSGi to the world- Simple integration of services not written in Java...Opening OSGi to the world- Simple integration of services not written in Java...
Opening OSGi to the world- Simple integration of services not written in Java...
mfrancis
 
OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...
OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...
OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...
mfrancis
 
Aceu2009 Osgi Productline
Aceu2009 Osgi ProductlineAceu2009 Osgi Productline
Aceu2009 Osgi Productline
guest60ed0b
 
Presentatie Duncan Rogers NMD2010 17 juni 2010
Presentatie Duncan Rogers NMD2010 17 juni 2010Presentatie Duncan Rogers NMD2010 17 juni 2010
Presentatie Duncan Rogers NMD2010 17 juni 2010
OGZ
 

Viewers also liked (13)

Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...
Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...
Everything can be a bundle - making OSGi bundles of Java legacy code - Gunnar...
 
Platinum Keynote - The Expanding Role of the OSGi Alliance - C Hayman
Platinum Keynote - The Expanding Role of the OSGi Alliance - C HaymanPlatinum Keynote - The Expanding Role of the OSGi Alliance - C Hayman
Platinum Keynote - The Expanding Role of the OSGi Alliance - C Hayman
 
Osgi Devconeu 2009 Welcome Christer Larsson
Osgi Devconeu 2009 Welcome Christer LarssonOsgi Devconeu 2009 Welcome Christer Larsson
Osgi Devconeu 2009 Welcome Christer Larsson
 
Opening OSGi to the world- Simple integration of services not written in Java...
Opening OSGi to the world- Simple integration of services not written in Java...Opening OSGi to the world- Simple integration of services not written in Java...
Opening OSGi to the world- Simple integration of services not written in Java...
 
Keynote - Internet Home Alliance - T Barra
Keynote - Internet Home Alliance - T BarraKeynote - Internet Home Alliance - T Barra
Keynote - Internet Home Alliance - T Barra
 
OSGi Best and Worst Practices
OSGi Best and Worst PracticesOSGi Best and Worst Practices
OSGi Best and Worst Practices
 
OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...
OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...
OSGi Best Practices – Learn how to prevent common mistakes and build robust, ...
 
OSGi Best Practices - Tim Ward
OSGi Best Practices - Tim WardOSGi Best Practices - Tim Ward
OSGi Best Practices - Tim Ward
 
Aceu2009 Osgi Productline
Aceu2009 Osgi ProductlineAceu2009 Osgi Productline
Aceu2009 Osgi Productline
 
Presentatie Duncan Rogers NMD2010 17 juni 2010
Presentatie Duncan Rogers NMD2010 17 juni 2010Presentatie Duncan Rogers NMD2010 17 juni 2010
Presentatie Duncan Rogers NMD2010 17 juni 2010
 
OSGi Training for Carbon Developers
OSGi Training for Carbon DevelopersOSGi Training for Carbon Developers
OSGi Training for Carbon Developers
 
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 20173 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Similar to Best Practices for Enterprise OSGi Applications - Emily Jiang

Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...
Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...
Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...
mfrancis
 
Travelling light for the long haul
Travelling light for the long haulTravelling light for the long haul
Travelling light for the long haul
Ian Robinson
 
Os gi introduction made by Ly MInh Phuong-SOC team
Os gi introduction made by Ly MInh Phuong-SOC teamOs gi introduction made by Ly MInh Phuong-SOC team
Os gi introduction made by Ly MInh Phuong-SOC team
Thuy_Dang
 
OSGi made simple - Fuse Application Bundles
OSGi made simple - Fuse Application BundlesOSGi made simple - Fuse Application Bundles
OSGi made simple - Fuse Application Bundles
Rob Davies
 
Scalable Deployment Patterns in WSO2 API Manager
Scalable Deployment Patterns in WSO2 API Manager Scalable Deployment Patterns in WSO2 API Manager
Scalable Deployment Patterns in WSO2 API Manager
WSO2
 

Similar to Best Practices for Enterprise OSGi Applications - Emily Jiang (20)

Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...
Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...
Hints and Tips for Modularizing Existing Enterprise Applications with OSGi - ...
 
Travelling light for the long haul
Travelling light for the long haulTravelling light for the long haul
Travelling light for the long haul
 
OSGi & Java EE: A hybrid approach to Enterprise Java Application Development,...
OSGi & Java EE: A hybrid approach to Enterprise Java Application Development,...OSGi & Java EE: A hybrid approach to Enterprise Java Application Development,...
OSGi & Java EE: A hybrid approach to Enterprise Java Application Development,...
 
Os gi introduction made by Ly MInh Phuong-SOC team
Os gi introduction made by Ly MInh Phuong-SOC teamOs gi introduction made by Ly MInh Phuong-SOC team
Os gi introduction made by Ly MInh Phuong-SOC team
 
Concierge: Bringing OSGi (Back) to Embedded Devices
Concierge: Bringing OSGi (Back) to Embedded DevicesConcierge: Bringing OSGi (Back) to Embedded Devices
Concierge: Bringing OSGi (Back) to Embedded Devices
 
What's new in p2 (2009)?
What's new in p2 (2009)?What's new in p2 (2009)?
What's new in p2 (2009)?
 
Spring Architecture | Advanced Java
Spring Architecture | Advanced JavaSpring Architecture | Advanced Java
Spring Architecture | Advanced Java
 
414: Build an agile CI/CD Pipeline for application integration
414: Build an agile CI/CD Pipeline for application integration414: Build an agile CI/CD Pipeline for application integration
414: Build an agile CI/CD Pipeline for application integration
 
Review Paper on Online Java Compiler
Review Paper on Online Java CompilerReview Paper on Online Java Compiler
Review Paper on Online Java Compiler
 
OSGi Overview TomTom DevDay May 2009
OSGi Overview TomTom DevDay May 2009OSGi Overview TomTom DevDay May 2009
OSGi Overview TomTom DevDay May 2009
 
Managing Change
Managing ChangeManaging Change
Managing Change
 
Opendaylight SDN Controller
Opendaylight SDN ControllerOpendaylight SDN Controller
Opendaylight SDN Controller
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 
Modular Architectures using Micro Services
Modular Architectures using Micro ServicesModular Architectures using Micro Services
Modular Architectures using Micro Services
 
OSGi Sticker Shock Eclipse Con 2010
OSGi Sticker Shock   Eclipse Con 2010OSGi Sticker Shock   Eclipse Con 2010
OSGi Sticker Shock Eclipse Con 2010
 
OSGi made simple - Fuse Application Bundles
OSGi made simple - Fuse Application BundlesOSGi made simple - Fuse Application Bundles
OSGi made simple - Fuse Application Bundles
 
[apidays LIVE HONK KONG] - OAS to Managed API in Seconds
[apidays LIVE HONK KONG] - OAS to Managed API in Seconds[apidays LIVE HONK KONG] - OAS to Managed API in Seconds
[apidays LIVE HONK KONG] - OAS to Managed API in Seconds
 
AliExpress’ Way to Microservices - microXchg 2017
AliExpress’ Way to Microservices  - microXchg 2017AliExpress’ Way to Microservices  - microXchg 2017
AliExpress’ Way to Microservices - microXchg 2017
 
Scalable Deployment Patterns in WSO2 API Manager
Scalable Deployment Patterns in WSO2 API Manager Scalable Deployment Patterns in WSO2 API Manager
Scalable Deployment Patterns in WSO2 API Manager
 
Spring core module
Spring core moduleSpring core module
Spring core module
 

More from mfrancis

Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
mfrancis
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
mfrancis
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
mfrancis
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
mfrancis
 

More from mfrancis (20)

Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
 
OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)OSGi and Java 9+ - BJ Hargrave (IBM)
OSGi and Java 9+ - BJ Hargrave (IBM)
 
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
 
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank LyaruuOSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
 
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
 
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
 
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
 
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
 
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
 
OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
 
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
 
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
 
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
 
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
 
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
 
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
 
How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)
 

Recently uploaded

Recently uploaded (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Best Practices for Enterprise OSGi Applications - Emily Jiang

  • 1. © 2010 IBM Corporation OSGi Application Best Practices Emily Jiang, emijiang@uk.ibm.com WebSphere Application Server OSGi Developer, Apache Aries Committer
  • 2. © 2011 IBM Corporation 2 AGENDA > Why OSGi? > What is OSGi? > How to best use OSGi?
  • 3. © 2011 IBM CorporationIBM ConfidentialOctober 18, 2012 > Jars have no modularization characteristics – No “jar scoped” access modifiers. – No means for a jar to declare its dependencies. – No versioning. JarJar PackagePackage ClassClass ClassClass ClassClass PackagePackage ClassClass ClassClass ClassClass PackagePackage ClassClass ClassClass ClassClass Modularization in Java
  • 4. 4 Problems with Global Java ClassPath J a va V M log 4 j ba rc ode 4 j a xis ba tik c om m ons de rby fop e zm orph fre e m a rke r httpunit ja ka rta jc l js on jdbm jdom je nks jpos 1 8 jython looks luc e ne m a il m x4 j na m ing je tty poi re s olve r rom e s e ria lize r s e rvle ts tom c a t ve loc ity w s -c om m ons xa la n w s dl4 j xe rc e s xm lg ra phic s xm lrpc xm la pis .. g e ronim 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 ounting a s s e tm a int ba s e bi c a ta lina c om m on oa g is orde r e ba y c onte nt da ta file e c om m e rc e e ntity g oog le ba s e ofbiz w idg e t m inila ng pa rty pos . produc t w orke ffort w orkflow … s unjc e _prov. plug in js s e jc e rt dns ns .. … C la s s N ot F ound E xc e ption B e g inB e g in He reHe re
  • 5. 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
  • 6. AGENDA > Why OSGi? > What is OSGi? > How to best use OSGi?
  • 7. © 2011 IBM Corporation7 JarJar 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
  • 8. 831/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 Loading Each 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.0 Bundle-ManifestVersion: 2 Bundle-Name: MyService bundle Bundle-SymbolicName: com.sample.myservice Bundle-Version: 1.0.0 Bundle-Activator: com.sample.myservice.Activator Import-Package: com.something.i.need;version="1.1.2" Export-Package: com.myservice.api;version="1.0.0" Bundle
  • 9. 9 OSGi Bundle Bundles have a dynamic lifecycle Can come and go independently Bundle
  • 10. • 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
  • 11. AGENDA > Why OSGi? > What is OSGi? > How to best use OSGi?
  • 12. 1231 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
  • 13. 1331 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
  • 14. 1431 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
  • 15. 1531 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
  • 16. 1631 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.
  • 17. 1731 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 B org.hal.a 1.0.0 Client A Imports: org.hal.a [1.0, 2.0) API B org.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
  • 18. 1831 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
  • 19. 1931 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
  • 20. 2031 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
  • 21. 2131 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
  • 22. 2231 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
  • 23. 2331 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
  • 24. 2431 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
  • 25. 2531 October 2012 BP6 – Make Bundles Loosely Coupled & Highly Cohesive Implementation org.hal.DBlog.impl DBAPI 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
  • 26. 2631 October 2012 Using services can be hard! @Override public 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); } }
  • 27. 2731 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 injected publishes service consumes service A static assembly and configuration of components (POJOs) Blueprint bundle OSGI-INF/blueprint/ blueprint.xml
  • 28. 2831 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.
  • 29. 2931 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
  • 30. 3031 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
  • 31. 31 Quiz – What best practices are used? Blogging Service Blog Persistence Service 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