Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sam Brannen on Modular Web Applications with OSGi

Sam Brannen on Modular Web Applications with OSGi

More Decks by Enterprise Java User Group Austria

Other Decks in Technology

Transcript

  1. Speaker Profile   Software Consultant   Spring Framework Core Developer

      Previous SpringSource dm Server™ developer: OSGi- enabled Web deployment models, Tomcat integration, Test Framework   Java developer with 10+ years' experience   Regular speaker at conferences on Spring, dm Server, Java, OSGi, and testing   Co-author of Spring in a Nutshell; chief technical reviewer for Spring Recipes 2 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  2. The Challenge? Monolithic WAR deployments hinder both development and runtime

    modularity and promote library bloat. The Solution! Harness the flexibility of the OSGi Web Container standard, Spring, Spring-DM, and the SpringSource dm Server™. Overview 3 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  3. Agenda   Status Quo   OSGi   Spring DM  

    OSGi Web Container   SpringSource dm Server™   Demos   Q&A 4 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  4. Status Quo 5 Copyright 2008-2009 Sam Brannen and SpringSource. Copying,

    publishing or distributing without express written permission is prohibited.
  5. Standard Java EE WAR   Java Servlet 2.5   WAR

    format   Static content   Dynamic content   WEB-INF   WEB-INF/classes   WEB-INF/lib   All-in-one deployment model   EAR   Container shared libraries 6 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  6.  Spring MVC 2.5 based "Hello World" web application  Domain model:

    HelloMessage  Service layer: HelloService, HelloServiceImpl  Presentation layer:   Controllers: HelloController   Views: JSP + JSTL Introduction to Demos 7 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  7. DEMO Standard WAR 8 Copyright 2008-2009 Sam Brannen and SpringSource.

    Copying, publishing or distributing without express written permission is prohibited.
  8. OSGi 9 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing

    or distributing without express written permission is prohibited.
  9. It's a module system   Partition a system into a

    number of modules – “bundles”   Dynamic: bundles can be installed, started, stopped, uninstalled and updated ... at runtime!   Strict visibility rules   Resolution process satisfies dependencies of a module   Versioning   Packages & Services   Deploy multiple versions of a module 10 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  10. It's even service-oriented   Bundles can publish services… dynamically!  

    Service Registry allows other bundles to consume services   Services come and go at runtime   … transparently when using Spring-DM 11 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  11. OSGi Bundle   The fundamental unit of deployment and modularity

    in OSGi   Just a JAR file   with additional entries in META-INF/MANIFEST.MF   Common manifest headers:   Bundle-SymbolicName   Bundle-Version   Bundle-Name   Bundle-ManifestVersion   Export-Package   Import-Package 12 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  12. Export & Import Packages Declare package-level visibility and dependencies of

    your bundle. Export-Package: com.xyz.bar Export-Package: com.xyz.bar;version="1.0.5" Import-Package: com.xyz.foo Import-Package: com.xyz.foo;version="1.0.3" Import-Package: com.xyz.foo;version="[1.0.3,1.0.3]" Import-Package: com.xyz.foo;version="[1.0.3,1.1.0)", com.xyz.bar;version="[1.0.3,2.0.0)" >= 1.0.3; e.g., 1.0.3, 1.0.3.GA, 1.0.4, 2.0, etc. 13 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  13. ServiceRegistration reg = bundleContext.registerService( Bar.class.getName(), myBarService); ... reg.unregister(); Publish a

    Service 14 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  14. Consume a Service ServiceReference ref = bundleContext.getServiceReference( Bar.class.getName()); Bar bar

    = (Bar) bundleContext.getService(ref); ... bundleContext.ungetService(ref); // bar should no longer be used here Complex… Potential resource leaks! 15 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  15. Implementations & Tools   OSGi implementations:   Equinox   Knopflerfish

      Felix   IDEs   Eclipse PDE   SpringSource Tool Suite   Build support:   BND   Bundlor 16 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  16. Spring DM 17 Copyright 2008-2009 Sam Brannen and SpringSource. Copying,

    publishing or distributing without express written permission is prohibited.
  17. Spring-DM: App. Context   Configuration files in /META-INF/spring   Automatically

    merged   ..and an ApplicationContext is created   one per Bundle   Spring-DM manages the ApplicationContext lifecycle   OSGi manages the Bundle lifecycle 18 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  18. Spring-DM: Service Registry <beans ...> <osgi:service ref="customerDAO" interface="dao.CustomerDAO" /> <osgi:reference

    id="dataSource" interface="javax.sql.DataSource" /> </beans> Also supports filters, listeners, collections (lists and sets), etc. Contrast with programmatic API 19 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  19.   Dynamic services handled automatically   Instances and collections are

    proxied   Method calls are buffered   Configurable timeouts   Purely declarative   Application code is typically decoupled from OSGi   Spring’s POJO programming model   Testable, out-of-container Spring-DM: Service Registry 20 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  20. Out-of-scope for Spring-DM   Easy import of bundles and libraries

      Using JPA or Hibernate in OSGi   Seamless Web support   Notion of an application   Enter:   OSGi Web Container   SpringSource dm Server 21 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  21. OSGi Web Container 22 Copyright 2008-2009 Sam Brannen and SpringSource.

    Copying, publishing or distributing without express written permission is prohibited.
  22. Web Bundle   RFC 66: OSGi Web Container specification  

    Reference implementation by SpringSource   Web Bundle = web application in OSGi   Multiple supported formats   Standard WAR   Shared Libraries WAR   Shared Services WAR   Context path defined via query parameter or in manifest:   Web-ContextPath: /my-web-app 23 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  23. OSGi Web Migration Path  Status quo: everything in a monolithic

    WAR  Goals:   Libraries: eradicate library bloat   Services: OSGi Service Registry  Results: decrease overall footprint 24 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  24.  Supported as is  The WAR file is transformed into an

    OSGi bundle and installed into the Servlet Container  All standard WAR contracts are honored  Easy on-ramp for web applications in OSGi Standard Java EE WAR 25 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  25.  Eradicate the library bloat of monolithic Java EE WARs  Declare

    dependencies via OSGi manifest headers •  Import-Package & Require-Bundle •  Import-Library & Import-Bundle ˠ Import-Package  Reduce application deployment footprint  SpringSource Enterprise Bundle Repository: central source for OSGi bundles Shared Libraries WAR 26 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  26. DEMO Shared Libraries WAR 27 Copyright 2008-2009 Sam Brannen and

    SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  27.  Share services between bundles and web applications  Spring-DM simplifies usage

    of the OSGi Service Registry   Publish services via <osgi:service … />   Consume them via <osgi:reference … /> • ServerOsgiBundleXmlWebApplicationContext: use as contextClass property for ContextLoaderListener and DispatcherServlet  Program to interfaces and decouple web artifacts from the domain model, services, infrastructure, etc. Shared Services WAR 28 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  28. DEMO Shared Services WAR 29 Copyright 2008-2009 Sam Brannen and

    SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  29. SpringSource dm Server™ 30 Copyright 2008-2009 Sam Brannen and SpringSource.

    Copying, publishing or distributing without express written permission is prohibited.
  30. dm Server Architecture  Built on Equinox  Modular architecture  Subsystems  Bundles

     Small footprint  Modular profiles  Bundle repository  Library provisioning  Serviceability  FFDC  Logging  Tracing 31 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  31. Import: Library & Bundle   Plain OSGi + …  

    Import-Library and Import-Bundle   Library imports  bundle imports   Bundle imports  package imports   OSGi runtime semantics remain the same Import-Library: org.springframework.spring; version="[2.5.6,3.0)" Import-Bundle: com.springsource.org.hibernate; version="[3.2.6.ga,3.2.6.ga]" 32 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  32. PAR   Packaging format for all modules in an app.

      JAR with Application-* manifest headers   Single unit: deploy, refresh, undeploy   Application boundaries   Scoping of types / services   DataSource does not leak out of the application   Hibernate can change domain objects 33 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  33. DEMO PAR 34 Copyright 2008-2009 Sam Brannen and SpringSource. Copying,

    publishing or distributing without express written permission is prohibited.
  34. Plan   Conceptually similar to the PAR format:   Defines

    all modules within an application   Single configuration for deployment, etc.   Application boundaries   Scoping of types / services   Differences:   Plan = XML file with „.plan“ extension   Application modules are referenced within XML   Modules are installed stand-alone in the repository   Control deployment order of modules   Share modules between plans   Configure scoping and atomicity 35 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  35. Slices   Slice = mechanism for extending a host web

    bundle   Goes beyond mere code   Slices can provide code and content to a host   Slices can use content and services from a host   Analogous to OSGi fragments, slices attach to a host web bundle:   Slice-Host: hello.war.host;version="[1.0,2.0)”   Slice-ContextPath: /de   Host interacts with slices via slices tag library   Slices provide configuration properties to host 36 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  36. DEMO Slices 37 Copyright 2008-2009 Sam Brannen and SpringSource. Copying,

    publishing or distributing without express written permission is prohibited.
  37. Closing 38 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing

    or distributing without express written permission is prohibited.
  38. Further Resources   OSGi: http://osgi.org   Spring Framework: http://springframework.org  

    Spring-DM: http://springframework.org/osgi   SpringSource dm Server: http://springframework.org/dmserver   SpringSource Team Blog: http://blog.springsource.com   German Getting Started: http://www.dpunkt.de/buecher/3231.html 39 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  39. Q&A Sam Brannen sam [at] sambrannen [dot] com http://SamBrannen.com http://twitter.com/sam_brannen

    40 Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.