Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | ccustine |
View: | 4,428 times |
Download: | 5 times |
Introduction - Chris Custine
Apache ServiceMix - Committer
Open Source ESB
JBI 1.0 Implementation
ServiceMix 4 - OSGi Based (Apache Felix)
Apache Directory Server - Committer and PMC Member
LDAP v3 Certified
100% Java
Soon to be OSGi, but currently used in some OSGi services
Organic Element - Open Source Consulting and Software Development
2
Presentation Agenda
OSGi Overview and History
OSGi Architecture and Layers
Best Practices
Recommended Tools
3
What is OSGi?
OSGi - Formerly Open Services Gateway Initiative
Now just OSGi due to generalization of market
OSGi Alliance - Consortium of Vendors
4
What Does the OSGi Alliance Do?
Defines OSGi Service Platform specification
Multiple groups define the specifications
Core Platform Expert Group (CPEG) – Core framework
Mobile Expert Group (MEG) – Mobile
Vehicle Expert Group (VEG) – Automotive
Enterprise Expert Group (EEG) – Enterprise
Residential Expert Group (REG) – Home Automation
5
Where did OSGi come from?
Originally aimed at Home Automation market
Home Gateway device to monitor and interact with devices
Multi vendor devices connected throughout the home
6
Where did OSGi come from?
Requirement for standard plugin model for Gateway deviceGUI Model for administration of devicesServices available to devices
Data storage
Conversion
Network
Software management and deploymentSoftware updates
SecurityServices only available to same device/vendorOnly allow updates from properly signed jar
“Hello, this is your refrigerator... you need to buy milk”
7
How we got here
Original use case (Home Automation) faltered around 2000
OSGi used in niche areas (automotive, mobile) for several years
Eclipse 3.0 released June 2004 - First Eclipse with OSGi
Gradual adoption and recognition for OSGi after Eclipse
Currently undergoing widespread adoption
8
OSGi is...
About building systems from smaller components
Components working together
Managing components
9
Why OSGi?
Convergence of mobile and embedded devices creates huge manageability problems
Portability - Hardware, OS
Management - Massive pushes for updates and fixes
Maintenance - Dynamic library management and dependencies causing unmanageable hacks like Class.forName()
10
OSGi Relationship with Java
Always focused on Java
Bundles may contain native libraries used via JNI
Example: Eclipse uses native libraries for SWT GUI calls
RFC-119 for distributed OSGi allows communication with external systems (possibly not Java)
Script language support from bundle providers
11
OSGi Service Platform
Two distinct specifications
Core Specification - Core framework, packaging, modularity, security, API (interfaces), etc.
Service Compendium - Additional services (some optional), utilities, logging, event system, configuration admin, etc.
12
OSGi Architecture and Layers
Execution Environment
Module
Lifecycle
Service
Se
cu
rity
Hardware / OS
Bundles
13
Java Execution Environment
Managing the runtime environment
What do I require to run?
JRE Level
GUI Requirements
Matching application requirements with the platform
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
14
Module Layer
Packaging and deployment standards
Specify constraints on dependencies and requirements
Visibility between components
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
15
Lifecycle Layer
Fine grained events for application and component lifecycle
Installing, starting, stopping
Standard API for registering, listening, and acting on events
No custom API needed for plugins and extensions
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
16
Services Layer
Standard methods for registering service instances
Discover and bind to services from other components
Reference tracking
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
17
Security Layer
Protect Visibility between components
Example: Vendor A only wants to use the XYZ service provided by Vendor A (could be multiple other implementations of XYZ)
Constraints can be expressed using certificates from signed jars or attributes for bundle, service, etc.
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
18
Layer Details
Module, Lifecycle, and Service layers are where most work is done for enterprise Java development
Security is important but not as widely utilized
19
Java Modularity Limitations
Scoping and visibility is limited to packages (no module scoping)
Dependency management? Managed through classpath
Version handling? First package match on classpath
Modularity is not supported as a first class citizen
20
Module Layer
Bundles are the primary building block
Package visibility and sharing
Security constraints on packages
Classloader per bundle
Graph of classloaders, resolved at deploy time
Execution Environment
Module
Lifecycle
Service
Se
cu
rity
Hardware / OS
Bundles
21
Module Layer
Bundle - Just a jar file
META-INF/manifest.mf
Dependencies
Versioning
Consistency
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
� � � � � � � � � � � � � � � � �
� �� � � � � � � � � � � � � � �Manifest-Version: 1.0
Import-Package: ...
Export-Package: ...
Bundle-Activator: ...
Bundle-Version: 1.1.0.SNAPSHOT
Bundle-Name: ...
Bundle-ManifestVersion: 2
Bundle-SymbolicName: ...
22
Module Layer - Example Manifest
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitorBundle-Version: 1.1.0.SNAPSHOTImport-Package: org.apache.commons.logging,org.apache.servicemix.kerne l.filemonitor,org.osgi.framework,org.osgi.service.cm;version="1.2",or g.osgi.service.packageadmin,org.osgi.util.tracker;version="1.3"Export-Package: org.apache.servicemix.kernel.filemonitor;uses:="org.os gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi. service.packageadmin,org.osgi.util.tracker"Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivatorBundle-Name: Apache ServiceMix Kernel :: File MonitorBundle-Description: This bundle monitors the deploy directory for new OSGi bundles in jar form, expanded form or for configuration f ile changes and undeploys/redeploys them
Text
META-INF/manifest.mf
23
Module Layer - Example Manifest
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitorBundle-Version: 1.1.0.SNAPSHOTImport-Package: org.apache.commons.logging,org.apache.servicemix.kerne l.filemonitor,org.osgi.framework,org.osgi.service.cm;version="1.2",or g.osgi.service.packageadmin,org.osgi.util.tracker;version="1.3"Export-Package: org.apache.servicemix.kernel.filemonitor;uses:="org.os gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi. service.packageadmin,org.osgi.util.tracker"Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivatorBundle-Name: Apache ServiceMix Kernel :: File MonitorBundle-Description: This bundle monitors the deploy directory for new OSGi bundles in jar form, expanded form or for configuration f ile changes and undeploys/redeploys them
Text
META-INF/manifest.mf
24
Module Layer - Example Manifest
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitorBundle-Version: 1.1.0.SNAPSHOTImport-Package: org.apache.commons.logging,org.apache.servicemix.kerne l.filemonitor,org.osgi.framework,org.osgi.service.cm;version="1.2",or g.osgi.service.packageadmin,org.osgi.util.tracker;version="1.3"Export-Package: org.apache.servicemix.kernel.filemonitor;uses:="org.os gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi. service.packageadmin,org.osgi.util.tracker"Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivatorBundle-Name: Apache ServiceMix Kernel :: File MonitorBundle-Description: This bundle monitors the deploy directory for new OSGi bundles in jar form, expanded form or for configuration f ile changes and undeploys/redeploys them
Text
META-INF/manifest.mf
25
Module Layer - Example Manifest
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitorBundle-Version: 1.1.0.SNAPSHOTImport-Package: org.apache.commons.logging,org.apache.servicemix.kerne l.filemonitor,org.osgi.framework,org.osgi.service.cm;version="1.2",or g.osgi.service.packageadmin,org.osgi.util.tracker;version="1.3"Export-Package: org.apache.servicemix.kernel.filemonitor;uses:="org.os gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi. service.packageadmin,org.osgi.util.tracker"Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivatorBundle-Name: Apache ServiceMix Kernel :: File MonitorBundle-Description: This bundle monitors the deploy directory for new OSGi bundles in jar form, expanded form or for configuration f ile changes and undeploys/redeploys them
Text
META-INF/manifest.mf
26
Module Layer - Example Manifest
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitorBundle-Version: 1.1.0.SNAPSHOTImport-Package: org.apache.commons.logging,org.apache.servicemix.kerne l.filemonitor,org.osgi.framework,org.osgi.service.cm;version="1.2",or g.osgi.service.packageadmin,org.osgi.util.tracker;version="1.3"Export-Package: org.apache.servicemix.kernel.filemonitor;uses:="org.os gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi. service.packageadmin,org.osgi.util.tracker"Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivatorBundle-Name: Apache ServiceMix Kernel :: File MonitorBundle-Description: This bundle monitors the deploy directory for new OSGi bundles in jar form, expanded form or for configuration f ile changes and undeploys/redeploys them
Text
META-INF/manifest.mf
27
Module Layer - Example Manifest
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.apache.servicemix.kernel.filemonitorBundle-Version: 1.1.0.SNAPSHOTImport-Package: org.apache.commons.logging,org.apache.servicemix.kerne l.filemonitor,org.osgi.framework,org.osgi.service.cm;version="1.2",or g.osgi.service.packageadmin,org.osgi.util.tracker;version="1.3"Export-Package: org.apache.servicemix.kernel.filemonitor;uses:="org.os gi.framework,org.apache.commons.logging,org.osgi.service.cm,org.osgi. service.packageadmin,org.osgi.util.tracker"Bundle-Activator: org.apache.servicemix.kernel.filemonitor.FileMonitorActivatorBundle-Name: Apache ServiceMix Kernel :: File MonitorBundle-Description: This bundle monitors the deploy directory for new OSGi bundles in jar form, expanded form or for configuration f ile changes and undeploys/redeploys them
Text
META-INF/manifest.mf
28
Module Layer - Bundle Contents
Bundles may contain additional contents not usually included in jars.
Native code - Multiple platforms, OS, etc. loading rules specified in manifest
Embedded jar files - Bundle-Classpath allows embedded jars to be referenced from manifest.mf (not recommended)
Resource files - images, configuration, html
29
Module Layer - Bundle State
INSTALLED
UNINSTALLED RESOLVED
STARTING STOPPING
ACTIVE
uninstall resolve
update
uninstall
start
stop
install
30
Module Layer - Bundle State
INSTALLED
UNINSTALLED RESOLVED
STARTING STOPPING
ACTIVE
uninstall resolve
update
uninstall
start
stop
install
Resolution of dependencies at deployment
If resolution fails, bundle is not available for use
No runtime CNFE
Services and lifecycle not called until AFTER resolution
31
Module Layer - Resolution Goodies
Exporters can name arbitrary attributes for matching
Attributes can be mandatory for matching
org.apache.mypackageb-1.0.0.jar
META-INF/manifest.mf
Export-Package:
org.apache.mypackage;
version=”1.0.0”;flavor=”vanilla”
org.apache.bundlea-1.0.0.jar
META-INF/manifest.mf
Import-Package:
org.apache.mypackage;
version=”1.0.0”;flavor=”vanilla”
org.apache.mypackagea-1.0.0.jar
META-INF/manifest.mf
Export-Package:
org.apache.mypackage;
version=”1.0.0”
32
Module Layer - Resolution Goodies
Same package can be installed with different versions
Importers can specify version ranges for compatibility
Allows staged migration of multiple components
org.apache.mypackage-2.0.0.jar
META-INF/manifest.mf
Export-Package:
org.apache.mypackage;
version=”2.0.0”
org.apache.bundlea-1.0.0.jar
META-INF/manifest.mf
Import-Package:
org.apache.mypackage;
version=”[1.0.1,2.0.0)”
org.apache.mypackage-1.0.1.jar
META-INF/manifest.mf
Export-Package:
org.apache.mypackage;
version=”1.0.1”
33
Lifecycle Layer
Execution Environment
Module
Lifecycle
Service
Se
cu
rity
Hardware / OS
Bundles
Bundle-Activator: Like main() for your bundle.
Bundles can tap into the lifecycle events of the framework
Extremely powerful when combined with startlevel features
Ability to register listeners for bundle state changes
34
Lifecycle Layer
Execution Environment
Module
Lifecycle
Service
Security
Hardware / OS
Bundles
Bundle activation provides the bundle context to the bundle
Bundle context provides access to the framework and all services
Creates and registers service instances
Get reference to required services
Register listeners for bundle lifecycle BundleActivator.java
package org.osgi.framework; public interface BundleActivator { public void start(BundleContext context) throws Exception; public void stop(BundleContext context) throws Exception; }
35
Service Layer
Primary means of collaboration between components
Service Registry for discovering and binding to services
Ability to listen for services (Service Tracker)
Service oriented model for in VM interactions
RFC-119 Brings distributed services across instances
Execution Environment
Module
Lifecycle
Service
Se
cu
rity
Hardware / OS
Bundles
36
Start Level Service
Setting bundle start order
Similar to Unix init scripts
Start level can be changed up and down at runtime
Persistent start level on restarts
37
OSGi Compendium Services
Log Service
Http Service
Device Access
Configuration Admin
Metatype Service
Preferences Service
User Admin
Wire Admin
IO Connector
Initial Provisioning
UPnP
Declarative Services
Event Admin
Deployment Admin
Auto Configuration
Application Admin
DMT Admin
Monitor Admin
Service Tracker
38
Configuration Admin
Configuration provided by framework service
Notification to bundles when configuration changed
39
Best Practices
Use package names consistently - We use package name for artifact name (Eclipse plugins do too), with one package heirarchy per bundle. Quick recognition of artifact and package purpose.
Keep public and private classes in separate packages
Use explicit Import-Package. Don’t use Require-Bundle. Easy to use initially, but causes problems down the road.
Avoid Dynamic-Import - Defers classloading to runtime
Allows arbitrary Class.forName() use case but causes trouble
Use tools to compose manifest files and package bundles (Maven plugins, Eclipse PDE, etc.)
40
Tools for OSGi - BND
Peter Kriens - BND
Tool for analyzing dependencies and validating manifest.mf
Uses reflection to analyze imports and lists dependencies
Great tool for analyzing non-OSGi jar files and converting
41
Tools for OSGi - Maven Felix Plugin
Uses BND to automate creation of manifest.mf
Use packaging=bundle to invoke plugin in package lifecycle
pom.xml <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> <Export-Package>org.apache.servicemix.kernel.gshell.activemq;version=${project.version};-split-package:=merge-first</Export-Package> <Import-Package> org.apache.geronimo.gshell.wisdom.command, org.apache.servicemix.kernel.gshell.core, org.apache.geronimo.gshell.wisdom.registry, * </Import-Package> <Private-Package>!*</Private-Package> <Spring-Context>*;publish-context:=false;create-asynchronously:=false</Spring-Context> </instructions> </configuration> </plugin>
42
Tools for OSGi - Spring DM
SpringDM bridges the Spring world with OSGi Service Platform
Reuse existing knowledge of Spring
Limited knowledge of OSGi required
Expose spring beans in the OSGi Service Registry
Can be bound to by pure OSGi service consumers
Lookup and bind bean refs to OSGi services
43
Tools for OSGi - Spring DM
Example: ActiveMQ broker definition using SpringDM and OSGi
broker.xml <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactoryBean"> <property name="maxConnections" value="8" /> <property name="transactionManager" ref="transactionManager" /> <property name="connectionFactory" ref="activemqConnectionFactory" /> <property name="resourceName" value="activemq.${name}" /> </bean> <osgi:reference id="transactionManager" interface="javax.transaction.TransactionManager" cardinality="0..1"/> <osgi:service ref="pooledConnectionFactory"> <osgi:interfaces> <value>javax.jms.ConnectionFactory</value> </osgi:interfaces> <osgi:service-properties> <entry key="name" value="${name}"/> </osgi:service-properties> </osgi:service>
44
Tools for OSGi - Spring DM
Example: Using OSGi Configuration Admin for Property Placeholder
beans.xml <bean id="myTransform" class="org.apache.servicemix.examples.camel.MyTransform"> <property name="prefix" value="${prefix}"/> </bean> <osgix:property-placeholder persistent-id="org.apache.servicemix.examples"> <osgix:default-properties> <prop key="prefix">MyTransform</prop> </osgix:default-properties> </osgix:property-placeholder>
45
Conclusion
OSGi forces planning and engineering of bundle interactions
More work up front, but rewards come later
No silver bullet, bad code and bad ideas are still bad
Many built in features and services provided for applications
Modularization is rewarded when creating new systems and modifying existing ones
46
Tour of ServiceMix 4 OSGi Features
47
Questions?
48