This presentation covers how to develop modular Java web applications with OSGi using the new OSGi Web Container (RFC 66), Spring, Spring DM, and the SpringSource dm Server. Also covers special features of dm Server including PARs, Plan files, and Web Slices.
1. Modular Web Applications
with OSGi
eJUG Seminar Sam Brannen
Linz, Austria Software Consultant
17 September 2009
2. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
2
3. Overview
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™.
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
3
4. Agenda
Status Quo
OSGi
Spring DM
OSGi Web Container
SpringSource dm Server™
Demos
Q&A
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
4
5. Status Quo
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
5
6. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
6
7. Introduction to Demos
Spring MVC 2.5 based "Hello World" web application
Domain model: HelloMessage
Service layer: HelloService, HelloServiceImpl
Presentation layer:
Controllers: HelloController
Views: JSP + JSTL
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
7
8. DEMO
Standard WAR
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
8
9. OSGi
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
9
10. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
10
11. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
11
12. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
12
13. 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
>= 1.0.3; e.g.,
Import-Package: 1.0.3, 1.0.3.GA,
com.xyz.foo;version="1.0.3" 1.0.4, 2.0, etc.
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)"
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
13
14. Publish a Service
ServiceRegistration reg =
bundleContext.registerService(
Bar.class.getName(), myBarService);
...
reg.unregister();
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
14
15. 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!
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
15
16. Implementations & Tools
OSGi implementations:
Equinox
Knopflerfish
Felix
IDEs
Eclipse PDE
SpringSource Tool Suite
Build support:
BND
Bundlor
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
16
17. Spring DM
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
17
18. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
18
19. Spring-DM: Service Registry
Contrast with programmatic
<beans ...> API
<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.
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
19
20. Spring-DM: Service Registry
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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
20
21. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
21
22. OSGi Web Container
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
22
23. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
23
24. OSGi Web Migration Path
Status quo: everything in a
monolithic WAR
Goals:
Libraries: eradicate library bloat
Services: OSGi Service Registry
Results: decrease overall
footprint
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
24
25. Standard Java EE WAR
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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
25
26. Shared Libraries WAR
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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
26
27. DEMO
Shared Libraries WAR
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
27
28. Shared Services WAR
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.
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
28
29. DEMO
Shared Services WAR
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
29
30. SpringSource dm Server™
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
30
31. dm Server Architecture
Built on Equinox
Modular architecture
Subsystems
Bundles
Small footprint
Modular profiles
Bundle repository
Library provisioning
Serviceability
FFDC
Logging
Tracing
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
31
32. 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]"
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
32
33. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
33
34. DEMO
PAR
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
34
35. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
35
36. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
36
37. DEMO
Slices
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
37
38. Closing
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
38
39. 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
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
39
40. Q&A
Sam Brannen
sam [at] sambrannen [dot] com
http://SamBrannen.com
http://twitter.com/sam_brannen
Copyright 2008-2009 Sam Brannen and SpringSource. Copying, publishing or distributing without express written permission is prohibited.
40