using osgi for complex applications

Download Using OSGi for complex applications

If you can't read please download the document

Upload: -

Post on 24-May-2015

2.326 views

Category:

Technology


2 download

TRANSCRIPT

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 HelloBundleMETA-INF/spring/context.xmlMETA-INF/spring/osgi.xmlMETA-INF/spring/context.xmlMETA-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 differentMETA-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 exampleprintermvn:commons-io/commons-io/1.4mvn:sample/api/${pom.version}mvn:sample/namebundle/${pom.version}mvn:sample/hellobundle/${pom.version}mvn:sample/configadmin-features/${pom.version}/cfg 32. Q & A