How you choose to access data in your Flex application impacts performance. Because the application is cached on the browser after the first request, data access is responsible for significantly affecting performance while the application runs. Flex provides several solutions for data delivery to the client. It delivers data through runtime services that invoke Java classes loaded in the Flex classpath, or sends proxy requests to web services or HTTP servers. Figure 2 shows the available Flex data services.
Figure 2. Flex data services
The following list describes the Flex data services:
Web services proxy—Due to the Flash Player security sandbox, requests to web services can only be made to the same domain that loaded the SWF file. In many cases, web services will be located on another domain. In Flex, all web service requests go through a web services proxy running on the Flex presentation server. The following snippet shows a call to a web service:
<mx:WebService id="ws" wsdl="http://acme.com/stock.wsdl"> <mx:operation name="getQuote"/> </mx:WebService>
When the SWF file loads on the client, it sends a request to the
web services proxy requesting the WSDL file. Then, the web services
proxy makes a request to the WSDL file and sends the response back
to the client, using the proxy that the response sent back to the
original client. All client server interaction using the <mx:Webservice> tag
passes through the proxy unless you use the useProxy="false" property.
When you use useProxy="false", the Flash
client makes the web service call directly to the end service.
For this to work properly with the sandbox security in the Flash
client, the end service must have a crossdomain.xml file in place.
In some cases you have no control over the end service, so you
need the proxy. In some cases you may experience issues using HTTPS
and retrieving error messages when not using the proxy. Macromedia
recommends that you use the proxy unless you have a good reason
not to.
Remote object AMF gateway —The
remote object AMF gateway enables
you to access server-side objects (Java Beans, EJBs, POJOs) running
on the Flex presentation server. The remote object proxy uses two
different encoding mechanisms, which you choose by modifying the
encoding property in the RemoteObject tag.
Flex 1.0 provided two encoding mechanisms for transferring the data AMF (Action
Message Format) and SOAP. In Flex 1.5, SOAP encoding has been deprecated. AMF
provides a binary protocol
for data transfer between server and client. The following
code shows an example of using the RemoteObject tag
with AMF:
<mx:RemoteObject id="ro" src="samples.StockBean" encoding="AMF"> <mx:method name="getQuote"> </mx:method> </mx:RemoteObject>
HTTP services—Another data access method uses the HTTPService tag with remote URLs to load XML into Flash Player. The HTTP services proxy handles requests from the Flash client, and the proxy invokes the URL and sends the response back to the client using a proxy. The following snippet shows how to use the HTTPService tag:
<mx:HTTPService id="myRequest" url=http://acme.com/mydata.xml> </mx:HTTPService>
You can use a standards-based approach to access data from your Flex applications,
but in some cases you can improve performance with other options. Using the <mx:Webservice> tag
enables you to use a standards-based
approach but doesn't always yield the best performance. Also, there is an extra
bit of overhead for the additional XML with the SOAP encoding in comparison
to AMF. On average, AMF is three times faster than SOAP and in cases of very
large payloads can be up to six times faster. This is because AMF uses a binary
protocol that greatly reduces the size of the payload compared to XML-based
soap packets for the same data. The performance of SOAP with web services is
also dependent on your web services implementation. Different application servers
use different web service back ends, so you might see performance differences
depending on the implementation. The only way to understand how well your implementation
will perform is to load-test your services. Carrying the same data, SOAP is
usually double the size of AMF. By using AMF, you can reduce the overall bandwidth
of your applications.
The two best-performing methods of data delivery are XML using HTTPService or RemoteObject. As mentioned previously, web services may be slower due to the overhead of deserializing and serializing the SOAP packets on the server and client. Many times, the choice depends on your existing applications and how you choose to integrate with your back end server-side resources. The performance of web services can be highly dependent on your application server's underlying implementation of the web services engine, so you should load-test to see how well it performs.
You also have the option to use
remote classes for more complex objects with the <mx:RemoteObject> tag. Always consider the overhead of
serializing and deserializing remote classes and use them only in cases where
they are needed.
When using remote classes, make sure that you use the Object.registerClass() method
to specify the fully-qualified name of the corresponding Java class on the
server. By mapping the ActionScript class directly to its Java server-side
equivalent, you can prevent the AMF gateway from having to search for the equivalent.
Find more information on Object.registerClass() in the Developing
Flex Applications document.
Serialization for list-based objects will occur faster in
the client.
You also have the option of bypassing the Webservice
and HTTPService proxies in Flex, allowing you to invoke the services directly. If
the service you are trying to invoke is on the same host or shares the same
domain name as the location of the MXML and SWF files, you can invoke the
service directly by adding the useProxy="false" property.
When you specify useProxy="false", the Flash client
no longer routes calls through the Flex proxy; instead it routes it directly
to the end service. If the MXML document and service are served from different
domains or hosts, then you must place a crossdomain.xml file on the server
that is hosting the service. Read more about this procedure in the Flash
7 security article. Macromedia recommends that you develop using the proxy
because it aids in debugging and that you only turn off the proxy if you're
confident that performance requires it during production. The proxy also handles
authentication and authorization. With it, you can use named services instead
of embedding the URLs in the MXML files; it also resolves conflicts by handling
same-named cookies from different domains.