Your SlideShare is downloading. ×
Using OSGi for complex applications
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Using OSGi for complex applications


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide
  • In OSGi all but very core services are regular bundles that can be replaced In fact, even OSGi implementation can be repaced in Karaf OSGi container J2EE server provides you with a whole bunch of services So, in given server you are tied to it's services. It can be hard, if not impossible to replace some
  • In OSGi all but very core services are regular bundles that can be replaced In fact, even OSGi implementation can be repaced in Karaf OSGi container J2EE server provides you with a whole bunch of services So, in given server you are tied to it's services. It can be hard, if not impossible to replace some
  • Use bnd tool directly. If will detect most things automatically Use maven-bundle-plugin. It uses bnd under the hood, passing additional information from maven. Often you don't need additional configuration at all Write yourself. You'd better use other simplier option
  • Check it! It's highly possible your library jars are already packages as bundles Your container may have the library bundle in it's repository There are 3rd party bundle repositories (or maven groups), e.g.:
  • For synchronous start bundle start order can be specified with bundle start level title Synchronous start OSGI->+NameBundle\\n(Start Level 10): Start NameBundle\\n(Start Level 10)->+Registry: Register api.AskName Registry->-NameBundle\\n(Start Level 10): NameBundle\\n(Start Level 10)-->-OSGI: OSGI->+HelloBundle\\n(Start Level 20): Start HelloBundle\\n(Start Level 20)->+Registry: Get api.AskName Registry->-HelloBundle\\n(Start Level 20): HelloBundle\\n(Start Level 20)-->-OSGI:
  • For asynchronous start it's possible to wait for service to appear title Asynchronous start OSGI->>+HelloBundle: Start HelloBundle->+Registry: Get api.AskName OSGI->>+NameBundle: Start NameBundle->+Registry: Register api.AskName Registry->-NameBundle: NameBundle-->-OSGI: Registry-->-HelloBundle: HelloBundle-->-OSGI:
  • r title Circular start OSGI->>+HelloBundle: Start HelloBundle->+Registry: Get api.AskName OSGI->>+NameBundle: Start NameBundle->>Registry: Get api.AskListener NameBundle->+Registry: Register api.AskName Registry->-NameBundle: NameBundle-->-OSGI: Registry-->-HelloBundle: HelloBundle->+Registry: Register api.AskListener Registry-->NameBundle: appeared api.AskListener Registry-->-HelloBundle: HelloBundle-->-OSGI:
  • Transcript

    • 1. Adding structure to complexapplication with OSGIVitalii
    • 2. What is OSGi● OSGi is module system and service platformthat helps to build complex java applications● OSGi provides your application with means to:– Deploy– Configure– Modularize– Control
    • 3. Program growthOne method One class One IoC context One OSGI application110100100010000100000
    • 4. OSGi vs J2EE: J2EEJ2EE container: everythings bundledJTA Pool JMS JNDI ...Application
    • 5. OSGi vs J2EE: OSGiOSGi container: anything can be replacedJTAPoolJMSJNDIIoCApp3App2App1
    • 6. OSGi container levels● OSGi framework (e.g. Felix, Equinox)– Something you can directly embed in your code● OSGi container (e.g. Karaf)– Startable vanilla application server without muchpreinstalled features● Enterprise integration container (e.g.ServiceMix)– Similar to J2EE server — provides you withprechecked set of features, etiher preinstalled orreadily available
    • 7. OSGi Bundle
    • 8. OSGi Bundle● OSGI container loads application code frombundles● OSGi bundle is a JAR with additional data inmanifest● Minimal manifest data set consist of bundleidentification and package import/exportstatements● OSGi container will not load vanilla jar without atleast basic bundle information
    • 9. Example manifestManifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: HelloWorldBundle-SymbolicName: sample.HelloWorldBundle-Version: 1.0.0Bundle-Activator: hello.ImplBundle-Localization: pluginImport-Package: org.osgi.framework;version="1.3.0"
    • 10. Creating manifest for own jarsEditorManifest-Version: 1.0Bundle-Name: HelloWorldBundle-Localization: pluginImport-Package: org.osgi.framework...BND toolBundle-Name: HelloWorldBundle-Localization: plugin...Maven-bunle-pluginBundle-Localization: plugin...
    • 11. Getting bundles for librariesCheckMANIFEST.MFDone!AlreadyA bundle!CheckYourcontainerHas OSGi versionfor the library!CheckwellknownreposSee nextslide
    • 12. Creating bundles for libraries● You can also use bnd tool● In Apache Karaf you can use “wrap:” schemato call bnd tool in runtimekaraf@root> osgi:install wrap:mvn:antlr/antlr/2.7.7Bundle ID: 56karaf@root> headers 56wrap_mvn_antlr_antlr_2.7.7 (56)-------------------------------Manifest-Version = 1.0Bnd-LastModified = 1366218263809Tool = Bnd-0.0.357Originally-Created-By = 1.4.2_09 (Apple Computer, Inc.)Generated-By-Ops4j-Pax-From = wrap:mvn:antlr/antlr/2.7.7...
    • 13. Classes and classloaders● Each bundle can import and export somepackages● Its possible to have circular dependencies● Each bundle has its own classloader● There is no classloader hierarchy● Bundle activator thread has contextclassloader equal to bundle classloader
    • 14. Example applicationinterfaceapi.NameListenerclasshello.Implinterfaceapi.AskNameclassask.Implimplemented byimplemented bycallsasync answer
    • 15. Avoid circular dependencies!HelloBundleupdatedNameBundlerestartedapi.AskNameapi.NameListenerWhen updating bundleyou need to restart all bundlesthat import from itHelloBundle NameBundleapi.AskName api.NameListenerapi.AskNameapi.NameListenerAPI bundleHelloBundle NameBundleNow unless your APIve changed,you can update single bundleHelloBundleupdatedNameBundleapi.AskName api.NameListenerAPI bundle
    • 16. Activating your bundles
    • 17. Bundle activators● Bundle can have direct or indirect activator toaccess OSGi features & implement itslifecycle● Direct activator is a class, referenced inmanifest. It will be instantiated & called duringbundle start and stop events● Direct activator is synchronous, while manyindirect ones are asynchronous by default
    • 18. Indirect activatorsContainer can have special features installed thatscan all starting bundles & activate specificcomponent. Some examples are:● Spring-DM – META-INF/spring/* will form a springcontext● Blueprint – OSGI-INF/blueprint/* will form ablueprint context● JPA – META-INF/persistence.xml will publish JPAentity manager factory
    • 19. Bundles collaboration
    • 20. Bundles collaboration● Import classes – useful to get API or classeswithout shared state● Service registry – allows to publish or retrieveservices by interface and optional attributesNameBundle HelloBundleImportsapi.*Importsapi.*API bundleService registryPublishesask.Implasapi.AskNameUsesapi.AskName
    • 21. Imports vs Registry● Imports are resolved before bundle start● Mandatory services are resolved duringretrieval. This creates start order problem.
    • 22. Order problem resolutions: Sync
    • 23. Order problem resolutions: Async
    • 24. Order problem resolutions: CircularP.S. Dont do this :)
    • 25. Structure with Spring & OSGiNameBundle HelloBundle<bean id=”asker”class=”ask.Impl”/>META-INF/spring/context.xml<osgi:service ref=”asker”interface=”api.AskName”/>META-INF/spring/osgi.xml<bean class=”hello.Impl”><property name=”asker” ref=”asker”/></bean>META-INF/spring/context.xml<osgi:reference id=”asker”interface=”api.AskName”/>META-INF/spring/osgi.xmlOSGI registry• Service from NameBundlewith interfaceapi.AskName
    • 26. Tips & Tricks
    • 27. Testing● Split spring contextsinto OSGI and non-OSGI files● This will make it easyto test you bundlewithout OSGi● To test wholeapplication, you canuse Pax Exam● It will start OSGicontainer and injectyour test code● Remember to selectPax version: 1,2 and3 are very different<bean id=”asker”class=”ask.Impl”/>META-INF/spring/context.xml
    • 28. Config Admin● OSGI provides Config Admin to helpconfiguring your application● Its very easy to use. In Spring-DM / Blueprintit integrates with Placeholder configurer● It has advanced features, e.g. to createservice factories● You can provide meta data to make it userfriendly
    • 29. Config Admin
    • 30. Assembling an applicationApache Karaf provides you with features● Features specify set of bundles to install● Features can refer other required features, thusforming nested dependencies● Features can refer configuration files to deploy● You can generate features with maven plugin● After all, features are simple XMLs that can bedeployed to maven
    • 31. Feature example<features><feature name=helloWorld version=${pom.version}><feature>printer</feature><bundle>mvn:commons-io/commons-io/1.4</bundle><bundle>mvn:sample/api/${pom.version}</bundle><bundle>mvn:sample/namebundle/${pom.version}</bundle><bundle>mvn:sample/hellobundle/${pom.version}</bundle><configfile finalname="/etc/HelloConfig.cfg">mvn:sample/configadmin-features/${pom.version}/cfg</configfile></feature></features>
    • 32. Q & A