31
OSGi Remote Services with SCA using Apache Tuscany Raymond Feng [email protected]

OSGi Remote Services With SCA using Apache Tuscany

Embed Size (px)

DESCRIPTION

OSGi goes beyond service invocations in a single JVM with the introduction of RFC 119 - Distributed OSGi. It will enable an OSGi bundle deployed in a JVM to invoke a service (either OSGi or non-OSGi) in another JVM or process, potentially on a remote computer accessed via a network protocol. Meanwhile, an OSGi service deployed in another JVM or a non-OSGi program such as Web Service client, potentially on a remote computer, to find and access a service running in the "local" OSGi JVM (i.e. an OSGi deployment can accept service invocations from remote OSGi bundle or external environments). The distributed computing functionality is added to the OSGi programming model without additional APIs or concepts as the distribution layer will be mostly transparent to OSGi developers by configuration.SCA (Service Component Architecture) provides a technology-neutral approach to abstract business logic into components and assemble them into composite applications. It greatly simplifies the component communications using declarative bindings. QoS requirements can be uniformly declared as SCA intents which can be mapped and realized using different stacks. SCA is a great fit to be a distribution provider for distributed OSGi.Apache Tuscany is an open source project that implements the SCA specifications. It provides integrations of implementation types (such as Java, Scripting, BPEL, Spring, OSGi and JEE) and binding types (such as Web Service, JMS, EJB, CORBA, RMI, JSONRPC, and ATOM) as the infrastructure for SCA programming. The latest version of Tuscany runtime is fully built on top of OSGi as the foundation. We recently added the OSGi RFC 119 support.In this session, we will teach you how to develop a distributed OSGi application to leverage the SCA capabilities using Apache Tuscany. A calculator scenario will be used to demonstrate the distributed OSGi service invocations using RMI and Web Service protocols. We will also explain how to model an OSGi bundle as an SCA component and configure the SCA composite to provide communications between services. The readers will understand the basic ideas behind distributed OSGi in the concrete example and the power of Tuscany SCA for service composition.

Citation preview

Page 1: OSGi Remote Services With SCA using Apache Tuscany

OSGi Remote Services with SCA using Apache Tuscany

Raymond Feng

[email protected]

Page 2: OSGi Remote Services With SCA using Apache Tuscany

Agenda

• What are OSGi remote services?• A sample scenario: Distributed Calculator• Representing OSGi entities using SCA• Predefined mapping from OSGi to SCA• On-demand mapping from OSGi to SCA• Discovery of OSGi remote services• Demo of distributed calculator• Q&A

Page 3: OSGi Remote Services With SCA using Apache Tuscany

What are OSGi remote services?

Page 4: OSGi Remote Services With SCA using Apache Tuscany

OSGi local services

• The OSGi framework decouples service providers and consumers via a local service registry, where a service is an object that one bundle registers and another bundle gets.

• The services are only be accessed locally by bundles within the same framework instance. It would be nice to make them remote without significant changes of the programming model?

Page 5: OSGi Remote Services With SCA using Apache Tuscany

OSGi Remote Services

• The OSGi core framework specifies a model where bundles can use distributed services. (R4.2)

• The basic model for OSGi remote services is that a bundle can:– register services that are exported to a

communication endpoint – use services that are imported from a

communication endpoint (registering a proxy in local service registry)

Page 6: OSGi Remote Services With SCA using Apache Tuscany

OSGi Remote Services

NOTE: The diagram is copied from OSGi Service Platform Service Compendium R4.2 spec

Page 7: OSGi Remote Services With SCA using Apache Tuscany

Related OSGi specs

• OSGi Service Platform Release 4 Version 4.2 Compendium Specification – Chapter 13: Remote Services (PM, concepts and

properties)– http://www.osgi.org/download/r4v42/r4.cmpn.pdf

• Early Access draft of the OSGi 4.2 Enterprise Release – SCA Configuration Type (SCA specific properties)– Remote Service Admin (runtime architecture, SPIs)– http://www.osgi.org/download/osgi-4.2-enterprise-earl

y-draft4.pdf

Page 8: OSGi Remote Services With SCA using Apache Tuscany

SCA Configuration Type

• SCA Configuration Type for Remote Services– This chapter provides a standard mechanism

to configure Remote Services and provide qualities of service or intents, through SCA configuration metadata and WS-Policy. Remote Service implementations that also implement the SCA config type provide a portable way to configuration.

Page 9: OSGi Remote Services With SCA using Apache Tuscany

Remote Service Admin

• Remote Service Admin– This specification adds an extra layer on top of the existing

Remote Services spec (chapter 13 in the 4.2 Compendium). – The Distribution Provider registers a RemoteServiceAdmin

service that exports and imports services when asked. – The Discovery System API (EndpointListener) provides a

standard view over any Discovery System, regardless of how it's realized or what protocol it uses.

– The Topology Manager provides a Policy over these things. It decides what services will be exported and for when to look for services in a Discovery System.

Page 10: OSGi Remote Services With SCA using Apache Tuscany

OSGi remote services- A sample scenario

Page 11: OSGi Remote Services With SCA using Apache Tuscany

An OSGi based Calculator

Add

Service

Operations Bundle

Subtract

Service

Multiply

Service

Divide

ServiceCalculator bundle

Calculator

Service

OSGi service

OSGi service reference

Calculator

ServiceOSGi service interface

OSGi service implementation

Page 12: OSGi Remote Services With SCA using Apache Tuscany

Making the Calculator Distributed

• Run the calculator bundle and the operations bundle on two OSGi framework instances. – The calculator bundle registers the

CalculatorService and it looks up the Add/Subtract/Multiply/Divide services (which are remote over RMI) from the service registry. The CalculatorService is exported as a Web Service.

– The operations bundle registers four remote services (Add/Subtract/Multiply/Divide). These services are exported over RMI.

Page 13: OSGi Remote Services With SCA using Apache Tuscany

OSGi Remote Services enabled Calculator

AddService

Operations Bundle

SubtractService

MultiplyService

DivideService

Calculator bundle

CalculatorService RMIWeb Service

RMI

RMI

RMI

OSGi runtime #1OSGi runtime #1 OSGi runtime #2OSGi runtime #2

Page 14: OSGi Remote Services With SCA using Apache Tuscany

Modeling OSGi entities using SCA

Page 15: OSGi Remote Services With SCA using Apache Tuscany

SCA environment

Add

Service

The Calculator Scenario: OSGi Remote Serviceswith SCA runtime as the distribution software

OSGi bundle

OSGi bundle

SCA Componentimplementation.osgi

SCA Componentimplementation.osgi

Subtract

Service

Multiply

Service

Divide

Service

Calculator

Service

binding.rmi or binding.ws

Page 16: OSGi Remote Services With SCA using Apache Tuscany

OperationsCalculator

binding.rmi

binding.ws

OSGI-INF/sca/bundle.composite

Add

Service

Operations Bundle

Subtract

Service

Multiply

Service

Divide

ServiceCalculator bundle

Calculator

Service

SCA bundle

Defining the SCA composite for OSGi bundles

Page 17: OSGi Remote Services With SCA using Apache Tuscany

SCA view of OSGi services and references

RMIRMI

RMIRMI

RMIRMI

RMIRMI

Calculator

ServiceWeb

ServiceWeb

Service

SCA proxiesSCA proxies

Local ServiceLocal Service

Local OSGi ServiceImplementation

OSGi Service Registry

Add

Service

Subtract

Service

Multiply

Service

Divide

Service

SCA Service

SCA References

Page 18: OSGi Remote Services With SCA using Apache Tuscany

SCA implementation.osgi

• The SCA implementation.osgi component will be used to encapsulate one or more OSGi bundles. – OSGi View: implementation.osgi provides the metadata and

infrastructure to enable the distribution of OSGi services– SCA View: implementation.osgi allows OSGi bundles to

participate in the SCA assembly.

• The references for an SCA OSGi component represents the OSGi services to be consumed by the bundles

• The services for an SCA OSgi component represents the OSGi services to be provided by the bundles

Page 19: OSGi Remote Services With SCA using Apache Tuscany

Predefined mapping from OSGi to SCA

http://svn.apache.org/repos/asf/tuscany/java/sca/samples/dosgi-calculator/http://svn.apache.org/repos/asf/tuscany/java/sca/samples/dosgi-calculator-operations/

Page 20: OSGi Remote Services With SCA using Apache Tuscany

Mapping an OSGi bundle to an SCA componentType

<componentType xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1"> <!-- The service elment defines an SCA view of the OSGi service --> <service name="CalculatorService"> <!-- The interface will be mapped into the OSGi service class --> <interface.java interface="calculator.dosgi.CalculatorService"/></service> <!-- The reference elment defines an SCA proxy to a remote OSGi service --> <reference name="addService"> <interface.java interface="calculator.dosgi.operations.AddService"/> </reference> <reference name="subtractService"> <interface.java interface="calculator.dosgi.operations.SubtractService"/> </reference> <reference name="multiplyService"> <interface.java interface="calculator.dosgi.operations.MultiplyService"/> </reference> <reference name="divideService"> <interface.java interface="calculator.dosgi.operations.DivideService"/> </reference></componentType>

Page 21: OSGi Remote Services With SCA using Apache Tuscany

SCA composite for the OSGi bundle

<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903“ xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1“ targetNamespace=http://calculator.dosgi name="CalculatorComposite"> <component name="CalculatorComponent"> <tuscany:implementation.osgi bundleSymbolicName="calculator.dosgi" bundleVersion="1.0.0" /> <service name="CalculatorService"> <binding.ws uri="http://localhost:8086/CalculatorService"/> </service> <reference name="addService"> <tuscany:binding.rmi uri="rmi://localhost:8085/AddService"/> </reference> <reference name="subtractService"> <tuscany:binding.rmi uri="rmi://localhost:8085/SubtractService"/> </reference> <reference name="multiplyService"> <tuscany:binding.rmi uri="rmi://localhost:8085/MultiplyService"/> </reference> <reference name="divideService"> <tuscany:binding.rmi uri="rmi://localhost:8085/DivideService"/> </reference> </component></composite>

Page 22: OSGi Remote Services With SCA using Apache Tuscany

On-demand mapping from OSGi to SCA

http://svn.apache.org/repos/asf/tuscany/java/sca/samples/dosgi-dynamic-calculator/http://svn.apache.org/repos/asf/tuscany/java/sca/samples/dosgi-dynamic-calculator-operations/

Page 23: OSGi Remote Services With SCA using Apache Tuscany

Remote Service Admin

NOTE: The diagram is copied from OSGi Remote Service Admin spec (to be published)

Page 24: OSGi Remote Services With SCA using Apache Tuscany

Remote Services with SCA

NOTE: The diagram is copied from OSGi Remote Service SCA Configuration Type spec (to be published)

Page 25: OSGi Remote Services With SCA using Apache Tuscany

Publish remote services public void start(BundleContext context) throws Exception { Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("sca.service", "CalculatorComponent#service-

name(Calculator)"); props.put("calculator", "Calculator"); props.put("service.exported.configs", new String[] {"org.osgi.sca"}); props.put("org.osgi.sca.bindings", new String[]

{"{http://sample}Calculator"}); props.put("service.exported.interfaces", new String[] {"*"}); CalculatorService calculator = new CalculatorServiceImpl(context); context.registerService(CalculatorService.class.getName(), calculator,

props);}

Page 26: OSGi Remote Services With SCA using Apache Tuscany

Look up remote services (ServiceTracker)

Filter remoteFilter = null;try { remoteFilter = context.createFilter("(&(" + OBJECTCLASS +

"=calculator.dosgi.operations.*) (service.imported=*))");} catch (InvalidSyntaxException e) { …}this.remoteServices = new ServiceTracker(context, remoteFilter, null);remoteServices.open();…Object[] remoteObjects = remoteServices.getServices();

Page 27: OSGi Remote Services With SCA using Apache Tuscany

MANIFEST.MFManifest-Version: 1.0Export-Package: calculator.dosgi;version="1.0.1", calculator.dosgi.operations;version="1.0.1"Bundle-Version: 1.0.0Bundle-Name: calculator.dosgi.dynamicBundle-Activator: calculator.dosgi.impl.CalculatorActivatorBundle-ManifestVersion: 2Import-Package: org.oasisopen.sca.annotation;version="2.0.0", org.osgi.framework, org.osgi.service.component;resolution:=optional, org.osgi.service.packageadmin, org.osgi.util.trackerBundle-SymbolicName: calculator.dosgi.dynamicBundle-ActivationPolicy: lazyBundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6SCA-Configuration: OSGI-INF/sca-config/calculator-config.xmlRemote-Service: OSGI-INF/remote-service/*.xml

Page 28: OSGi Remote Services With SCA using Apache Tuscany

SCA Configuration<scact:sca-config targetNamespace="http://sample" xmlns:scact="http://www.osgi.org/xmlns/scact/v1.0.0" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"> <sca:binding.ws name="Calculator" uri="http://localhost:8086/CalculatorService"/> <!-- bindings for the remote services --> <tuscany:binding.rmi name="Add" uri="rmi://localhost:8085/AddService"/> <tuscany:binding.rmi name="Divide" uri="rmi://localhost:8085/DivideService"/> <tuscany:binding.rmi name="Subtract" uri="rmi://localhost:8085/SubtractService"/> <tuscany:binding.rmi name="Multiply" uri="rmi://localhost:8085/MultiplyService"/></scact:sca-config>

Page 29: OSGi Remote Services With SCA using Apache Tuscany

Local endpoint descriptions<service-descriptions xmlns="http://www.osgi.org/xmlns/sd/v1.0.0" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"> <!-- Describe a remote OSGi service --> <service-description> <provide interface="calculator.dosgi.operations.AddService" /> <property name="remote.exported.intents"></property> <property name="remote.configs.supported">org.osgi.sca</property> <property name="org.osgi.sca.bindings">{http://sample}Add</property> </service-description>…</service-descriptions>

Page 30: OSGi Remote Services With SCA using Apache Tuscany

SCA Domain based service discovery

Page 31: OSGi Remote Services With SCA using Apache Tuscany

Local/Domain Registry for OSGi discovery

Calculator

Service

SCA proxiesSCA proxies

Local ServiceLocal Service

Local OSGi ServiceImplementation

OSGi Service Registry

Subtract

Service

Multiply

Service

Divide

Service

OSGi Service Registry

Add

Service

Subtract

Service

Multiply

Service

Divide

Service

RMIRMI

RMIRMI

Add

Service

OSGi runtime #1OSGi runtime #1 OSGi runtime #2OSGi runtime #2

Node

Node

ListenerHook

Filter Matching

ServiceTracker

SCA Domain

Endpoint descriptionsin Local XML file

Endpoint descriptionsin Local XML file

SCA Domain Registry•Endpoints•Intent/PolicySet Definitions

SCA Domain Registry•Endpoints•Intent/PolicySet Definitions