Saturday, May 28, 2011

Adobe AIR, Java, BlazeDS and Tomcat with Eclipse

In my project, front end is created using Adobe AIR desktop application. Mainly back end is created using java and to integrate the java and AIR I have used BlazeDS technology.
In this article I am going to talk about how to create a simple application using Adobe AIR, java and BlazeDS using eclipse developing environment.

To develop the flex application using eclipse required to installed the flex plugins for the eclipse. also required to install the Adobe AIR runtime environment. you can download it from http://get.adobe.com/air/?promoid=BUIGQ

Also you need download the BlazeDS. you can download binary distribution from here http://opensource.adobe.com/wiki/display/blazeds/Release+Builds

For the blazeds communication we required to hav
e java server so I am going to have a tomcat server for my java server.

Configuration

1) extract the downloaded blaseds.war in to folder blazeds.
2) put that blazeds folder in to C:\apache-tomcat-6.0.29\webapps. in here I have my tomcat server in
C:\


let's create Hello world application for AIR, java and blazeds

I am going to create AIR project first. Here I am using Eclipse.

1). open new and navigate to 'flex project'
as the above you have to select Application type as Desktop Application and Application server type as J2EE. also required to check the check box and select LiveCycle Data Services radio button and click Next.

2). In there need to give
Root Folder by browsing to C:\apache-tomcat-6.0.29\webapps\blazeds
Root url - http://localhost:8080/blazeds

Context root - /blazeds
output folder - C:\apache-tomcat-6.0.29\weba pps\blazeds\hello-debug
after that you can create simple hello world application using AIR.



<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="473" height="249"
creationComplete="init()"
>

<mx:Script>
<![CDATA[
import mx.messaging.Channel;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.messaging.messages.IMessage;
import flash.net.registerClassAlias;
import mx.messaging.messages.RemotingMessage;

private var fileRef:FileReference;
private var file:File;

registerClassAlias("mx.messaging.messages.IMessage", IMessage);
registerClassAlias("flex.messaging.messages.RemotingMessage", RemotingMessage);

public function init():void

{
var cs:ChannelSet = new ChannelSet();
var customChannel:Channel = new AMFChannel("my-amf", "http://localhost:8080/blazeds/messagebroker/amf");
cs.addChannel(customChannel);
srv.channelSet = cs;
}
//fault of conversion
private function faultHandler(event:FaultEvent):void
{
Alert.show( ObjectUtil.toString(event.fault) );
}
//success of conversion
private function resultHandler(event:ResultEvent):void
{
Alert.show( ObjectUtil.toString(event.result) );
}

private function send():void
{
var input:String = input.text;

//send the details to java side
srv.send(input);
}
]]>
</mx:Script>
<mx:RemoteObject id="srv" destination="connector" result="resultHandler(event)" fault="faultHandler(event)"/>
<mx:Panel x="0" y="0" width="433" height="209" layout="absolute" title="Hello World">
<mx:TextInput id="input" x="64" y="68"/>
<mx:Button x="232" y="68" label="Hello" click="send()"/>
</mx:Panel>
</mx:WindowedApplication>


in the above air application get the name from the user and when user click on the hello button application send the name to java side through the blazeds.

java class can be shown as bellow,

public class hello {
public String sendData(String name){
String result="";
result = name;
return result;
}
}
it is a simple java class that take the name receiving from AIR side and send it back.
To integrate both of java and AIR we need to create another java cla
ss called connector. That can be shown as bellow,'

public class connector {
public String send(String name){
String result="";
hello ob=new hello();
result=ob.sendData(name);
return result;
}

}

after that need to run both java classes and put there class files into C:
\apache-tomcat-6.0.29\webapps\blazeds\WEB-INF\classes

after that change the remoting-config file in flex folder C:\apache-tomcat-6.0.29\webapps\blazeds\WEB-INF\flex as bellow,

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService"
>

<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>

<default-channels>
<channel ref="my-amf"/>
</default-channels>

<destination id="connector">

<properties>

<source>connector</source>

</properties>

<adapter ref="java-object"/>

</destination>

</service>

after did all the things start up the tomcat server and you can view your AIR application. Give the name as text input and click hello button and you can see the
alert box saying your name.

No comments:

Post a Comment