Invoking Java classes on a server is a very useful feature provided by the BlazeDS. When a method from a Java class is invoked from a Flex application on the client, Blaze DS converts automatically converts the Java data types to suitable Action Script (AS) data types
For Java objects that BlazeDS does not handle implicitly, values found in public bean properties with get/set methods and public variables are sent to the client as properties on an Object. Private properties, constants, static properties, and read-only properties, and so on, are not serialized. For ActionScript objects, public properties defined with the get/set accessors and public variables are sent to the server.
BlazeDS uses the standard Java class, java.beans.Introspector, to get property descriptors for a Java bean class. It also uses reflection to gather public fields on a class. It uses bean properties in preference to fields. The Java and Action-Script property names should match. Native Flash Player code determines how ActionScript classes are introspected on the client.
We can map an Action Script class to a Java class on the server. Once mapped the objects are casted to respective types on the client and the server. We use [RemoteClass] metadata tag to map Action Script class to the Java class.
For this we will create a Person.java class and an Person.as class and map both. Once mapped we will invoke a method from a Java class which will return a object of the type Person.java on the server using RemoteObject component. Once the object is returned, we will use the returned object as object of the type Person.as
We need BlazeDS setup and running to execute this sample. Please follow the steps in http://sujitreddyg.wordpress.com/2008/01/14/invoking-java-methods-from-adobe-flex/ to set up the BlazeDS and setting up a Flex application, which is mapped to BlazeDS root directory. Use the same link to configure a destination on BlazeDS to map to a Java Class.
Creating Java classes
RemoteServicehandler.java
This class will be invoked from the Flex application. Once the getPerson() method is invoked, we will return the Person object.
package com.adobe.remoteobjects;
import java.util.Date;
import com.adobe.objects.Person;
public class RemoteServiceHandler {
public RemoteServiceHandler()
{
}
public Person getPerson()
{
Person person = new Person();
person.id = 1;
person.dateOfBirth = new Date();
person.name = “Sujit”;
person.company = “Adobe”;
return person;
}
}
Person.java
package com.adobe.objects;
import java.util.Date;
public class Person {
public int id;
public String name;
public Date dateOfBirth;
public String company;
}
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.
Creating Flex application
RPC.mxml
Please do have a reference to the Person.as in your mxml file, otherwise Person.as will not be linked to the SWF file created and will not be available on run time.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical”>
<mx:Script>
<![CDATA[
import objects.Person;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function displayPersonDetails(event:ResultEvent):void
{
var person:Person = Person(event.result);
Alert.show(person.name + ", " + person.dateOfBirth.toDateString() + ", " + person.id);
}
]]>
</mx:Script>
<mx:RemoteObject id=”remObj”
destination=”CreatingRpc”
result=”displayPersonDetails(event)”
fault=”Alert.show(event.fault.faultString);”
/>
<mx:Button label=”Get Person” click=”remObj.getPerson();” />
</mx:Application>
Person.as
Please observe the [RemoteObject] metadata tag. Using this tag we map this class to the Java class on the remote location. The Java class should be in the classpath, recognizable by the BlazeDS. The name should be the fully qualified name.
package objects
{
[RemoteClass(alias="com.adobe.objects.Person")]
public class Person
{
public var id:int;
public var name:String;
public var dateOfBirth:Date;
}
}
That is all we need to do.
Posted by Sujit Reddy G 
