Archive

Posts Tagged ‘Remote object’

Integrate flex application with Java

May 18, 2009 1 comment

Why do we need to integrate ?

Flex application is meant for presentation tier where the user will input the data but it could not directly access and store the data in database. As well as it does not have much functionality as we have server side technologies like j2ee, .net etc. I can tell you the example, we don’t have file handling mechanism in flex which is a big blow if you want to develop web application with flex alone. Mainly if you want to develop web application we should use both client and server side technology, here flex plays client role and it needs help from the server side technology to develop complete web application.

How is it possible ?

In flex, we do have service named remoting service which allows flex application to communicate with server side Java classes using BlazeDS server and we can implement remoting service in flex with the help of RemoteObject component which is one of the three components in Remote Procedure Call (RPC) components and other two are HttpService & WebService components.

now its time to get in to the action !!!. To access the methods of server side Java classes from flex application we have to do the following task.

  • Create RemoteObject component
  • Create, compile & deploy Java class
  • Configure server side settings.

Remote Object component

  • Set up your web application in Tomcat and integrate your flex application with your web application in Tomcat. If you dont know how to do this please go through to my previous post click here.
  • Create remote object instance in your MXML application file, name the instance and define the event handler function name for result and fault property.
  • The most important thing is dont forget to define the destination for your RemoteObject instance because using destination alone we map the flex application request to the corresponding Java class.

<mx:RemoteObject id="{your_instance_ name}" destination = "{your_destination_name}" result="{your_result_eventHandler_name}" fault="your_result_eventHandler_name}"/
>
  • We have to define the event handler function name for result and fault because if our remote object component successfully accessed the method then the event handler function for result will be executed and where we execute some business logic with the result. Similarly if it is not executed successfully,event handler function for fault will be executed.
  • Define event handler function function for result and fault inside <mx:script> tag
  • Create simple user interface to call and show the return string of method in server side java classes.

Server side Java class

  • Create a java class and define a method which should return string when it is called.
  • Compile the java class and copy the .class file of your java class.
  • Paste that file in to {Tomcat_home_directory}\webapps\{Web_application_name}\WEB-INF\classes folder of your web application.

Configure server side settings

  • Go in to {Tomcat_home_directory}\webapps\{Web_application_name}\WEB-INF\flex folder of your web application.
  • Open the Remoting-config.xml file in notepad.
  • Before service tag, map your remote object destination to corresponding java class.

<destination id="{your_remote_object_destination}">
<properties>
<source>{fully_qualified_class_name}</source>
</properties>
</destination>

Finally invoke your server side java method with remote object instance i.e. remoteObject_instanceName.javaClass_Method(), it will request the BlazeDS server, BlazeDS will interpret and forward the request to corresponding java class. Once the method has been executed successfully, BlazeDS will forward the response to the requested remote object instance and the event handler function defined for result will be executed.