Transcript of "Giorgio Natilli - Blaze DS Connectivity Framework"

2.
During the session
I. Introduction
II. Blaze DS and Live Cycle DS
III. How to invoke Java from Flex
I. Setting up the environment
II. Create a simple script
IV. A centralized approach
I. Service Connector (part of the nabiro class set)
I. The ActionScript classes involved
II. The analysis of the class
II. Model View Presenter
V. Demo

3.
It’s RIA time
• The adoption of Rich Internet Applications is increasing in a
very different range of contexts
- Business
- Education
- Entertainment
• If your software has an architecture where the business logic
is clearly separated from other layers, then you can use one
of the available technologies (or mix all the best features of
each technology) and develop a RIA

4.
RIA solutions
• The main solution you can find on the market today for the
front end implementation of a RIA are
– Java FX
– Silverlight
– Ajax
– Flash
• Accordingly to your choice people can say:
- He’s a real programmer
- He’s good guy but too much related to MS
- He’s a scripter
- He’s a graphic designer, he can’t do a software

6.
And then comes Flex...
• I totally agree with the first reaction of people with Flash, the
skip intro nightmare is still live in my mind!
• But I have to be honest in my experience with the Flex
framework you can get great cross platform result and reach
a wide range of users because the Flash Player is omni
present and a de facto standard in the web development
• The use of the Flash Player is not only related to Flex, each
Flash application may be a good one or a bad one
accordingly to the way you design it

7.
Blaze DS and Live Cycle DS
• Blaze DS and Live Cycle DS (LCDS) represent today the
two main choices developers can do when they are planning
a new RIA connected with Java or Coldfusion
• The first one is open source (great!) and the second one is a
close source (less great but a very good tool!) product with a
greater set of functionalities
• There are obviously a lot of difference between the two
solutions but the choice depends only on your needs

8.
AMF communication
• One of the key point of these software is the communication
over AMF
• Action Message Format (AMF) is a compact binary format
that is used to serialize ActionScript object graphs
• Once serialized an AMF encoded object graph may be used
to persist and retrieve the public state of an application across
sessions or allow two endpoints to communicate through the
exchange of strongly typed data

9.
AMF 10 times faster, how?
• Is a compact binary format for data serialization / de-
serialization and remote method invocation
• Object encoding controls how objects are represented in
Action Message Format (AMF)
• Representation can be transferred over HTTP/HTTPS
• As data size increases the performance benefits of using
Blaze DS increase exponentially
• Objects casting is performed on the server side part

12.
And the performance?
• Accordingly to the features you are planning to use the
performance can change
- RPC services over an AMF Channel -> NO difference
- Data pushing -> A LOT of difference
LCDS
Blaze DS

13.
Why this difference?
• The reason why is that Blaze DS supports long-polling and
streaming over HTTP to push data to the client but it
manages this through the Servlet API which has the
restriction right now of mandating blocking IO
• LCDS provides support for the RTMPChannel (direct duplex
socket connection between the client and server) as well as
non-blocking long-polling and streaming support over HTTP
that bypasses the Servlet API and its blocking IO limitation
• All of these options in LCDS are built on top of the Java NIO
APIs

14.
Blaze benefits
• Blaze DS is open source and it’s extendable so you can add
your modules or functionalities
• Supports the main Java servers
– Tomcat
– Jboss
– Glassfish
– Others…
• Blaze DS is shipped in bundle with Tomcat so it’s easy to start
also without a Java background

15.
Blaze effect
Java developers and Flash developers became friends and start
to work together….

16.
How to invoke Java from Flex
• In order to start with Java and Flex you can download and
setup Blaze DS on your system from Adobe Labs http://
labs.adobe.com/technologies/blazeds/
• In order to invoke Java methods from Flex application
I. Add a mapping to the Java class to services-config.xml
II. Map your RemoteObject (provided with Flex SDK) on the client side
to the destination configured in services-config.xml
III. Invoke the method in your Java class using the RemoteObject
instance

17.
It’s so simple
• In your Flex application use MXML in order to invoke a
remote method you can use the following snippet of code
<mx:RemoteObject id=”remoteObj” destination=”testRpc”
result=”Alert.show(event.result.toString());”
fault=”Alert.show(event.fault.faultString);” />
<mx:Button label=”Remote Service”
click=”remoteObj.testRemoteMethod();”/>
• The destination is defined in your services-config.xml and the
testRemoteMethod() is defined in your java class on the
server and you can handle the result and fault event via
MXML or via ActionScript for each remote object

18.
Nothing against simple, but…
I totally agree with you, it seems to be the typical script
approach, the first questions that can came out probably are
I. And what happens in a large application?
II. How long is the equivalent code in ActionScript?
III.Why I have to learn this if I’m not sure of the results?
IV. How many killer application really use it?
V. Is it an application that use MXML to handle the
communication with the business logic really scalable?
VI. Even more…

24.
A centralized approach
• The development of enterprise application involves actually a
large number of server side calls
• You can work with remote objects defined in each component
handling the result and the fault event in each component or
you can work in a centralized way with only one class that
handle for you all the remote operations
• There is not a native solution in Flex, you have tor write down
your own solution

25.
The Service Connector
• The ServiceConnector class is not part of the Flex
framework, it’s part of a set of utilities I created for my
projects
• The packaging structure
– Events
– Remote
– Utils
– Main classes

26.
Service Connector features
• The ServiceConnector class is a Singleton in order to
avoid multiple instances of the class in the same application
and provides the public methods a developer needs in order
to perform authenticated and anonymous call against java
services deployed under Blaze DS
• The ServiceConnector class is also able to perform the
login and the logout of a user, to handle the queue of multiple
calls following a FIFO style and to recover a service using the
name of the method you need to call
• The ServiceConnector class implements the
IEventDispatcher interface and therefore is the
responsible of the notification of all the events fired during the
server side interaction

27.
Used classes
– AMFChannel and SecureAMFChannel; provides the AMF
support for messaging. You can configure this Channel to poll the
server at an interval to approximate server push
– Channel; is the base message channel class that all
channels in the messaging system must extend
– ChannelSet; is a set of Channels that are used to send
messages to a target destination
– AbstractOperation; represents an individual method on
a service
– AbstractService; is the base class for the WebService
and RemoteObject classes
– AsyncResponder; allows the creator to associate data (a
token) and methods that should be called when a request is completed
– AsyncToken; provides a place to set additional or token-
level data for asynchronous RPC operations

30.
The RemoteObjectWrapperEvent
• The RemoteObjectWrapperEvent class is a custom
Event that uses two public static constants in order to
define the FAULT and RESULT type of the event
• It’s the one that bring the data recovered out from the
ServiceConnector to your application
• It’s the one that has the knowledge of the remote
method (with an instance of the AbstractService)
invoked that is used to notify the appropriate control that
data are coming

31.
The IRemoteMethod interface
The IRemoteMethod is the interface implemented by the remote
method called trough the ServiceConnector class, each method needs to
define the following accessor methods (getters not shown for brevity)
function set name(value:String):void;
function set arguments(value:Array):void;
function set source(value:String):void;
function set destination(value:String):void;
function set returnObject(value:Class):void;
function set isList(value:Boolean):void;

32.
The IRemoteMethod interface
• The source and the destination are used in order to
define the service to call
• The returnObject is the class type of the object /
objects returned from the method invocation
• The isList is used in order to understand if the
remote method returns a single value or an Array of
values
• arguments is self explanatory

33.
The RemoteMethod class
The RemoteMethod implements the IRemoteMethod
interface, the use of an interface here is due to possible
future enhancement of the micro architecture we use for
the server side communication

34.
The MethodQueue class
• The MethodsQueue class follows the Singleton design
pattern, when the application calls more than one
method from different components is trough this class
that the queue of results is handled and the results are
sent to the appropriate component
• Each item stored in the MethodQueue
class is an instance of the class
MethodsQueueElement
that defines the method
and abstract operation performed

35.
The ServerSideCallEvent
• The ServerSideCallEvent is a bubbling event that
in the Flex architecture (actually in the Flash Player) is
able to reach an upper level component
• The ServerSideCallEvent is the one that drives to
the ServiceConnector the method to call

36.
The ServiceConnector in 5 steps (1/5)
• Create the ServiceConnector instance and specify which
is the object that needs to receive the events dispatched
from the class
sc = ServiceConnector.getConnector(this);
• Set the default services for the ServiceConnector
instance
sc.defaultSource = quot;com.gnstudio.services.Adminquot;;
sc.defaultDestination = quot;Adminquot;;
• Define the listeners for the events
sc.addEventListener(RemoteObjectWrapperEvent.FAULT, onFault)
sc.addEventListener(RemoteObjectWrapperEvent.RESULT,
onResult)

37.
The ServiceConnector in 5 steps (2/5)
• Define the listeners for the event that will generate the
server side calls
addEventListener(ServerSideCallEvent.ANONYMOUS_CALL,
onAnonymousCall);
addEventListener(ServerSideCallEvent.LOGGED_CALL,
onLoggedCall);
• Create the hash map used to handle the queue of the
server side calls, the map is the one used also to send
the data to a specific component
hash = new HashMap();

41.
How to put everything in action
• Use the Model View Presenter for your component
• Make a comparison between Model View Presenter and
Model View Controller
• Understand why in an UI context the MVP is the best choice
with external data
• Make a simple implementation

42.
The MVP pattern
• The original implementation of the MVP is born in the 1979,
its name was Thing Model View Editor and during these
years there were a lot of different implementations between
them the Taligent’s one (1996) was for sure the more
sophisticated and the more powerful
• The Model-View-Presenter pattern is a variation on the Model-
View-Controller pattern, and similarly separates the concerns of an
application’s data, presentation, and user input into specialized
components

44.
The MVP actors
• The Model refers to the data and business functionality of the
application
• Selections are components which specify what portion of the data
within the Model is to be operated upon
• Commands are classes which define the operations which can be
performed on the data
• The View is the visual representation of the Model and is comprised
of the screens and widgets used within an application
• Interactors are classes which address how user events are
mapped onto operations performed on the Model
• The Presenter is a component which orchestrates the overall
interaction of the other components within the application

46.
MVP and MVC (2/2)
MVC MVP
The three components would interact Presenter can also interact with View to
with each other, Controller would access Model
sometime also be responsible to update
view (like Front Controller Pattern)
Controller is behavior based and Usually one view have one presenter
multiple views can share single controller (1-1 mapping), multiple presenters
would be associated with a complex view
Identifies which view to update Presenter will update its associated view