Modularity with OSGi

How to modularize your enterprise applications using OSGi

Copyright 2005-2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Topics in this Session
• • • • Introduction OSGi basics OSGi & Spring Enterprise OSGi

2

Introduction
• OSGi: a dynamic module system for Java • Modular:
– Bundles: JAR files with meta-data – Strict visibility for types, based on packages – Versioning of both packages and bundles

• Dynamic:
– Add, remove, start, stop bundles at runtime – Use shared services to share objects, not just types

3

History of OSGi
• Open Specification managed by OSGi Alliance
– Founded un March 1999

• Based on the realized need for light weight dynamic platform
– Initially targeted to network and embedded devices – 2006: server side adoption

• Member companies
– IBM, SpringSource, Motorola, Oracle....

4

Topics in this Session • • • • Introduction OSGi basics OSGi & Spring Enterprise OSGi 5 .

.MF • Meta-data defined in JAR Manifest Manifest-Version: 1.0 Bundle-Version: 1.0.importer .Bundle MANIFEST. • This bundle is a black-box – Its types are invisible to other bundles – It can't see any types outside itself 6 .springdm.0 Bundle-Name: Plain OSGi Importer Bundle-ManifestVersion: 2 Bundle-SymbolicName: osgi..

other.springdm. export their packages Export-Package: osgi.Exporting types • To make types available to other bundles.0.version=1.0.exporter • Can add version info Export-Package: osgi.0.package.exporter.springdm.2 7 .version=3.

Exporting Best Practises • Separate interfaces from implementations – Use different packages – Only export public API. hide internals – Expose implementations through its service interface • Version your packages – Allows multiple versions in the same container – Clients can ask for a particular version 8 .

exporter • Can specify requested version(s) Import-Package: osgi.Importing types • To access types from other bundles.0.springdm.2.version=”[1. () for exclusion 9 .0.0.exporter.0)” – [] for inclusion. import their packages Bundle-Name: Plain OSGi Importer Import-Package: osgi.springdm.

OSGi Visibility • Adds encapsulation and versioning to your modules – Preserves modularity at runtime • Not restricted to a single hierarchical classpath – Each bundle has its own ClassLoader • All managed by the OSGi container 10 .

OSGi Containers • Provide the runtime environment for OSGi bundles • Small core with additional services • OSS implementations – Equinox (eclipse) – Apache Felix – Eclipse Virgo RT (formerly Spring dm-server) 11 .

all dependencies present) Starting Started (services available) Stoping Uninstalled (will remove after restart or refresh) 12 . but missing dependencies) Resolved (stopped.OSGi Dynamics • Bundles can be installed into container • Have a managed lifecycle – – – – – – Installed (present.

plain.ImporterActivator 13 .importer.OSGi Lifecycle • Bundles can receive callbacks when started or stopped public class ImporterActivator implements BundleActivator { public void start(BundleContext context) throws Exception { } public void stop(BundleContext arg0) throws Exception { } } • Register in Manifest Bundler-Activator: osgi.

BundleContext • API of OSGi runtime • Work with bundles and services – (un)install. start/stop bundles – Register services and obtain references • Register listeners to be informed of events 14 .

start/stop bundles – Register services and obtain references • Register listeners to be informed of events 15 .BundleContext • API of OSGi runtime • Work with bundles and services – (un)install.

.DEMO Using plain OSGi to share services between bundles Copyright 2005-2008 SpringSource. publishing or distributing without express written permission is prohibited. Copying.

Topics in this Session • • • • Introduction OSGi basics OSGi & Spring Enterprise OSGi 17 .

2: Blueprint service specification – Based on Spring Dynamic Modules project – Contributed to Eclipse Foundation as Gemini Blueprint (late 2009) – Use familiar Spring programming model in OSGi 18 .OSGi & Spring • OSGi sets the runtime • Spring adds the component model • OSGi 4.

Spring Dynamic Modules (spring DM for short) • Removes OSGi dependencies from the code • Creates application context per bundle • Declarative service management – Both for export & import 19 .

Spring DM Service management • Exporter bundle: – Exposes Spring beans as OSGi services • Importer bundle: – Creates proxies for service references – No more manual management of service dynamics! – removes plumbing code (as usual) 20 .

springdm.Spring DM configuration • Spring config files placed in META-INF/spring – Detected and picked automatically • osgi namespace for service management <bean id="multiplierService" class="osgi.exporter.exporter.springdm.exporter.internal.springdm.MultiplierService"/> 21 .MultiplierService"/> <osgi:reference id="multiplierService" interface="osgi.SimpleMultiplierService"> <constructor-arg value="2"/> </bean> <osgi:service ref="multiplierService" interface="osgi.

Spring DM configuration • Separate normal Spring config files from Spring-DM config files – Test or reuse modules without OSGi <bean id="myService" class="samples."> <constructor-arg ref="myService"/> </bean> <osgi:service ref="myService" interface="samples.internal.ServiceImpl"/> <bean id="serviceClient" class="...Service"/> 22 .Service"/> <osgi:reference id="myService" interface="sample.

publishing or distributing without express written permission is prohibited.DEMO Using Spring Dynamic Modules to share types and services Copyright 2005-2008 SpringSource. Copying. .

publishing or distributing without express written permission is prohibited. Copying.DEMO Developing a multi-bundle web application Copyright 2005-2008 SpringSource. .

1 . publishing or distributing without express written permission is prohibited.Modularity with OSGi How to modularize your enterprise applications using OSGi Copyright 2005-2008 SpringSource. Copying.

Reiterate that it provides consistent. and obscures the intent. the JdbcTemplate is introduced to demonstrate how it simplifies data access and enforces consistency at the same time. generic queries (Map and List of Maps). Next.Topics in this Session • • • • Introduction OSGi basics OSGi & Spring Enterprise OSGi 2 Description of theory presented in “Introduction to Spring JDBC” This module introduces the JdbcTemplate. SQLExceptions are typically fatal. Recommended presentation time: -1 hour (not including lab) What will students have learnt after this module? -How to use JdbcTemplate for queries and updates 2 . It begins by showing an example of raw JDBC API usage. meaningful exceptions that the developer may choose to catch but normally will propagate (NOTE: customization of Spring’s exception translation need not be discussed here). Then. Second. duplication of generic code is redundant. it reviews the problems with that approach. the SQLException represents a leak in the JDBC abstraction. First. by forcing developers to deal with vendorspecific codes only. and finally an example of performing an update and insert. error prone. The rest of the module shows examples of the various “basic” query methods including simply types (int and String). object queries (using the RowMapper callback for a single item result and for a Collection). Spring’s exception hierarchy was already introduced in the “Introduction to Data Access with Spring” module. and therefore the developer should not be forced to catch them. In addition.

Introduction • OSGi: a dynamic module system for Java • Modular: – Bundles: JAR files with meta-data – Strict visibility for types. based on packages – Versioning of both packages and bundles • Dynamic: – Add. start. stop bundles at runtime – Use shared services to share objects. remove. not just types 3 3 .

History of OSGi • Open Specification managed by OSGi Alliance – Founded un March 1999 • Based on the realized need for light weight dynamic platform – Initially targeted to network and embedded devices – 2006: server side adoption • Member companies – IBM. 4 4 ... SpringSource. Oracle.. Motorola.

meaningful exceptions that the developer may choose to catch but normally will propagate (NOTE: customization of Spring’s exception translation need not be discussed here). Recommended presentation time: -1 hour (not including lab) What will students have learnt after this module? -How to use JdbcTemplate for queries and updates 5 . duplication of generic code is redundant. generic queries (Map and List of Maps). by forcing developers to deal with vendorspecific codes only. The rest of the module shows examples of the various “basic” query methods including simply types (int and String). object queries (using the RowMapper callback for a single item result and for a Collection). Reiterate that it provides consistent. Then. Spring’s exception hierarchy was already introduced in the “Introduction to Data Access with Spring” module. error prone. the JdbcTemplate is introduced to demonstrate how it simplifies data access and enforces consistency at the same time. Second. In addition. and therefore the developer should not be forced to catch them. it reviews the problems with that approach. First. It begins by showing an example of raw JDBC API usage. and obscures the intent. Next. the SQLException represents a leak in the JDBC abstraction. SQLExceptions are typically fatal.Topics in this Session • • • • Introduction OSGi basics OSGi & Spring Enterprise OSGi 5 Description of theory presented in “Introduction to Spring JDBC” This module introduces the JdbcTemplate. and finally an example of performing an update and insert.

Bundle MANIFEST.springdm..0 Bundle-Version: 1.MF • Meta-data defined in JAR Manifest Manifest-Version: 1.importer . • This bundle is a black-box – Its types are invisible to other bundles – It can't see any types outside itself 6 6 ..0 Bundle-Name: Plain OSGi Importer Bundle-ManifestVersion: 2 Bundle-SymbolicName: osgi.0.

version =3.Exporting types • To make types available to other bundles.2 7 7 .springdm. export their packages Export-Package: osgi.0.exporter • Can add version info Export-Package: osgi.0. other.springdm.package.exporter.0. version =1.

Exporting Best Practises • Separate interfaces from implementations – Use different packages – Only export public API. hide internals – Expose implementations through its service interface • Version your packages – Allows multiple versions in the same container – Clients can ask for a particular version 8 8 .

0)” – [] for inclusion.exporter. version =”[1.springdm.exporter • Can specify requested version(s) Import-Package: osgi.2.0.Importing types • To access types from other bundles. import their packages Bundle-Name: Plain OSGi Importer Import-Package: osgi. () for exclusion 9 9 .springdm.0.0.

OSGi Visibility • Adds encapsulation and versioning to your modules – Preserves modularity at runtime • Not restricted to a single hierarchical classpath – Each bundle has its own ClassLoader • All managed by the OSGi container 10 10 .

OSGi Containers • Provide the runtime environment for OSGi bundles • Small core with additional services • OSS implementations – Equinox (eclipse) – Apache Felix – Eclipse Virgo RT (formerly Spring dm-server) 11 11 .

all dependencies present) Starting Started (services available) Stoping Uninstalled (will remove after restart or refresh) 12 12 . but missing dependencies) Resolved (stopped.OSGi Dynamics • Bundles can be installed into container • Have a managed lifecycle – – – – – – Installed (present.

importer.plain.ImporterActivator 13 13 .OSGi Lifecycle • Bundles can receive callbacks when started or stopped public class ImporterActivator implements BundleActivator { public void start(BundleContext context) throws Exception { } public void stop(BundleContext arg0) throws Exception { } } • Register in Manifest Bundler-Activator: osgi.

start/stop bundles – Register services and obtain references • Register listeners to be informed of events 14 14 .BundleContext • API of OSGi runtime • Work with bundles and services – (un)install.

start/stop bundles – Register services and obtain references • Register listeners to be informed of events 15 15 .BundleContext • API of OSGi runtime • Work with bundles and services – (un)install.

DEMO Using plain OSGi to share services between bundles Copyright 2005-2008 SpringSource. 16 . publishing or distributing without express written permission is prohibited. Copying.

generic queries (Map and List of Maps). object queries (using the RowMapper callback for a single item result and for a Collection). and finally an example of performing an update and insert. Next. meaningful exceptions that the developer may choose to catch but normally will propagate (NOTE: customization of Spring’s exception translation need not be discussed here). Second. it reviews the problems with that approach. the JdbcTemplate is introduced to demonstrate how it simplifies data access and enforces consistency at the same time. the SQLException represents a leak in the JDBC abstraction. It begins by showing an example of raw JDBC API usage. Reiterate that it provides consistent.Topics in this Session • • • • Introduction OSGi basics OSGi & Spring Enterprise OSGi 17 Description of theory presented in “Introduction to Spring JDBC” This module introduces the JdbcTemplate. error prone. Then. and obscures the intent. by forcing developers to deal with vendorspecific codes only. SQLExceptions are typically fatal. The rest of the module shows examples of the various “basic” query methods including simply types (int and String). Spring’s exception hierarchy was already introduced in the “Introduction to Data Access with Spring” module. First. Recommended presentation time: -1 hour (not including lab) What will students have learnt after this module? -How to use JdbcTemplate for queries and updates 17 . and therefore the developer should not be forced to catch them. duplication of generic code is redundant. In addition.

2: Blueprint service specification – Based on Spring Dynamic Modules project – Contributed to Eclipse Foundation as Gemini Blueprint (late 2009) – Use familiar Spring programming model in OSGi 18 18 .OSGi & Spring • OSGi sets the runtime • Spring adds the component model • OSGi 4.

Spring Dynamic Modules (spring DM for short) • Removes OSGi dependencies from the code • Creates application context per bundle • Declarative service management – Both for export & import 19 19 .

Spring DM Service management • Exporter bundle: – Exposes Spring beans as OSGi services • Importer bundle: – Creates proxies for service references – No more manual management of service dynamics! – removes plumbing code (as usual) 20 20 .

springdm.SimpleMultiplierService" > <constructor-arg value="2"/> </bean> <osgi:service ref="multiplierService" interface="osgi.exporter.exporter.Spring DM configuration • Spring config files placed in META-INF/spring – Detected and picked automatically • osgi namespace for service management <bean id="multiplierService" class="osgi.MultiplierService"/> 21 21 .springdm.exporter.MultiplierService"/> <osgi:reference id="multiplierService" interface="osgi.springdm.internal.

Service"/> <osgi:reference id="myService" interface="sample."> <constructor-arg ref="myService"/> </bean> <osgi:service ref="myService" interface="samples.Spring DM configuration • Separate normal Spring config files from Spring-DM config files – Test or reuse modules without OSGi <bean id="myService" class="samples.ServiceImpl"/> <bean id="serviceClient" class=".internal..Service"/> 22 22 ..

23 . publishing or distributing without express written permission is prohibited.DEMO Using Spring Dynamic Modules to share types and services Copyright 2005-2008 SpringSource. Copying.

Copying.DEMO Developing a multi-bundle web application Copyright 2005-2008 SpringSource. 24 . publishing or distributing without express written permission is prohibited.

Sign up to vote on this title
UsefulNot useful