+ All Categories
Home > Technology > Modular Web Applications with OSGi

Modular Web Applications with OSGi

Date post: 10-May-2015
Category:
Upload: sam-brannen
View: 4,610 times
Download: 0 times
Share this document with a friend
Description:
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.
Popular Tags:
40
Modular Web Applications with OSGi Sam Brannen Software Consultant eJUG Seminar Linz, Austria 17 September 2009
Transcript
Page 1: Modular Web Applications with OSGi

Modular Web Applications with OSGi

Sam Brannen Software Consultant

eJUG Seminar Linz, Austria 17 September 2009

Page 2: Modular Web Applications with OSGi

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.

Page 3: Modular Web Applications with OSGi

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.

Page 4: Modular Web Applications with OSGi

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.

Page 5: Modular Web Applications with OSGi

Status Quo

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

Page 6: Modular Web Applications with OSGi

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.

Page 7: Modular Web Applications with OSGi

 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.

Page 8: Modular Web Applications with OSGi

DEMO

Standard WAR

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

Page 9: Modular Web Applications with OSGi

OSGi

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

Page 10: Modular Web Applications with OSGi

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.

Page 11: Modular Web Applications with OSGi

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.

Page 12: Modular Web Applications with OSGi

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.

Page 13: Modular Web Applications with OSGi

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.

Page 14: Modular Web Applications with OSGi

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.

Page 15: Modular Web Applications with OSGi

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.

Page 16: Modular Web Applications with OSGi

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.

Page 17: Modular Web Applications with OSGi

Spring DM

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

Page 18: Modular Web Applications with OSGi

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.

Page 19: Modular Web Applications with OSGi

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.

Page 20: Modular Web Applications with OSGi

  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.

Page 21: Modular Web Applications with OSGi

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.

Page 22: Modular Web Applications with OSGi

OSGi Web Container

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

Page 23: Modular Web Applications with OSGi

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.

Page 24: Modular Web Applications with OSGi

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.

Page 25: Modular Web Applications with OSGi

 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.

Page 26: Modular Web Applications with OSGi

 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.

Page 27: Modular Web Applications with OSGi

DEMO

Shared Libraries WAR

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

Page 28: Modular Web Applications with OSGi

 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.

Page 29: Modular Web Applications with OSGi

DEMO

Shared Services WAR

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

Page 30: Modular Web Applications with OSGi

SpringSource dm Server™

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

Page 31: Modular Web Applications with OSGi

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.

Page 32: Modular Web Applications with OSGi

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.

Page 33: Modular Web Applications with OSGi

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.

Page 34: Modular Web Applications with OSGi

DEMO

PAR

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

Page 35: Modular Web Applications with OSGi

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.

Page 36: Modular Web Applications with OSGi

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.

Page 37: Modular Web Applications with OSGi

DEMO

Slices

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

Page 38: Modular Web Applications with OSGi

Closing

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

Page 39: Modular Web Applications with OSGi

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.

Page 40: Modular Web Applications with OSGi

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.


Recommended