JavaOne 2010: OSGI Migrat

of 39/39
S314613: OSGI Migration headaches Krasimir Semerdzhiev SAP Labs Bulgaria Development Architect Peter Peshev SAP Labs Bulgaria Development Architect
  • date post

  • Category


  • view

  • download


Embed Size (px)


The session details are accessible over here: Session abstract Nowadays, every platform considers having the "OSGi supported" checkbox ticked and offers "easy" and "smooth" migration from the classical Java EE world to the new "modular" future. In spite of the multiple guidelines and best practices, migrating from a monolithic Java EE application to OSGi remains a pain and reveals a number of hidden and time-consuming pitfalls and hurdles. The session will guide you through the usual problems that every project faces on its way to holy modularity, providing helpful tips and tricks on how to quickly find your way through the maze of NoClassDeffFound, ClassCast, and other OSGi-specific errors.

Transcript of JavaOne 2010: OSGI Migrat

  • 1.S314613: OSGI Migration headaches Krasimir Semerdzhiev SAP Labs Bulgaria Development Architect Peter Peshev SAP Labs Bulgaria Development Architect

2. Agenda OSGI intro Headaches Painkiller options Q&A 3. Modularity 4. Alternative implementations VW Golf91-97 5. Good architecture and interchangeable minilangpartypos. productworkeffort workflow accounting assetmaintcontentbiebayecommerce googlebase guiapphhfacility manufact. marketingminervaoagisorderApplication layer common catalinabase datafile entitywidgetresolverrome serializer servletstomcatxalanbsf mailmx4jnamingjetty poiwsdl4jbshjenks jpos18 jython lookslucene xercesvelocity jakarta jcljsonjdbmjdomxmlapis ws-commonslog4jbarcode4jaxis batikcommons xmlrpc geronimo httpunitfreemarker ezmorphfopderby xmlgraphicsLibs.. Java VMrtjce jsseplugin sunjce_prov. dnsns.. 6. Problem: CLASSPATH Java VM partycommon resolver axisxercesBegin rtjce assetmaint hhfacilitycatalinabasemailjenksezmorph servlets xmlapisxmlrpc Here jssepos.datafile jakarta jetty xmlgraphicsplugin contententitylog4j looks sunjce_prov manufact.widgethttpunit jdbm. dnsnsproductmx4jbsf .. biromebatikbsh Classmarketing workflowjpos18fopvelocityworkeffortecommercejcltomcatws-commons Notebayoagisbarcode4jpoi geronimo Foundminerva freemarkerlucene ..Exceptionminilang googlebaseserializerjdomjson accounting order namingcommonsxalanguiapp ofbizjythonderbywsdl4j 7. The OSGi Alliance Started March 1999 as an initiative from ERICSSON, SUN and IBM Today: A stable Alliance with almost 100 members Now practically all of the Java Server vendors are represented there A standardization body 8. OSGi Framework LayersStaircase Applications can make useof different layers directly Module Layer definespackaging , deploymentdependancies amongmodules Lifecycle Layer controlslifecycyle operations of abundle 9. Module Layer: BundlesBundles Are JAR files Contain classes, other resources like HTML, images, specific metadata Contain manifest file (META-INF/MANIFEST.MF) with OSGI specificheaders used to manage the bundle Describes the content of the JAR file ExportImport package Require-Bundle 10. Module Layer: Runtime class loading load class 11. Class Loading Architecture: Class Space 12. Class Loading Architecture: DelegationBasic properties Parent delegation Wire importers and exporters There can be fragment bundles Class loading traversal based on importers and their wires 13. Lifecycle Layer Runtime representation of bundles Forevery bundle, the framework creates a Bundle java object Bundles are identified by Bundle ID, Bundle Symbolic Name and Bundle Location Bundle has a runtime state: 14. What is the story?WebWeb Module bundleWeb Module Web Modules Modules (war)(war) bundleWeb ModulesbundleWeb Modules State 2bundleLib Lib Lib Lib EAR fileEAR file OSGi frameworkJava EE compliant runtime bundle bundlebundlebundle bundlebundlebundle bundlebundlebundle bundlebundlebundle bundlebundleOSGi framework 15. What do we aim to get from OSGi? Standardized execution environment Java-based modularization Well defined module contract Strict dependency management at build and run time Manifest as a single point of truth Deterministic declarative runtime resolution or in other words Evolve from JARs/modules to interchangeable/reusable parts 16. Agenda OSGI intro Headaches Painkiller options Q&A 17. Step 1: Choose an OSGi runtime 18. Step 2: How to create bundles? Manifest-Version: 1.0 Bundle-Name: My First Bundle Bundle-SymbolicName: com.acme.myfirstbundle Bundle-Version: 1.0.0 Bundle-Description: Small Bundle Bundle-Vendor: ACME Bundle-Activator: com.acme.myfirstbundle.impl.Activator Bundle-Category: example Import-Package: org.osgi.framework Export-Package: com.acme.myfirstbundle.apiExport-Package ::= export(, export)* export ::= package-names(; parameter)* package-names ::= package-name(; package-name)* Import-Package ::= import(, import)* import ::= package-names(; parameter)* package-names ::= package-name(; package-name)* Require-Bundle ::= bundle-description ( ',' bundle-description )* bundle-description ::= symbolic-name (';' parameter )* 19. Step 2: How to create bundles? Lets take one example Apache Derby org.apache.derby.* iapi.sql impl.sql.catalog iapi.types impl.sql.execute.rts impl 20. Step 2: How to create bundles? Apache Derby impl.* gone org.apache.derby.* securitycatalog.types iapi.sql shareddatabaseiapi.sql.compile shared.commondiagiapi.sql.connshared.common.erroriapiiapi.sql.dependshared.common.sanityiapi.db iapi.sql.dictionarytoolsiapi.erroriapi.sql.execute vtiiapi.jdbc 21. Step 2: How to create bundles? Apache Derby what Eclipse uses out of it :-) org.apache.derby.*authenticationdatabaseiojdbcvti How to create manifests? By hand BND tool Apache Felix Maven plugin Eclipse PDE Bundlor or in other words Dont rush to define manifests, for modules you know nothing of! 22. Step 3: Lets try to activate the definition is missing!The forName() method in class Class. Possible causes? The findSystemClass method() in class ClassLoader.The loadClass() method in class ClassLoader Missing import-package Missing export-package Import version mismatch [1.2.3, 2.0); 1.2.3 Forgotten/missing component (really missing!) Wrong boot class delegation property Missing file system permissions (cant read the JARs) 23. Step 3: Separate a the first library as a bundlejava.lang.NoClassDefFoundError Class definition isfound, butinstantiation failed! Possible causes? Exception in static block Missing imported class (NoClassDefFoundException for it) Class version mismatch Hotspot reflection JIT optimizations (BugID 6265952 )(injection of sun.misc.Unsafe.defineClass)or in other words Dont rush blaming the framework! Classloading there is quite solid! 24. Step 4: Iterative refactoringMy bundle is not updated?!Possible causes? Didyou update the version? Youre replacing your bundle directly in the equinox plugins folder Package-imports do not fit to the new bundle version?or in other words Dont rush blaming the framework! ;) 25. Step 4: Iterative refactoringjava.lang.ClassCastErrorPossible causes? Additionalcopies (besides the system bundle) of the OSGi framework classes Undesired additional JARs in classpath Clash between RT.jar and application libs(previously used embedded jars) Java SE 5 vs. Java SE 6 does make a difference(a bunch of new packages added (StAX, JDBC 4.0, JAXB 2.0, etc.)or in other words Make sure you understand the root cause. (try using Eclipse Memory Analyzer) 26. Step 4: Iterative refactoringWorks in Eclipse, but doesnt work in Equinox Possible issues Different setting of org.osgi.parentClassloader app (default for Eclipse) boot ext fwk Certain expectations of org.osgi.framework.system.packages Different values of org.osgi.framework.bootdelegation property osgi.compatibility.bootdelegation (default for Eclipse is false) or in other words Dont be fooled by configuration differences 27. Step 4: Design flawsDependencies on startup orderPossible causes? Avoid lookup-error-fail patterns As a last resort you can use start levels Keep Activators really lean. Typically seen when extracting code from a web module with fixed Servlet.init() order. or in other wordsDont do anything needless in your activator. Thats not init()/start()! 28. Step 4: Design flawsClassloader resource assumptionsPossible issues JAR files Folder containing class files/packages URLs (to any of the above) new File(), after loader resources iteration bundle:xxx as a resourceor in other words Dont make assumptions on the classloader resources! 29. Step 4: Design flawsBoot delegation specifics org.osgi.framework.bootdelegation shortcut the OSGi resolution Adding* includes all sub-packages, but not the package itself. osgi.compatibility.bootdelegation - true/false or in other wordsMake sure that you understand the setup of your OSGi environment 30. Step 5: Design flawsSPI pattern limitations Possible issues Load a class, described in some meta-data file, used via interface META-INF/services/ Explicit import of the impl packages is required! 31. Step 5: Design flawsMy process exits unexpectedly?! Possible issues Lots of code is written with the assumption that it owns the world and not that it lives in a small corner of it. Enabling a Security Manager which traces System.exit() and Runtime.halt() calls might help 32. Step 5: Design flawsRegistering objects in the VM has direct impact on the ability to stop a bundle Some of those have no Possible issuesunregister methods! System in and out streams Shutdown hooks MBean server factory Log4J formatters JNDI provider/factoryor in other wordsDont try to do the job of the underlying middleware. 33. Step 5: Design flawsContextClassLoader usage Possible issues Some code written to run in a web app might make some assumptions on the Context classloader. One example here is JNDI. or in other wordsConsider environment dependencies when extracting code into bundles 34. Agenda OSGI intro Headaches Painkiller options Q&A 35. Eclipse Virgo shell improvementsSupportability commands Nested OSGi frameworks Classloading 36. SpringSource Tool Suite 37. Other relevant sessions Tue 12:00 PM Developing OSGi-Enabled Java EE Applications Tue 6:00 PMPatterns for modularity Tue 6:00 PMOSGI BOF Tue 9:00 PMOSGi at a Large-Scale Enterprise: Lessons from eBay Wed 10:00 AM Java EE OSGi applications : Design and Deployment for On-Premises and Cloud Wed 1:00 PMOSGi and JSR 294: Module Keyword Wed 4:45 PMVisualizing the Science of Conversation with JavaFX and OSGi to Save Lives Thu 3:30 PMCreating Modular Applications with Apache Aries and OSGi Thu 3:30 PMDeveloping Rich Modular Clients with Java, JavaFX and OSGi Technology 38. Q&A? 39. Thank you!Krasimir Semerdzhiev [email protected] [email protected]