Enterprise Applications With OSGi and SpringSource dm Server

46
Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. Enterprise Applications with OSGi and SpringSource dm Server Eberhard Wolff – SpringSource Sam Brannen – OpenCredo Jazoon – 24 June 2009 – Zürich, Switzerland

description

This talk covers the benefits of OSGi in Enterprise Java as well as the concrete challenges and solutions to them. The focus is on SpringSource dm Server which represents a complete OSGi environment and solves many of the issues in this area. Throughout the talk demos and code samples will demonstrate the practical application of the discussed principles. This includes the stepwise migration of a (legacy) WAR based web application to a fully modularized OSGi-based web application.

Transcript of Enterprise Applications With OSGi and SpringSource dm Server

Page 1: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Enterprise Applications with OSGi and SpringSource dm Server

Eberhard Wolff – SpringSourceSam Brannen – OpenCredo

Jazoon – 24 June 2009 – Zürich, Switzerland

Page 2: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 2

Eberhard.Wolff (@) SpringSource.com

• Regional Director of German speaking countries and Principal Consultant

• Author of several articles and books• First German book on Spring• Speaker at national and international

conferences• Blog: http://jandiandme.blogspot.com/

Page 3: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 3

SpringSource

• Employs most committers for Spring• Committers for Tomcat, ActiveMQ, Apache

HTTP• New: tc Server / dm Server• Groovy / Grails• Hyperic

• Training• Consulting• Subscription / Support

Page 4: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 4

Sam.Brannen (@) OpenCredo.com

• 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, and testing• Co-author of Spring in a Nutshell; chief

technical reviewer for Spring Recipes

Page 5: Enterprise Applications With OSGi and SpringSource dm Server

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

Open Credo

• Experts in Open Source application development

• Consulting• Coaching• Management Training• Committers for numerous Spring projects

– Spring Framework– Spring Integration– Spring .NET

Page 6: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Why another Application Server?

Page 7: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 7

OSGi’s Promise of Modularity

• Modularization is key to maintainable software

• Application Server itself is modularized– No more "one size fits all"– Java EE 6 introduces profiles

• On the client and in the embedded world OSGi has succeeded as a standard for modularization

• OSGi enters the enterprise server market…

Page 8: Enterprise Applications With OSGi and SpringSource dm Server

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

Building on OSGi

• Almost all app server vendors base their systems on OSGi (JBoss, Sun, Oracle, IBM)

• But none offers OSGi as a programming model for the customer

• Why shouldn't the customer be as empowered as the app server vendor?

• Enter SpringSource dm Server…

Page 9: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

OSGi

Page 10: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 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

• better operations

• Strict visibility rules• Resolution process satisfies dependencies of a

module• Understands versioning

Page 11: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 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

Page 12: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 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– Bundle-Vendor

Page 13: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 13

Export & Import Packages

Declare package-level visibility and dependencies of your bundle.

Export-Package: com.xyz.barExport-Package: com.xyz.bar;version="1.0.5"

Import-Package: com.xyz.fooImport-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.

Page 14: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 14

Publish a Service

ServiceRegistration reg = bundleContext.registerService(Bar.class.getName(),

myBarService);...reg.unregister();

Page 15: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 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!

Page 16: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

OSGi in the Enterprise

Page 17: Enterprise Applications With OSGi and SpringSource dm Server

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

OSGi as a Server Platform

OSGi Service Platform

Enterprise Server Bundles

TransactionManagement

OSGiApplication

Web Container

...

Web Application

Features might be added on demand

Page 18: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 18

OSGi for Enterprise Applications

• Dynamic services are hard to develop (boiler plate code)

• Basic infrastructure for OSGi + Web has to be done by yourself

• What do we do about WARs?• How do you keep a service or type from

leaking out of an application?– The notion of an application does not exist in

OSGi…

• Many enterprise libraries are not suited for OSGi

Page 19: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 19

Enterprise Libraries under OSGi

• Class and resource loading problems– class visibility– context class loader is undefined in OSGi– resources in META-INF

– …

• Not directly supported: libraries with multiple bundles

Page 20: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 20

Example: Class Visibility

Data LayerBundle

Data LayerBundle

Domain ModelBundle

<Export-Pkg>

Domain ModelBundle

<Export-Pkg>

HibernateBundle

<Export-Pkg>

HibernateBundle

<Export-Pkg>

Import-Package Import-Package

Domaintypes &mappingfiles

SessionFactory

Page 21: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 21

Good News for OSGi in the Enterprise

• Spring 2.5 and many other Spring projects are OSGi-ready– modules shipped as bundles– all class loading behaves correctly under OSGi

• Hundreds of other enterprise libraries are now also packaged for use under OSGi in the SpringSource Enterprise Bundle Repository

Page 22: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Spring Dynamic Modules &SpringSource dm Server

Page 23: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 23

Spring-DM: ApplicationContext

• 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

Page 24: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 24

Spring-DM: Service Registry (1/2)

<beans ...><osgi:service ref="customerDAO"interface="dao.ICustomerDAO" />

<osgi:reference id="dataSource"interface="javax.sql.DataSource" />

</beans>

Also supports filters, listeners, collections (lists and sets), etc.

Contrast with programmatic API

Page 25: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 25

Spring-DM: Service Registry (2/2)

• Dynamic services handled automatically– Instances and collections are proxied– Method calls are buffered– Configurable timeouts

• Purely declarative

Page 26: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 26

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 SpringSource dm Server…

Page 27: Enterprise Applications With OSGi and SpringSource dm Server

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

dm Server Platform• Built on Equinox• Modular architecture

–Subsystems–Bundles

• Small footprint• Modular profiles• Bundle repository• Library provisioning• Serviceability

–FFDC–Logging–Tracing

Page 28: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 28

Importing & Exporting Types

• 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]"

Page 29: Enterprise Applications With OSGi and SpringSource dm Server

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

Example Application Bundles

• Web• Service• API

– Only interfaces and domain classes

– Implementation can be exchanged

• Could add infrastructure: data source, transaction manager, etc.

WebExport

API

Service

Import

Page 30: Enterprise Applications With OSGi and SpringSource dm Server

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

Bundles & Types

• Only dependencies on the API– Implementation can be exchanged, …

even at runtime

• No direct dependencies on any implementation

• Not shown: dependencies on external bundles

• … can be installed in dm Server• … modular middleware!

Page 31: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 31

Bundles & Services

API

Service

WebService RegistryType:

Import

publish

consume

Services:Publish /Consume

Page 32: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 32

Bundles & Services

• Infrastructure can use the same principle as application services– DataSource and PlatformTransactionManager

are just services

Can I still run on plain Java EE?• Yes: instead of OSGi Service directly inject

Spring Beans• No more dynamic services / modularization• No code change needed• Application can run on Java EE or OSGi

Page 33: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 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

Page 34: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Web Migration:From WAR to PAR

Page 35: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 35

Web Application Deployment Options• Standard Java EE WAR

– supported as is– converted into an OSGi

bundle

• Shared Libraries WAR– WAR + OSGi package

imports– Eradicate library bloat of

monolithic Java EEWARs

• Shared Services WAR– Uses OSGi services with

Spring's <osgi:reference>

• Web Module

Page 36: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 36

Web Module

• Deployment & packaging option for OSGi-compliant web applications: stand-alone or within a PAR

• OSGi bundle: structure similar to Shared Services WAR + MODULE-INF for resources

• Reduced configuration for Spring MVC applications via Web manifest headers– Auto-configuration of Spring MVC'sDispatcherServlet

– Single WebApplicationContext and no Root WAC

• Additional configuration via web.xml fragments

Page 37: Enterprise Applications With OSGi and SpringSource dm Server

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

Web Manifest Headers

Manifest-Version: 1.0Bundle-ManifestVersion: 2...Module-Type: WebWeb-ContextPath: /my-web-appWeb-DispatcherServletUrlPatterns: *.do

Page 38: Enterprise Applications With OSGi and SpringSource dm Server

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

DEMO

Web Module

Page 39: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Roadmap

Page 40: Enterprise Applications With OSGi and SpringSource dm Server

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

dm Server 2.0 Roadmap• SpringSource dm Server 2.0: 3rd quarter 2009• Cloning bundles

– Solves problems around static variables and more

• Shared Repository– Share a single repository among several servers

• Plan Files– Define an application as a collection of bundles– Does not contain the bundles, more flexible

• Distributed and improved management– Operation on a group of servers – Like tc Server for Tomcat

• Modular Web Applications

Page 41: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 41

Support for Enterprise OSGi Standards

• RFC 66: Web Container for OSGi (RI based on dm Server)

• RFC 119: Distributed OSGi• RFC 124: Blueprint Service (RI based on

Spring-DM)• RFC 139: JMX interface for OSGi• RFC 142: JNDI and OSGi integration

Page 42: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Summary

Page 43: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 43

Summary: SpringSource dm Server

• Based on proven, established modularization technology (OSGi)

• Based on proven, established Web technology (Tomcat)

• Spring and Spring-DM programming models: easy to use

• Solves OSGi problems (e.g., context class loading)

• Dynamic updates of running modules• Lightweight

Page 44: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 44

Summary: Deployment Options

• OSGi bundles• PAR = logical & physical application

boundary• dm Server supports multiple Web

deployment formats and therefore migration– Java EE WAR → Shared Libraries WAR →

Shared Services WAR → Web Module

Page 45: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited. 45

• 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

• OpenCredo:http://opencredo.com

Resources

Page 46: Enterprise Applications With OSGi and SpringSource dm Server

Copyright 2008-2009 SpringSource and Open Credo. Copying, publishing or distributing without express written permission is prohibited.

Questions?

Eberhard [email protected]

http://SpringSource.com

Sam [email protected]

http://OpenCredo.com