The document provides an overview of using BlazeDS to integrate Flex and Java applications. It discusses BlazeDS remoting services which allow Flex clients to invoke methods on Java objects, messaging services to enable publish-subscribe communication between Flex clients, and proxy services to integrate multiple external services. Code examples are provided for creating remote objects and producers/consumers to call remoting services and send/receive messages. Configuration files and settings for remoting, messaging and channels are also summarized.
1. 1
FLEX & JAVA USING
BLAZEDS
Hien Luu 2009 Silicon Valley Code Camp
2. About Me
2
Senior Software Engineer at Netflix
Many years with developing web-based
applications with Java technologies
Instructor at UCSC-Extension school
RIA with Flex & Java
Spring Framework
Design Patterns
7. AMF
7
AMF (Action Message Format)
Wire protocol
Fast, small – 10x over XML
AMF3 specification
Open Source AMF Implementation
AMFPHP
OpenAMF
rubyAMF
PyAMF
8. BlazeDS Tour
8
Download Turnkey version
Include Tomcat 6.x
BlazeDS sample applications
Flex 3 SDK
Unzip and start Tomcat and HSQL
Come with sample database – product and census data
http://localhost:8400/
9. BlazeDS Architecture
9
BlazeDS Services
Access to enterprise
functionality
RPC Service
Let Flex clients
communicate with
each other
Messaging Service
asynchronously
Proxy service to
integrate multiple
services with a
Proxy Service single application
10. BlazeDS Architecture
10
Flex Client Architecture
Flex Client J2EE Server
BlazeDS Component
BlazeDS
RemoteObject
HTTPService
WebService
HTTP
Consumer
Producer AMF/AMFX
Channel Set
M
A
F
H
P
T
T
11. BlazeDS Architecture
11
BlazeDS Server Architecture
Message
MessageBrokerServlet Broker Service Destination Adapter
• AMFEndPoint
R RemotingService RemotingDestination
• HTTPEndPoint O
• StreamingAMFEndPoint U JavaAdapter
HttpProxyService
T HttpProxyDestination
E
HttpProxy
MessageService
Adapter
M
S MessageDestination
G ActionScript
Adapter
Security JMS
Check point
Adapter
12. BlazeDS Configuration
12
Filename Description
services-config.xml Top level configuration file. Contains security constraints
definitions. Logging settings. Basically common configurations
across services.
remoting-config.xml Remoting service definitions, such as destination.
proxy-config.xml Proxy service definitions for working with web services, HTTP.
remoting-config.xml Messaging service for publish & subscribe
13. BlazeDS Channels & EndPoints
13
Channel set fallback mechanism
Channel Endpoint Description
AMFChannel AMFEndPoint Transport data over HTTP in binary
AMF format. Use for Remoting.
HTTPChannel HTTPEndPoint Transport data over HTTP in AMFX
format. User for Remoting.
StreamingAMFChannel StringAMFEndPoint Streams data in real time over HTTP
in binary AMF format. User for
Messaging.
StreamingHTTPChannel StreamingHTTPEndPoint Streams data over HTTP in AMFX
foramt.
14. BlazeDS Channels Behavior
14
Channel Description
Non-polling AMF & HTTP channel Good for Remoting
Piggybacking AMF & HTTP channel Enable queuing of messages along with responses to
any messages from client.
Polling AMF & HTTP channel Simple polling mechanism from client side to request
messages
Long polling AMF & HTTP channel To get pushed messages to client. Server parks the
poll request until data arrives for client.
Streaming channel Stream data in real-time. Available in HTTP 1.1 only.
Make sure servers are capable of handling large
concurrent connections
15. Remoting
15
Invoke methods of Java objects on server side
HTTP-based request/response data communication
Use AMFChannel
Advantages
AMF binary messaging protocol for exchanging data
Serialization/de-serialization are handled automatically
Natively supported by Flash Player
Gateway services make easier to expose server side
services
Why not use HTTPService or WebService?
16. Remoting
16
RemoteObject – client side component
destination– source of Remoting Service destination
result – when service call successfully returns
fault – when service call fails
concurrency – how to handle multiple calls
multiple – existing requests are not cancelled
single – one at a time, multiple requests generate fault
last – New request cancels any existing request
17. BlazeDS Architecture
17
BlazeDS Server Architecture
Message
MessageBrokerServlet Broker Service Destination Adapter
• AMFEndPoint
R RemotingService RemotingDestination
O
U JavaAdapter
T
E
M
S
G
Security
Check point
19. Remoting
19
remoting-config.xml
<destination id="tutorial-product">
<properties>
<source>tutorial.ProductDAO</source>
</properties>
</destination>
Java Class Java Class
package tutorial; package tutorial;
public class ProductDAO { public class Product {
public List<Product> getProducts() { private int productId;
// DB logic private String name;
} private double price;
private int qty;
public void updateProduct(Product prod){
// DB logic // getters and setters for
} // above member variables
} }
21. Remoting
21
Destination scope
request – server creates an instance of Java class per
request (Default)
application – one instance for entire application
session - one instance per session
<destination id="tutorial-product">
<properties>
<source>tutorial.ProductDAO</source>
<scope>request</scope>
</properties>
</destination>
22. Remoting
22
Concurrency
multiple – existing requests are not canceled
Single – one at a time
last – latest request cancels any existing request
<mx:RemoteObject id="ro" destination="tutorial-product"
showBusyCursor="true">
<mx:method name="getProducts" fault="handleFault(event)"
result="handleGetProductsResult(event)"
concurrency=“multiple”/>
</mx:RemoteObject>
24. Data Serialization
24
Serializing Between ActionScript & Java
AS Type Java
Array(dense) java.util.List
Array(sparse) java.util.Map
Boolean java.lang.Boolean
Date java.util.Date
int/uint java.lang.Integer
Object java.util.Map
Typed Object Typed Object [RemoteClass]
XML org.w3c.dom.Document
• Dense array – all numeric indexes are consecutive (no gap)
• Sparse – gap between numeric indexes
25. Remoting
25
AS Serialization Rule
Public bean properties w/ getter/setter methods
Public variables
Private properties, constants are not serialized
Client side
Use [RemoteClass(alias=“ “)] to map directly to Java object
Custom Serialization
package {
Iexternallizable interface [Bindable]
[RemoteClass(alias="tutorial.Product")]
[Transient] public class Product {
public var productId:int;
public var name:String;
public var price:Number;
public var qty:int;
}
}
26. Remoting
26
java.util.ArrayList
ActionScript ActionScript
Array java.util.TreeSet
ArrayCollection
java.util.TreeSet
public class ProductService {
public void addProduct(java.util.Set products) {}
public void addProduct(java.util.SortedSet products) {}
public void addProduct(java.util.List products) {}
}
27. Messaging
27
Publish-subscribe messaging
Among multiple Flex clients
Support bridging to JMS topics (only)
Server can push message to client on its own.
Consumer
Message
Producer
Messaging Consumer
Service
28. Messaging
28
BlazeDS Server Architecture
Message
MessageBrokerServlet Broker Service Destination Adapter
R MessageService MessageDestination
• Producer
O
• Consumer U ActionScript
Adapter
T
E
JMS
M Adapter
S
G Custom
Adapter
30. Messaging
30
Message filtering
Through message header and subtopic information
Subscriber defines the filter criteria
Send to server during the subscripting step
Message filtering is done on the server
<mx:Application>
<mx:Script>
private function sendMessage(): void { <mx:Application>
var msg = new AsyncMesage(); <mx:Script>
msg.headers = new Array(); …
msg.headers[“user”]= “John”; </mx:script>
msg.body = “Hello there”; <mx:Consumer id=“consumer”
producer.send(msg); destination=“chat”
} message=“handler(event)”
</mx:script> selector=“user = ‘John’”>
… </mx:Application>
</mx:Application>
31. Messaging
31
Filtering through subtopic
Usesubtopic property of AsyncMessage
Support single-token wildcard in subtopic string
<mx:Application>
<mx:Script>
private function sendMessage(): void {
var msg = new AsyncMesage();
msg.body = “Hello there”;
producer.subtopic = “chat.gossip”;
producer.send(msg); <mx:Application>
} <mx:Script>
</mx:script> …
<mx:Producer id=“producer” </mx:script>
destination=“chat”> <mx:Consumer id=“consumer”
… destination=“chat”
</mx:Application> message=“handler(event)”
subtopic=“chat.gossip”>
</mx:Application>
33. Messaging
33
Performance related settings
Property Description
throttle-inbound Control # of inbound messages per second
throttle-outbound Control # of outbound messages per second
message-time-to-live How long server should keep a message pending
delivery before being discarded as undeliverable
Additional settings for security
34. Messaging
34
Measuring message processing performance
Message size
Sever processing time
Network travel time
Message waiting time
Ensure clients & server clocks are synchronized
Enable at the channel level
38. Proxy
38
HTTPService
REST functionality
All HTTP method
PUT, DELETE, HEAD, POST, GET
Get response data when status code != 200
URL alias control on server side
<mx:HTTPService destination="CNNFeed" useProxy="true" id="topNewService"
resultFormat="e4x” result="handleTopNewResult(event)"/>
<!– using default proxy destination
<mx:HTTPService useProxy="true" id="searchService" resultFormat="e4x”
result="handleSearchResult(event)” />