Invoking Java methods from Adobe Flex

Adoption to Rich Internet Applications is increasing. What about the products which were developed earlier and are not RIAs? how will they adopt RIA? It’s very simple. If your software has a architecture where the business logic is clearly separated from other layers, then you just have to start learning Adobe Flex and develop and RIA. Because Adobe Flex allows us to invoke Java objects on the server from the client side Flex application.

How to invoke Java methods from Flex application?

Add an mapping to the Java class to services-config.xml
Map your RemoteObject (provided with Flex SDK) on the client side to the destination configured in services-config.xml
Invoke the method in your Java class using the RemoteObject instance
What do you need to invoke a Java method from the client side application?

Flex Builder
Blaze DS

Creating Java class

Create the Java class with the code below and compile it. We should deploy this class in a location, where Blaze DS can find and instantiate this class. Under the folder where the Blaze DS zip file was extracted, navigate to tomcat/webapps/blazeds/WEB-INF/classes and then copy the Java class in appropriate directory structure.

RemoteServiceHandler.java

package com.adobe.remoteobjects;

import java.util.Date;

public class RemoteServiceHandler {

public RemoteServiceHandler()

{

//This is required for the Blaze DS to instantiate the class

}

public String getResults(String name)

{

String result = null;

result = “Hi ” + name + “, this is a service and the time now is : ” + new Date();

return result;

}

}

Creation of Flex application

Code to be included in the MXML file to invoke the Java class on the server is available below.

In the RemoteObject, the value of the destination property should be set to the ID of the destination, which will be added to the configuration file (Explained below).

MyApplication.mxml

This application has been configured to compile the output to the tomcat web application folder. Now that we have done developing the files required on both the client and the server. We need to configure Blaze DS.

Configuring Blaze DS

Navigate to tomcat/webapps/blazeds/WEB-INF/flex and open the file remote-config.xml. Replace the XML file content with the content below. The ID of the destination added below will be used by the components at the client side. Destination added will be map the destination name to the Java class, which has to be invoked.

remote-config.xml

com.adobe.remoteobjects.RemoteServiceHandler

Executing the application

Start the Tomcat server.

Navigate to http://localhost:8400/blazeds/MyApplication-debug/MyApplication.html in your browser. Click on the Remote Service button and then you can see the message from the Java class.

About ashokabhat

I am a C,C ,JAVA,Adobe Flex,.NET Programmer Currently working as a Software Developer
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s