OSGi Best Practices Emily Jiang, IBM OSGi Developer, Apache Aries Committer [email_address]
AGENDA Why OSGi? What is OSGi? How to best use OSGi?
Jars have no modularization  characteristics No “jar scoped” access modifiers.
No means for a jar to declare its dependencies.
No versioning. Modularization in Java Jar Package Class Class Class Package Class Class Class Package Class Class Class
Problems with Global Java ClassPath Java VM log4j barcode4j axis batik commons derby fop ezmorph freemarker httpunit jakarta jcl json jdbm jdom jenks jpos18 jython looks lucene mail mx4j naming jetty poi resolver rome serializer servlets tomcat velocity ws-commons xalan wsdl4j xerces xmlgraphics xmlrpc xmlapis .. geronimo bsh bsf guiapp hhfacility manufact. marketing minerva accounting assetmaint base bi catalina common oagis order ebay content datafile ecommerce entity googlebase ofbiz widget minilang party pos. product workeffort workflow … sunjce_prov. plugin jsse jce rt dnsns .. … Class Not Found Exception Begin Here
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?
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 Jar Jar Explicit exports Explicit dependencies Package Class Class Class Class Class Class Package Class Class Class Class Class Class
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
OSGi Bundle Bundles have a dynamic lifecycle Can come and go independently Bundle
OSGi Service Registry 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
AGENDA Why OSGi? What is OSGi? How to best use OSGi?
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
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
BP2 - Split Package examples API  B 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 API  A  org.hal .api 1.0.0 org.hal.a pi 1.0.0
BP2 - Split Package examples API A Implementation C org.hal.a 1.0.0 Figure 2. A complete package exported from a single bundle maintains high bundle cohesion org.hal.api 1.0.0
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

More Related Content

PDF
Java 9 New Features
PDF
Java 10 New Features
PDF
candi-binding-tutorial
PPTX
MAX 2008 - Building your 1st AIR application
PDF
Introduction to CDI
PDF
Intro To OSGi
ZIP
Practical OSGi
Java 9 New Features
Java 10 New Features
candi-binding-tutorial
MAX 2008 - Building your 1st AIR application
Introduction to CDI
Intro To OSGi
Practical OSGi

What's hot (16)

PPTX
Spring core
PDF
Benefits of OSGi in Practise
ODP
Liferay Module Framework
PDF
Spring - CDI Interop
PPS
Java rmi example program with code
PDF
Force.com migration utility
PDF
Corejava ratan
PPTX
Java Modularity with OSGi
PDF
Open Services Gateway Initiative (OSGI)
PDF
Migrating a JSF-Based Web Application from Spring 3 to Java EE 7 and CDI
ODT
Types of Dependency Injection in Spring
RTF
Readme
ODP
Different Types of Containers in Spring
PPTX
Generic UXD Legos - Selenium Conference 2015
PPTX
Mulesoftmeetup4th july
PDF
Sap tutorial
Spring core
Benefits of OSGi in Practise
Liferay Module Framework
Spring - CDI Interop
Java rmi example program with code
Force.com migration utility
Corejava ratan
Java Modularity with OSGi
Open Services Gateway Initiative (OSGI)
Migrating a JSF-Based Web Application from Spring 3 to Java EE 7 and CDI
Types of Dependency Injection in Spring
Readme
Different Types of Containers in Spring
Generic UXD Legos - Selenium Conference 2015
Mulesoftmeetup4th july
Sap tutorial

Viewers also liked (20)

PPT
Living Architecture
PPS
祝福世界好友周快樂!
PPTX
BTD2015 - Your Place In DevTOps is Finding Solutions - Not Just Bugs!
PPTX
Bài giảng Đường lối quân sự
PDF
Mla citation guide & citation data forms
PDF
Arh1000 sm2015 syllabus
PDF
New Realism
PDF
2003 Winter Newsletter
PDF
Hum2220 art of the stone age
PPTX
Clothes & markets
PDF
希望新聞特別版のご紹介
PPT
Проект Жизнь
PDF
Wundt, w. (1897)
PPT
Charleston Conference - Launching an ePreferred Approval Plan
PDF
Arh2050 fa2015 proust questionnaire
PDF
Unbound:Endless War
PPT
Robin hood
PPT
Power point harp seal
PPT
Оценка персонала
PPTX
London web perfug_performancefocused_devops_feb2014
Living Architecture
祝福世界好友周快樂!
BTD2015 - Your Place In DevTOps is Finding Solutions - Not Just Bugs!
Bài giảng Đường lối quân sự
Mla citation guide & citation data forms
Arh1000 sm2015 syllabus
New Realism
2003 Winter Newsletter
Hum2220 art of the stone age
Clothes & markets
希望新聞特別版のご紹介
Проект Жизнь
Wundt, w. (1897)
Charleston Conference - Launching an ePreferred Approval Plan
Arh2050 fa2015 proust questionnaire
Unbound:Endless War
Robin hood
Power point harp seal
Оценка персонала
London web perfug_performancefocused_devops_feb2014

Similar to Java Tech & Tools | OSGi Best Practices | Emily Jiang (20)

PDF
Best Practices for Enterprise OSGi Applications - Emily Jiang
PPT
Os gi introduction made by Ly MInh Phuong-SOC team
PDF
Moved to https://slidr.io/azzazzel/leveraging-osgi-to-create-extensible-plugi...
PPTX
IBM Cloud Pak for Integration 2020.2.1 installation
PPT
Managing Change
ODP
Dependency Injection, Zend Framework and Symfony Container
PPTX
Introduction to OSGi
PPT
Enabling modularization through OSGi and SpringDM
ODP
Frankenstein's IDE: NetBeans and OSGi
PDF
AtoZ about TYPO3 v8 CMS
PPT
OSGi Overview TomTom DevDay May 2009
PDF
Creating a modern web application using Symfony API Platform, ReactJS and Red...
PPT
The Web on OSGi: Here's How
PDF
Modular Architectures using Micro Services
PPT
Apache Aries: A blueprint for developing with OSGi and JEE
PPTX
PPT
Osgi Webinar
PDF
Itb 2021 - Bulding Quick APIs by Gavin Pickin
PPT
OSGi For Java Infrastructures [5th IndicThreads Conference On Java 2010, Pune...
PPTX
OSGi Training for Carbon Developers
Best Practices for Enterprise OSGi Applications - Emily Jiang
Os gi introduction made by Ly MInh Phuong-SOC team
Moved to https://slidr.io/azzazzel/leveraging-osgi-to-create-extensible-plugi...
IBM Cloud Pak for Integration 2020.2.1 installation
Managing Change
Dependency Injection, Zend Framework and Symfony Container
Introduction to OSGi
Enabling modularization through OSGi and SpringDM
Frankenstein's IDE: NetBeans and OSGi
AtoZ about TYPO3 v8 CMS
OSGi Overview TomTom DevDay May 2009
Creating a modern web application using Symfony API Platform, ReactJS and Red...
The Web on OSGi: Here's How
Modular Architectures using Micro Services
Apache Aries: A blueprint for developing with OSGi and JEE
Osgi Webinar
Itb 2021 - Bulding Quick APIs by Gavin Pickin
OSGi For Java Infrastructures [5th IndicThreads Conference On Java 2010, Pune...
OSGi Training for Carbon Developers

More from JAX London (20)

PDF
Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
ODP
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
PDF
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
PDF
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
PDF
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
PDF
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
PDF
Spring Day | Identity Management with Spring Security | Dave Syer
PDF
Spring Day | Spring and Scala | Eberhard Wolff
PDF
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
PPT
Keynote | The Rise and Fall and Rise of Java | James Governor
PPTX
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
PDF
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
PDF
Java Tech & Tools | Social Media in Programming in Java | Khanderao Kand
PDF
Java Tech & Tools | Just Keep Passing the Message | Russel Winder
PDF
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
PDF
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
ODP
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
PDF
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
KEY
Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concu...
PDF
Java Core | Java 8 and OSGi Modularisation | Tim Ellison & Neil Bartlett
Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Identity Management with Spring Security | Dave Syer
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
Keynote | The Rise and Fall and Rise of Java | James Governor
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Java Tech & Tools | Social Media in Programming in Java | Khanderao Kand
Java Tech & Tools | Just Keep Passing the Message | Russel Winder
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concu...
Java Core | Java 8 and OSGi Modularisation | Tim Ellison & Neil Bartlett

Recently uploaded (20)

PDF
Human Computer Interaction Miterm Lesson
PPTX
How to use fields_get method in Odoo 18
PDF
Fitaura: AI & Machine Learning Powered Fitness Tracker
PDF
1_Keynote_Breaking Barriers_한계를 넘어서_Charith Mendis.pdf
PDF
Applying Agentic AI in Enterprise Automation
PPTX
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
FASHION-DRIVEN TEXTILES AS A CRYSTAL OF A NEW STREAM FOR STAKEHOLDER CAPITALI...
PPTX
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
PDF
Child-friendly e-learning for artificial intelligence education in Indonesia:...
PDF
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
PDF
Examining Bias in AI Generated News Content.pdf
PDF
State of AI in Business 2025 - MIT NANDA
PDF
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
PDF
Domain-specific knowledge and context in large language models: challenges, c...
PDF
Altius execution marketplace concept.pdf
PDF
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
PDF
Addressing the challenges of harmonizing law and artificial intelligence tech...
PDF
substrate PowerPoint Presentation basic one
PDF
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
Human Computer Interaction Miterm Lesson
How to use fields_get method in Odoo 18
Fitaura: AI & Machine Learning Powered Fitness Tracker
1_Keynote_Breaking Barriers_한계를 넘어서_Charith Mendis.pdf
Applying Agentic AI in Enterprise Automation
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
Presentation - Principles of Instructional Design.pptx
FASHION-DRIVEN TEXTILES AS A CRYSTAL OF A NEW STREAM FOR STAKEHOLDER CAPITALI...
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
Child-friendly e-learning for artificial intelligence education in Indonesia:...
The Digital Engine Room: Unlocking APAC’s Economic and Digital Potential thro...
Examining Bias in AI Generated News Content.pdf
State of AI in Business 2025 - MIT NANDA
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
Domain-specific knowledge and context in large language models: challenges, c...
Altius execution marketplace concept.pdf
Revolutionizing recommendations a survey: a comprehensive exploration of mode...
Addressing the challenges of harmonizing law and artificial intelligence tech...
substrate PowerPoint Presentation basic one
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf

Java Tech & Tools | OSGi Best Practices | Emily Jiang

  • 1. OSGi Best Practices Emily Jiang, IBM OSGi Developer, Apache Aries Committer [email_address]
  • 2. AGENDA Why OSGi? What is OSGi? How to best use OSGi?
  • 3. Jars have no modularization characteristics No “jar scoped” access modifiers.
  • 4. No means for a jar to declare its dependencies.
  • 5. No versioning. Modularization in Java Jar Package Class Class Class Package Class Class Class Package Class Class Class
  • 6. Problems with Global Java ClassPath Java VM log4j barcode4j axis batik commons derby fop ezmorph freemarker httpunit jakarta jcl json jdbm jdom jenks jpos18 jython looks lucene mail mx4j naming jetty poi resolver rome serializer servlets tomcat velocity ws-commons xalan wsdl4j xerces xmlgraphics xmlrpc xmlapis .. geronimo bsh bsf guiapp hhfacility manufact. marketing minerva accounting assetmaint base bi catalina common oagis order ebay content datafile ecommerce entity googlebase ofbiz widget minilang party pos. product workeffort workflow … sunjce_prov. plugin jsse jce rt dnsns .. … Class Not Found Exception Begin Here
  • 7. 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
  • 8. AGENDA Why OSGi? What is OSGi? How to best use OSGi?
  • 9. What is OSGi? “ The dynamic module system for Java” Mature 10-year old technology
  • 10. Governed by OSGi Alliance: http://www.osgi.org
  • 11. 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 Jar Jar Explicit exports Explicit dependencies Package Class Class Class Class Class Class Package Class Class Class Class Class Class
  • 12. OSGi Bundles and Class Loading OSGi Bundle – A jar containing: Classes and resources.
  • 13. OSGi Bundle manifest. What’s in the manifest: Bundle-Version: Multiple versions of bundles can live concurrently.
  • 14. Import-Package: What packages from other bundles does this bundle depend upon?
  • 15. Export-Package: What packages from this bundle are visible and reusable outside of the bundle? Class Loading Each bundle has its own loader.
  • 16. No flat or monolithic classpath.
  • 17. Class sharing and visibility decided by declarative dependencies, not by class loader hierarchies.
  • 18. 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
  • 19. OSGi Bundle Bundles have a dynamic lifecycle Can come and go independently Bundle
  • 20. OSGi Service Registry Service An object associated with a list of classes (usually interfaces) it provides
  • 21. Dynamic (can come and go), framed by bundle lifecycle
  • 22. Services are the primary means of collaboration between bundles. S
  • 23. AGENDA Why OSGi? What is OSGi? How to best use OSGi?
  • 24. BP1 - Use Import-Package not Require-Bundle Require-Bundle Tightly coupled with a particular bundle with the specified symbolic name and version
  • 27. Bundle version management Import-Package Can wire to any bundles exporting the specified package
  • 29. Only import the package you need
  • 30. 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
  • 31. 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
  • 32. BP2 - Split Package examples API B 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 API A org.hal .api 1.0.0 org.hal.a pi 1.0.0
  • 33. BP2 - Split Package examples API A Implementation C org.hal.a 1.0.0 Figure 2. A complete package exported from a single bundle maintains high bundle cohesion org.hal.api 1.0.0
  • 34. 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.
  • 35. Minor – API enhancement, e.g. adding methods to an API
  • 36. Micro – bug fixing
  • 37. Qualifier – identifier such as timestamp
  • 38. Example: 1.1.2.20101122am, 1.0.0, 2.0 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.
  • 39. BP3 - Version bundles and packages examples Figure 3: A client and an implementation can use either of two equivalently versioned packages Figure 4: How a client and implementation are affected differently by a minor API version change 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)
  • 40. BP3 - Version bundles and packages examples Figure 5. How clients and implementations are similarly affected by a major API version change API A 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)
  • 41. BP4 - Separate API from Implementations Why? Great flexibility
  • 42. Many implementation bundles -> enable more services provided
  • 43. Reduce package dependencies -> reduce circular dependencies How? Put API classes in one bundle
  • 44. Put implementation classes in a separate bundle
  • 45. 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 Both API and implementation packages imported by client Client org.hal.b.client
  • 46. BP5 - Share services not implementations Use the OSGi service registry to construct instances
  • 47. Why? Able to obtain an instance of an implementation without knowing which one
  • 48. Achieve a loosely coupling of client, API and implementation How? Register an instance of the API interface in the OSGi service registry
  • 49. Register an implementation of the OSGi ServiceFactory interface in the OSGi service registry.
  • 50. BP4 & 5 - examples org.hal.myapi 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 API Client org.hal.b.client
  • 51. BP4 & 5 - examples Figure 8: Well designed provider bundles where the API and implementation classes have been separated 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
  • 52. 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 , &quot;(objectClass=org.osgi.service.log.LogService)&quot; ); ServiceReference ref = ctx.getServiceReference(LogService. class .getName()); if (ref != null ) { ls .set((LogService) ctx.getService(ref)); lr .set(ref); } }
  • 53. BP6 - Use Blueprint Specifies a Dependency Injection container, standardizing established Spring conventions
  • 54. 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.
  • 55. The Blueprint DI container can be 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
  • 56. BP6 - 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.
  • 57. “ singleton” scope is the default.
  • 58. BP6- 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
  • 59. service can change over time
  • 60. can be temporarily absent without the bundle caring
  • 61. managed by Blueprint container
  • 62. BP7 – 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 org.hal.fslog.impl Figure 9. Poorly purposed system where a single bundle provides multiple implementations of the same API API org.hal.log.api
  • 63. BP7 – 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
  • 64. BP8 – Use bundle repositories
  • 65. Summary – OSGi Best Practices BP1 - Use Import-Package instead of Require-Bundle
  • 66. BP2 - Avoid split packages
  • 67. BP3 - Version bundles and packages
  • 68. BP4 - Separate API from implementations
  • 69. BP5 - Share services not implementations
  • 70. BP6 - Use Blueprint
  • 71. BP7 - Make bundles loosely coupled & highly cohesive
  • 72. BP8 - Use Bundle Repositories
  • 73. 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
  • 75. Additional References OSGi Best Practices www.ibm.com/developerworks/websphere/techjournal/1007_charters/1007_charters.html Enterprise OSGi YouTube Channel: www.youtube.com/user/EnterpriseOSGi
  • 76. Copyright and Trademarks © IBM Corporation 2011. All Rights Reserved. IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corp., registered in many jurisdictions worldwide. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at “Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml.

Editor's Notes

  • #11: WSAD512_IntroToPortal.ppt Page of 32
  • #24: .
  • #27: On the provider side, the BillingServiceImpl is another POJO implementing a Java interface for which a service is registered by the Blueprint container when the Billing bundle is started. By default beans are created with “singleton” scope which means only a single instance is created by the container. If the bean maintains any state then it can be declared with “prototype” scope so that the container creates a new instance each time it needs to inject the dependency into a client.
  • #28: Here is a simple example of an eCommerce bundle which contains a “shop” bean which uses a BillingService provided by another bundle. The Blueprint container locates the Billing Service provider and injects it into the shop bean at runtime. OSGi services are dynamic so if the service provider can be changed then the blueprint container is able to dynamically rewire the reference to a new service without impacting the shop bean instance.
  • #33: What best practices used? BP4 - Separate API from implementations BP5 - Share services not implementations BP6 – Make bundles loosely coupled &amp; highly cohesive BP7 - Use Blueprint If you show the source code, the following three best practices can be included: BP1 - Use Import-Package instead of Require-Bundle BP2 - Avoid split packages BP3 - Version bundles and packages