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

Using OSGi for complex applications

on

  • 1,922 views

 

Statistics

Views

Total Views
1,922
Views on SlideShare
1,508
Embed Views
414

Actions

Likes
0
Downloads
41
Comments
0

2 Embeds 414

http://jeeconf.com 355
https://twitter.com 59

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.: 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->+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:

Using OSGi for complex applications Using OSGi for complex applications Presentation Transcript

  • 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 application with means to:– Deploy– Configure– Modularize– Control
  • 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 (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
  • OSGi Bundle
  • 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
  • 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"
  • 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...
  • Getting bundles for librariesCheckMANIFEST.MFDone!AlreadyA bundle!CheckYourcontainerHas OSGi versionfor the library!CheckwellknownreposSee nextslide
  • 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...
  • 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
  • Example applicationinterfaceapi.NameListenerclasshello.Implinterfaceapi.AskNameclassask.Implimplemented byimplemented bycallsasync answer
  • 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
  • Activating your bundles
  • 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
  • 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
  • Bundles collaboration
  • 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
  • Imports vs Registry● Imports are resolved before bundle start● Mandatory services are resolved duringretrieval. This creates start order problem.
  • 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: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
  • Tips & Tricks
  • 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
  • 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
  • Config Admin
  • 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
  • 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>
  • Q & A