OUGF - OSGi / Flex
- 1. Building complex and modular RIAs with
OSGi and Flex
francois.fornaciari@zenika.com
www.zenika.com
- 2. Agenda
• Introduction
• Flex and OSGi interactions
• Application modularization
• Demo
• Conclusion
www.zenika.com
- 3. What is Flex (1/2)
• Framework for building RIA
• Flash technology
• Free Software Development Kit (SDK)
• Flash Player (VM)
• Strong integration with Java
• Tooling
• Flash Builder : IDE based on Eclipse
• IntelliJ IDEA
www.zenika.com
- 4. What is Flex (2/2)
• MXML: XML-based language
• ActionScript: ECMAScript-compliant scripting
• Library of pre-built components
• Compiler: create SWFs from MXML/ActionScript
www.zenika.com
- 5. 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>
www.zenika.com
- 6. ActionScript
• Core of the Flex Framework
• ECMAScript-compliant scripting
• Familiar syntax
package com.zenika.flex {
public class MyClass interface MyInterface {
public function MyClass() {
}
public function doSomething():String {
}
}
}
www.zenika.com
- 7. What is OSGi (1/3)
• A module system and service platform for the Java
programming language
• OSGi framework specifications
• Core Specification
• Compendium Specification
• Enterprise Specification
www.zenika.com
- 8. What is OSGi (2/3)
• Service Oriented Architecture
Service
broker
Lookup Register
Specification
Binding
Consumer Provider
www.zenika.com
- 9. What is OSGi (3/3)
• Bundle
• Deployment unit: JAR + MANIFEST
• Functional unit (provides services)
• Application
• Set of bundles
• Dynamically deployed
• Potentially shared between applications
www.zenika.com
- 10. Purpose
• Presentation of the main design patterns for building
complex and modular applications with Flex and OSGi
• Two axes
• Communication between Flex and OSGi
• Application modularization
www.zenika.com
- 11. General architecture
Client (browser) Server (Java)
Flex HTTPService OSGi Framework
WebService
RemoteObject
Producer
SWF Application
SWF Module
SWF Module
Consumer
Bundle
Bundle
Bundle
www.zenika.com
- 12. 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, …
www.zenika.com
- 13. 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 {...}
www.zenika.com
- 14. Communications (1/2)
• Accessing “non-OSGi services”
• 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 remote servers
• ex: exposed through D-OSGi
www.zenika.com
- 15. Communications (2/2)
• 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
• Unsafe access to properties and methods
• Introspection API
• Producer/Consumer
• Topic subscription
www.zenika.com
- 16. AMF Protocol (1/2)
• 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, …
www.zenika.com
- 17. Integration frameworks
• Existing open source frameworks
Name OSGi support RemoteObject Producer / Consumer
AMF3 for OSGi (LGPL) yes yes yes
GraniteDS (LGPL) yes (prototype) yes (unstable) no
Spring Flex (ASL) yes (Virgo) yes yes
• AMF3 for OSGi (Christopher Brind)
• Easy to use
• Only 3 bundles to deploy
• Robust
www.zenika.com
- 18. AMF3 for OSGi
• OSGi bundles 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
www.zenika.com
- 19. 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)
www.zenika.com
- 20. 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
www.zenika.com
- 21. 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();
...
}
www.zenika.com
- 22. 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">
<mx:method name="doSometing"
result="onResult(event)"
fault="onFault(event)" />
</s:RemoteObject>
www.zenika.com
- 23. Type-safe objects
• Method arguments and result
[RemoteClass(alias="com.zenika.systemmanager...SWFModule")]
[Bindable]
public class SWFModule
www.zenika.com
- 24. Producer / Consumer (1/2)
• Bridge between Flex events and EventAdmin service
• Elements attributes
• Consumer: callback when a message is received
• Consumer and producer: destination
<mx:Consumer message="onMessage(event)"
destination="events"
fault="onFault(event)" />
<mx:Producer destination="events"
fault="onFault(event)" />
www.zenika.com
- 25. 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;
www.zenika.com
- 26. 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));
}
www.zenika.com
- 27. 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);
www.zenika.com
- 28. Receiving message (1/2)
public class FlexEventHandler implements EventHandler {
@ServiceProperty(name="event.topics")
private String eventTopics =
".../amf3/flex/remoting/events/PublishedObjectEvent";
public void handleEvent(Event event) {
if (event instanceof PublishedObjectEvent) { ...}
}
}
All Flex messages
are received by
this handler
www.zenika.com
- 29. Receiving message (2/2)
• Message: typed-object
• Start / cancel subscription
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;
...
}
www.zenika.com
- 30. Some key elements
• We have seen how to:
• Expose OSGi services to Flex
• Interact with different service types
• Remote Objects
• Event-based messages
• Standard web services
• Define strongly-typed objects
• Handle events posted by the backend
www.zenika.com
- 31. Flex modulary
• Flex modules
• SWF files that can be loaded and unloaded at runtime by an
application
• Cannot be run independently
• Applications can share modules
• No need to load all modules at startup
www.zenika.com
- 32. Benefits
• Split Flex application into several pieces
• Smaller initial download size of the SWF
• Shorter load time
• Better encapsulation
• Load required modules only
• Unload modules at runtime
• Free up memory and resources
www.zenika.com
- 33. Main goal
• OSGi is the Java modularity
• Flex supports modules
• How can we build a complete modular application?
www.zenika.com
- 34. 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>
www.zenika.com
- 35. 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;
}
}
www.zenika.com
- 36. Packaging
• Each bundle contains
• Business logic
• OSGi services
Application Module
bundle bundle • User interface
• SWF file
JAVA JAVA • Build may be done with
(OSGi) (OSGi) Maven
UI UI
(SWF) (SWF)
www.zenika.com
- 37. 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
www.zenika.com
- 38. Technical view (1/3)
• Registers bundle resources into the HTTP Service
• Main SWF, HTML files, …
• Listens to bundle arrival / departure
• Implementation of the extender pattern
• Use of a BundeTracker for being notified of bundles declaring
Flex modules
• MANIFEST.MF : SWF-Modules: [path]=[SWF name]
• Register/unregister SWFs into declared path
• Post event to notify Flex client
www.zenika.com
- 39. Technical view (2/3)
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();
}
www.zenika.com
- 40. Technical view (3/3)
• 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
www.zenika.com
- 41. Module bundle
• Autonomous module
• Contains its own UI and logic
• Good isolation
• Flex modules can easily be based upon OSGi services
www.zenika.com
- 42. Démo
DEMO
www.zenika.com
- 43. 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
www.zenika.com
- 44. Conclusion (2/2)
• Seamless communications
• Asynchronous remote calls
• Notifications
• Integration
• Application: set of autonomous bundles with their own
interface and business logic
www.zenika.com