Adding structure to complexapplication with OSGIVitalii Tymchyshyntivv00@gmail.com@tivv00
What is OSGi● OSGi is module system and service platformthat helps to build complex java applications● OSGi provides your ...
Program growthOne method One class One IoC context One OSGI application110100100010000100000
OSGi vs J2EE: J2EEJ2EE container: everythings bundledJTA Pool JMS JNDI ...Application
OSGi vs J2EE: OSGiOSGi container: anything can be replacedJTAPoolJMSJNDIIoCApp3App2App1
OSGi container levels● OSGi framework (e.g. Felix, Equinox)– Something you can directly embed in your code● OSGi container...
OSGi Bundle
OSGi Bundle● OSGI container loads application code frombundles● OSGi bundle is a JAR with additional data inmanifest● Mini...
Example manifestManifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-Name: HelloWorldBundle-SymbolicName: sample.HelloWorl...
Creating manifest for own jarsEditorManifest-Version: 1.0Bundle-Name: HelloWorldBundle-Localization: pluginImport-Package:...
Getting bundles for librariesCheckMANIFEST.MFDone!AlreadyA bundle!CheckYourcontainerHas OSGi versionfor the library!Checkw...
Creating bundles for libraries● You can also use bnd tool● In Apache Karaf you can use “wrap:” schemato call bnd tool in r...
Classes and classloaders● Each bundle can import and export somepackages● Its possible to have circular dependencies● Each...
Example applicationinterfaceapi.NameListenerclasshello.Implinterfaceapi.AskNameclassask.Implimplemented byimplemented byca...
Avoid circular dependencies!HelloBundleupdatedNameBundlerestartedapi.AskNameapi.NameListenerWhen updating bundleyou need t...
Activating your bundles
Bundle activators● Bundle can have direct or indirect activator toaccess OSGi features & implement itslifecycle● Direct ac...
Indirect activatorsContainer can have special features installed thatscan all starting bundles & activate specificcomponen...
Bundles collaboration
Bundles collaboration● Import classes – useful to get API or classeswithout shared state● Service registry – allows to pub...
Imports vs Registry● Imports are resolved before bundle start● Mandatory services are resolved duringretrieval. This creat...
Order problem resolutions: Sync
Order problem resolutions: Async
Order problem resolutions: CircularP.S. Dont do this :)
Structure with Spring & OSGiNameBundle HelloBundle<bean id=”asker”class=”ask.Impl”/>META-INF/spring/context.xml<osgi:servi...
Tips & Tricks
Testing● Split spring contextsinto OSGI and non-OSGI files● This will make it easyto test you bundlewithout OSGi● To test ...
Config Admin● OSGI provides Config Admin to helpconfiguring your application● Its very easy to use. In Spring-DM / Bluepri...
Config Admin
Assembling an applicationApache Karaf provides you with features● Features specify set of bundles to install● Features can...
Feature example<features><feature name=helloWorld version=${pom.version}><feature>printer</feature><bundle>mvn:commons-io/...
Q & A
Upcoming SlideShare
Loading in...5
×

Using OSGi for complex applications

2,474

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,474
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
59
Comments
0
Likes
0
Embeds 0
No embeds

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&apos;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&apos;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&apos;t need additional configuration at all Write yourself. You&apos;d better use other simplier option
  • Check it! It&apos;s highly possible your library jars are already packages as bundles Your container may have the library bundle in it&apos;s repository There are 3rd party bundle repositories (or maven groups), e.g.: http://ebr.springsource.com/ http://repo1.maven.org/maven2/org/apache/servicemix/bundles/
  • For synchronous start bundle start order can be specified with bundle start level title Synchronous start OSGI-&gt;+NameBundle\\n(Start Level 10): Start NameBundle\\n(Start Level 10)-&gt;+Registry: Register api.AskName Registry-&gt;-NameBundle\\n(Start Level 10): NameBundle\\n(Start Level 10)--&gt;-OSGI: OSGI-&gt;+HelloBundle\\n(Start Level 20): Start HelloBundle\\n(Start Level 20)-&gt;+Registry: Get api.AskName Registry-&gt;-HelloBundle\\n(Start Level 20): HelloBundle\\n(Start Level 20)--&gt;-OSGI:
  • For asynchronous start it&apos;s possible to wait for service to appear title Asynchronous start OSGI-&gt;&gt;+HelloBundle: Start HelloBundle-&gt;+Registry: Get api.AskName OSGI-&gt;&gt;+NameBundle: Start NameBundle-&gt;+Registry: Register api.AskName Registry-&gt;-NameBundle: NameBundle--&gt;-OSGI: Registry--&gt;-HelloBundle: HelloBundle--&gt;-OSGI:
  • r title Circular start OSGI-&gt;&gt;+HelloBundle: Start HelloBundle-&gt;+Registry: Get api.AskName OSGI-&gt;&gt;+NameBundle: Start NameBundle-&gt;&gt;Registry: Get api.AskListener NameBundle-&gt;+Registry: Register api.AskName Registry-&gt;-NameBundle: NameBundle--&gt;-OSGI: Registry--&gt;-HelloBundle: HelloBundle-&gt;+Registry: Register api.AskListener Registry--&gt;NameBundle: appeared api.AskListener Registry--&gt;-HelloBundle: HelloBundle--&gt;-OSGI:
  • Using OSGi for complex applications

    1. 1. Adding structure to complexapplication with OSGIVitalii Tymchyshyntivv00@gmail.com@tivv00
    2. 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. 3. Program growthOne method One class One IoC context One OSGI application110100100010000100000
    4. 4. OSGi vs J2EE: J2EEJ2EE container: everythings bundledJTA Pool JMS JNDI ...Application
    5. 5. OSGi vs J2EE: OSGiOSGi container: anything can be replacedJTAPoolJMSJNDIIoCApp3App2App1
    6. 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. 7. OSGi Bundle
    8. 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. 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. 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. 11. Getting bundles for librariesCheckMANIFEST.MFDone!AlreadyA bundle!CheckYourcontainerHas OSGi versionfor the library!CheckwellknownreposSee nextslide
    12. 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. 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. 14. Example applicationinterfaceapi.NameListenerclasshello.Implinterfaceapi.AskNameclassask.Implimplemented byimplemented bycallsasync answer
    15. 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. 16. Activating your bundles
    17. 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. 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. 19. Bundles collaboration
    20. 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. 21. Imports vs Registry● Imports are resolved before bundle start● Mandatory services are resolved duringretrieval. This creates start order problem.
    22. 22. Order problem resolutions: Sync
    23. 23. Order problem resolutions: Async
    24. 24. Order problem resolutions: CircularP.S. Dont do this :)
    25. 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. 26. Tips & Tricks
    27. 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. 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. 29. Config Admin
    30. 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. 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. 32. Q & A
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×