building complex and modular rias with osgi and flex

53
Building complex and modular RIAs with OSGi™ and Flex ® [email protected] www.zenika.com

Upload: caralyon

Post on 11-Nov-2014

3.280 views

Category:

Technology


0 download

DESCRIPTION

François fornaciari @Mix-IT 2011

TRANSCRIPT

Page 1: Building complex and modular RIAs with OSGi and Flex

Building complex and modular

RIAs with OSGi™ and Flex®

[email protected]

www.zenika.com

Page 2: Building complex and modular RIAs with OSGi and Flex

Content

Introduction

OSGi™ and Flex® interactions

Application modularization

Demo

Conclusion

Mix-IT 2011 - François Fornaciari

Page 3: Building complex and modular RIAs with OSGi and Flex

Bio

Java EE / RIA consultant and trainer

× Working on Flex projects at Manpower

Board member of the OSGi™ Users’ Group

France

Over the last 4 years

× Participation in the refactoring of the new

version of JOnAS based on OSGi™

Mix-IT 2011 - François Fornaciari

Page 4: Building complex and modular RIAs with OSGi and Flex

What is Flex® (1/2)

Framework for building RIA

× Flash technology

× Flash Player (VM)

× Free Software Development Kit (SDK)

× Strong integration with Java

× Tooling

× Flash Builder™ : IDE based on Eclipse

× IntelliJ IDEA

Mix-IT 2011 - François Fornaciari

Page 5: Building complex and modular RIAs with OSGi and Flex

What is Flex® (2/2)

MXML: XML-based language

ActionScript: ECMAScript-compliant

scripting

Library of pre-built components

Compiler: create SWFs from

MXML/ActionScript files

Mix-IT 2011 - François Fornaciari

Page 6: Building complex and modular RIAs with OSGi and Flex

MXML

XML-based language

Declarative way to build applications

<s:Application

xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx">

<s:Panel>

<s:Label text="Label" />

<s:Button label="Button" />

</s:Panel>

</s:Application>

Mix-IT 2011 - François Fornaciari

Page 7: Building complex and modular RIAs with OSGi and Flex

ActionScript

Core of the Flex® Framework

Familiar syntax

package com.zenika.flex {

public class MyClass interface MyInterface {

public function MyClass() {

}

public function doSomething():String {

}

}

}

Mix-IT 2011 - François Fornaciari

Page 8: Building complex and modular RIAs with OSGi and Flex

What is OSGi™

OSGi™ framework specifications (4.2)

× Core Specification

× Compendium Specification

× Enterprise Specification

A module system and service platform for

the Java programming language

Mix-IT 2011 - François Fornaciari

Page 9: Building complex and modular RIAs with OSGi and Flex

OSGi™ Bundles

Bundle

× Deployment unit: JAR + MANIFEST

× Functional unit (provides services)

Application

× Set of bundles

× Dynamically deployed

× Potentially shared between applications

Mix-IT 2011 - François Fornaciari

Page 10: Building complex and modular RIAs with OSGi and Flex

Bundle’s structure

Required services

Provided services

Imported packages

Exported packages

classes

manifest

res

Mix-IT 2011 - François Fornaciari

Page 11: Building complex and modular RIAs with OSGi and Flex

MANIFEST file

# Package(s) requis

Import-Package: com.zenika.interface;version=1.1.0

# Package(s) fournis

Export-Package: com.zenika.service;version=2.0.0

# Identifiant du bundle : unique pour la plate-forme

Bundle-SymbolicName: com.zenika.example

# Version du bundle

Bundle-Version: 2.1.0

# Nom du bundle

Bundle-Name: Exemple

# Nom de la classe Activator (l’équivalent du Main)

Bundle-Activator: com.zenika.internal.Activator

Mix-IT 2011 - François Fornaciari

Page 12: Building complex and modular RIAs with OSGi and Flex

Bundle lifecycle

Mix-IT 2011 - François Fornaciari

Page 13: Building complex and modular RIAs with OSGi and Flex

OSGi™ Services (1/2)

Service Oriented Architecture

Service broker

Consumer Provider

RegisterLookup

Binding

Specification

Mix-IT 2011 - François Fornaciari

Page 14: Building complex and modular RIAs with OSGi and Flex

OSGi™ Services (2/2)

Java™ Interface

× Generally, the package is exported by a

dedicated bundle

One or many implementations

× Packaged in a bundle

× Generally not public: package not exported

Identified by some properties

Implementation classcom.zenika.printer.laser.Driver

Interfacecom.zenika.device.PrintService(color=true)

Mix-IT 2011 - François Fornaciari

Page 15: Building complex and modular RIAs with OSGi and Flex

Purpose

Presentation of the main design patterns for

building complex and modular applications

with OSGi™ and Flex®

Two axes

× Communication between Flex® and OSGi™

× Application modularization

Mix-IT 2011 - François Fornaciari

Page 16: Building complex and modular RIAs with OSGi and Flex

General architecture

Flex® OSGi™ Framework

Client (browser) Server (Java)

HTTPServiceWebServiceRemoteObjectProducerConsumer

SWF

Ap

plic

atio

n

SWF

Mo

du

le

SWF

Mo

du

le

Bundle

Bundle

Bu

nd

le

Mix-IT 2011 - François Fornaciari

Page 17: Building complex and modular RIAs with OSGi and Flex

Handling remote calls

HTTPService, WebService, Remote

Objects, Consumer/Producer

× Asynchronous calls

× Result, Fault or Message handlers

× Arguments and result types

× Strongly typed or generic objects

× XML format

× Properties

× URL, destination, endpoint, id, …

Mix-IT 2011 - François Fornaciari

Page 18: Building complex and modular RIAs with OSGi and Flex

Service call example

private function callService():void {

var service:MyService = new MyService(destination);

service.addEventListener(ResultEvent.RESULT,

onResult);

service.addEventListener(FaultEvent.FAULT, onFault);

service.send();

}

private function onResult(event:ResultEvent):void {

var result:MyObject = event.result as MyObject;

...

}

private function onFault(event:FaultEvent):void {...}

Mix-IT 2011 - François Fornaciari

Page 19: Building complex and modular RIAs with OSGi and Flex

Accessing web services (1/2)

“Non-OSGi™” services

Flex consumer objects

× HTTPService

× Supported methods: GET | POST

× HEAD | OPTIONS | PUT | TRACE | DELETE only

through the server-based proxy service

× WebService

× Provides access to SOAP-based web services on

local or remote servers

Mix-IT 2011 - François Fornaciari

Page 20: Building complex and modular RIAs with OSGi and Flex

Accessing web services (2/2)

Expose OSGi™ services through the

“Remote Services Specification”

× Development of OSGi™ standard services

× Service properties for defining

× SOAP-based services

× RESTful JAXRS-based endpoints

× Implementation: Distributed OSGi™

× Sub-project of Apache CXF

Mix-IT 2011 - François Fornaciari

Page 21: Building complex and modular RIAs with OSGi and Flex

Accessing OSGi™ services

In a transparent way

Two communication types

× RemoteObject

× Simple call to an existing OSGi™ service

× Strongly-typed arguments and result or …

× … generic objects

× Producer/Consumer

× Topic subscription

Mix-IT 2011 - François Fornaciari

Page 22: Building complex and modular RIAs with OSGi and Flex

AMF Protocol

Action Message Format (v3)

× Binary format used to serialize ActionScript

objects

× Optimized transferred data amount

× Primarily used to exchange data between

Adobe Flash applications and remote services

× Specification since 2007

× Supported by many server-side languages and

technologies: Java™, .NET, PHP, …

Mix-IT 2011 - François Fornaciari

Page 23: Building complex and modular RIAs with OSGi and Flex

Integration frameworks

Existing open source frameworks

AMF3 for OSGi™ (Christopher Brind)

× Robust, OSGi™ compliant and easy to use

× Only 3 bundles to deploy

Name OSGi™ support RemoteObject OSGi™ EventAdmin

bridge

AMF3 for OSGi™ (LGPL) yes yes yes

GraniteDS (LGPL) yes (prototype) yes (unstable) no

Spring Flex (ASL) yes (Virgo) yes no

Mix-IT 2011 - François Fornaciari

Page 24: Building complex and modular RIAs with OSGi and Flex

AMF3 for OSGi™

OSGi™ bundle description

× uk.co.arum.osgi.amf3

× Serialization-deserialization of AMF3 objects

× uk.co.arum.osgi.amf3.http

× Registers an HTTP servlet that is capable of reading

and writing AMF3 objects

× uk.co.arum.osgi.amf3.flex.remoting

× Implements FlexRemoting over HTTP

× Provides support for channel based messaging

Mix-IT 2011 - François Fornaciari

Page 25: Building complex and modular RIAs with OSGi and Flex

Apache Felix projects (1/2)

Core of the server-side application

× Framework

× File Install

× Directory based OSGi™ management agent

× Used internally by AMF3 for OSGi ™

× Declarative Service (SCR)

× Event Admin Service

× Log Service

× HTTP Service (Jetty)

Mix-IT 2011 - François Fornaciari

Page 26: Building complex and modular RIAs with OSGi and Flex

Apache Felix projects (2/2)

Application-specific projects

× Event Admin Service

× OSGi™ EventHandler receives messages from Flex

Producers

× Messages posted by the OSGi™ EventAdmin

service are received by Flex Consumers

× iPOJO

× Used to declare OSGi™ components

× Support of annotations

× Extensible handlers

Mix-IT 2011 - François Fornaciari

Page 27: Building complex and modular RIAs with OSGi and Flex

Event Admin

Based on the Publish-Subscribe pattern

PublisherEventAdmin« service »

postEvent(topics)

EventHandler« service »

handleEvent(topics)

Mix-IT 2011 - François Fornaciari

Page 28: Building complex and modular RIAs with OSGi and Flex

Publishing OSGi™ services

Additional service property

× AMF_SERVICE_NAME

× Instantiated with the service class name

@Component(name="MyService")

@Provides

public class MyServiceImpl implements MyService {

@ServiceProperty(name="AMF_SERVICE_NAME")

private String serviceName = MyService.class.getName();

...

}

Mix-IT 2011 - François Fornaciari

Page 29: Building complex and modular RIAs with OSGi and Flex

Consuming OSGi™ services

Flex RemoteObject

× Endpoint: AMF3 servlet path

× ex: http://localhost:8080/amf3osgi

× Destination: OSGi™ service class name

<s:RemoteObject id="myService"

endpoint="/amf3osgi"

destination="com.zenika.service.MyService">

<s:method name="doSometing"

result="onResult(event)"

fault="onFault(event)" />

</s:RemoteObject>

Mix-IT 2011 - François Fornaciari

Page 30: Building complex and modular RIAs with OSGi and Flex

Type-safe objects

Method arguments and result

[RemoteClass(alias="com.zenika.systemmanager...SWFModule")]

[Bindable]

public class SWFModule

Mix-IT 2011 - François Fornaciari

Page 31: Building complex and modular RIAs with OSGi and Flex

Producer / Consumer (1/2)

Bridge between Flex® events and

EventAdmin service

Elements attributes

× Consumer: callback when a message is

received

× Consumer and producer: destination

<s:Consumer message="onMessage(event)"

destination="events"

fault="onFault(event)" />

<s:Producer destination="events"

fault="onFault(event)" />

Mix-IT 2011 - François Fornaciari

Page 32: Building complex and modular RIAs with OSGi and Flex

Producer / Consumer (2/2)

Destination configuration

× Set of channels used to send messages to a

target destination

× QoS: many channels for network failure tolerance

var channelSet:ChannelSet = new ChannelSet();

var channel:AMFChannel =

new AMFChannel("events","/amf3osgi");

channel.pollingInterval = 5000;

channelSet.addChannel(channel);

consumer.channelSet = channelSet;

Mix-IT 2011 - François Fornaciari

Page 33: Building complex and modular RIAs with OSGi and Flex

Sending messages (1/2)

From OSGi™

× PublishedObjectEvent (channelID, object)

× Extends org.osgi.service.event.Event

@Bind

public void bindEventAdmin(EventAdmin eventAdmin) {

this.eventAdmin = eventAdmin;

}

private void sendMessage() {

eventAdmin.postEvent(

new PublishedObjectEvent("events", object));

}

Mix-IT 2011 - François Fornaciari

Page 34: Building complex and modular RIAs with OSGi and Flex

Sending messages (2/2)

From Flex®

× Sending AsyncMessage through the producer

× Body: object to send

var message:AsyncMessage = new AsyncMessage();

message.body = messageObject;

producer.send(message);

Mix-IT 2011 - François Fornaciari

Page 35: Building complex and modular RIAs with OSGi and Flex

Receiving message (1/2)

@Component(name="FlexEventHandler")

@Provides

public class FlexEventHandler implements EventHandler {

@ServiceProperty(name="event.topics")

private String eventTopics =

".../amf3/flex/remoting/events/PublishedObjectEvent";

@ServiceProperty(name="event.filter")

private String eventFilter = "(channel.id=events)";

public void handleEvent(Event event) {

if (event instanceof PublishedObjectEvent) { ...}

}

}

Mix-IT 2011 - François Fornaciari

Page 36: Building complex and modular RIAs with OSGi and Flex

Receiving message (2/2)

Start / cancel subscription

Message: typed-object

private function start():void {

consumer.subscribe();

}

private function stop():void {

consumer.unsubscribe();

}

private function onMessage(event:MessageEvent):void {

var msg:MyMessage = event.message.body as MyMessage;

...

}

Mix-IT 2011 - François Fornaciari

Page 37: Building complex and modular RIAs with OSGi and Flex

Some key elements

We have seen how to:

× Interact with different communication types

× Standard web services

× Remote Objects

× Event-based messages

× Expose OSGi™ services to Flex®

× Handle events posted by the client-side or the

backend

Mix-IT 2011 - François Fornaciari

Page 38: Building complex and modular RIAs with OSGi and Flex

Flex® modules

SWF Files

Loaded / unloaded at runtime

Better encapsulation

Separated developments

Compilation of one part of the application

Cached by the browser

Monitoring loading process

Cannot be run independently

Mix-IT 2011 - François Fornaciari

Page 39: Building complex and modular RIAs with OSGi and Flex

Main goal

OSGi™ is the Java modularity

Flex® supports modules

How can we build a complete modular

application?

Mix-IT 2011 - François Fornaciari

Page 40: Building complex and modular RIAs with OSGi and Flex

Creating modules

“Module” as root element

<mx:Module

xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"

implements="...api.ModuleInterface"

creationComplete="start()">

<fx:Script>

<![CDATA[

public function start():void {...}

public function stop():void {...}

]]>

</fx:Script>

<s:Label text="Label" />

</mx:Module>

Mix-IT 2011 - François Fornaciari

Page 41: Building complex and modular RIAs with OSGi and Flex

Module lifecycle

Start method is called when the module is

loaded

Stop method is called before unloading the

module

× Common interface between the application and

the module

package com.zenika.systemmanager.api {

public interface ModuleInterface {

function stop():void;

}

}

Mix-IT 2011 - François Fornaciari

Page 42: Building complex and modular RIAs with OSGi and Flex

Module

Bundle

Packaging

Each bundle contains

× Business logic× OSGi™ services

× User interface× SWF file

Building tools likeMaven, Ant or Gradle can ease the packaging phase

Application

Bundle

UI

(SWF)

JAVA(OSGi™)

manifest manifest

UI

(SWF)

JAVA(OSGi™)

Mix-IT 2011 - François Fornaciari

Page 43: Building complex and modular RIAs with OSGi and Flex

Application bundle

User Interface skeleton

Able to retrieve list of available modules

× OSGi™ service for getting module URLs

Flex® client notified when a module

appears or disappears

× Flex consumer is aware of events posted by

the EventAdmin

Mix-IT 2011 - François Fornaciari

Page 44: Building complex and modular RIAs with OSGi and Flex

Technical view (1/4)

At startup, registers bundle resources by

using the HTTP Service

× Main SWF, HTML files, …

Gets registered modules and listens to

bundle arrival / departure

× Implementation of the extender pattern

Mix-IT 2011 - François Fornaciari

Page 45: Building complex and modular RIAs with OSGi and Flex

Technical view (2/4)

Extender pattern

× Use of a BundleTracker in order to be notified

of bundles declaring Flex® modules

× MANIFEST.MF :

– SWF-Modules: [path]=[SWF name]

× Register / unregister SWFs into path

× Creation of a resource-aware context

× Post event to notify Flex® client

Mix-IT 2011 - François Fornaciari

Page 46: Building complex and modular RIAs with OSGi and Flex

Technical view (3/4)

var modules:ArrayCollection = event.result as

ArrayCollection;

for each(var module:SWFModule in modules) {

var loader:ModuleLoader = new ModuleLoader(module.url);

// Register event listeners

loader.addEventListener(ModuleEvent.READY,

handleModuleReady);

loader.addEventListener(ModuleEvent.ERROR,

handleModuleError);

// Start the module loading

loader.load();

}

Mix-IT 2011 - François Fornaciari

Page 47: Building complex and modular RIAs with OSGi and Flex

Technical view (4/4)

After bundle undeployment

× Call the stop method

× Stop properly on-going processes

× Warning: OSGi™ services have already been

unregistered

× Unload associated modules

× Free-up memory and resources

Mix-IT 2011 - François Fornaciari

Page 48: Building complex and modular RIAs with OSGi and Flex

Module bundle

Autonomous module

× Contains its own UI and logic

× Good isolation

× Flex® modules have to be based upon their

own OSGi™ services

× Avoid using unregistered services or managing

service dynamicity on the client-side

Mix-IT 2011 - François Fornaciari

Page 49: Building complex and modular RIAs with OSGi and Flex

DEMO

Page 50: Building complex and modular RIAs with OSGi and Flex

Conclusion (1/2)

Flex®

× Development of rich interfaces

× Native support of modules

× Loading / unloading modules at runtime

OSGi™

× The Dynamic Module System for Java™

× Service Oriented Architecture

× Service availability to Flex®

Mix-IT 2011 - François Fornaciari

Page 51: Building complex and modular RIAs with OSGi and Flex

Conclusion (2/2)

Seamless communications

× Asynchronous remote calls

× Notifications

Integration

× Application: set of autonomous bundles with

their own interface and business logic

Mix-IT 2011 - François Fornaciari

Page 52: Building complex and modular RIAs with OSGi and Flex

References

OSGi™ : www.osgi.org

Apache Felix: felix.apache.org

Adobe Flex® :

www.adobe.com/fr/products/flex

AMF3 for OSGi™ :

www.arum.co.uk/amf3osgi.php

Mix-IT 2011 - François Fornaciari

Page 53: Building complex and modular RIAs with OSGi and Flex

Questions

[email protected]

www.zenika.com

Mix-IT 2011 - François Fornaciari