Sunday, November 30, 2008

Set up blazeds on tomcat with eclipse for FLEX .

Alright,

This is a quick post on how to setup your first blazeds environment for eclipse on your existing tomcat installation.

By the end of this short tutorial you will be able to have a simple flex page that can call your remote object using blazeds on tomcat.

Note:I use eclipse 3.3 with the flex builder plugin installed. Steps will probably remain the same for flex builder with wtp installed too.

1.Download blazeds from adobe.com. Extract the blazeds.war file onto a folder.I will tell you how to use this in a few steps.

2.Create your tomcat server on eclipse. If you had not added your existing tomcat as a runtime in eclipse , this is a good time to do so.

3.Create a new "Dynamic web project " on eclipse. Call it BlazeAllinOne. Now go back to the folder where you had extracted blazeds.war and copy the META-INF and WEB-INF folders from there to the "Webcontent" folder that eclipse had created in the project. The WEB-INF should include lib,flex,classes,src folders. And most importantly the new web.xml file.

4.Right click on the project and change the build path such that the java classes will be compiled into the classes folder.RightClick->properties->java build path->Source tab.


NOtice the "Default output folder" entry that is pointing to the classes folder we copied from blazeds.war

5.Open the services-config.xml file within WEB-INF/Flex folder and change entries from this

endpoint url="http://{host}:{port}/{context-root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"

TO

endpoint url="http://localhost:8080/BlazeAllinOne/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"

Note BlazeAllinOne was how my project was deployed. Note.Do not WebContent or anythng after your project's name.

6.Build your project, if you have not set Build-Automatically on the project menu.By this point you have generic blazeds-tomcat setup that can invoke remote objects.

Verify your setup by testing if the amf links are active . Paste the endpoint url you changed on services.xml onto your browser. something like this http://localhost:8080/BlazeAllinOne/messagebroker/amf should return you a blank page and not a 404 or 500 error.

If you get 404 error,Somethings wrong!

Now some clarifications. Why did I create a dynamic web project and copy my blazeds files? Because only of its a DWP , eclipse will deploy it in tomcat container. If you had created a simple java project and pointed to your blazeds WEB-INF folder, eclpse will not deploy the app, everytime you start the server through eclipse.

7.Let us now see how we tie up flex to the server side environment we just created..

8.Create a simple java classes on your blaze-server project .Something like this

package test;

public class tester {
public String getData() {
return "hi";
}
}

9.Build the project again. The test.tester.class file should now be generated within the WEB-INF/classes folder.

10.Open your remoting-config.xml file and create a new destination pointing to the new class.
the entry goes like this

<destination id="testremote">
<properties>
<source>test.tester</source>
</properties>
</destination>

Now Lets create the client

11.NewProject-Flex-select J2ee in application server type.Unselect "Create combined flex/java src folder".Click next

12.Deselect the "Use default location for LCDS " and enter the following.

RootFolder :{youreclipseworkspace}\BlazeAllinOne\WebContent\
Root URL:http://localhost:8080/BlazeAllinOne/
Context_Root:BlazeAllinOne/. Click finish

13.Create a simple mxml and paste this code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:RemoteObject id="test" destination="testremote">

</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function getData():void {
test.addEventListener(ResultEvent.RESULT,resultHandler);
test.addEventListener(FaultEvent.FAULT,faultHandler);
test.getData();
}
private function resultHandler(eve:ResultEvent):void{
Alert.show(eve.message.body.toString());
}
private function faultHandler(eve:FaultEvent):void{
Alert.show(eve.message.toString());
}

]]>
</mx:Script>
<mx:Button label="click" click="getData()">

</mx:Button>
</mx:Application>

Note:The remote object destination I use is testremote.The one I created in remoting-config.xml file.

14.Build all and restart the server if its neccessary. RIght click on your mxml and select "Run as Flex application"

15.Click on the button. You should be getting a pleasant "hi" from your server setup :-)

16.Note that you can setup break points on flex and java and debug them parallely.

There you go. You have the power of flex,blazeds,tomcat on your hands.

Make the world a better place :-)

Hope it helps!


Bookmark and Share

28 comments:

Anonymous said...

Thank you, this has been a great help. The most straight-forward tutorial I could find

Vengu said...

Glad it was of help.:-)

Anonymous said...

Thank you indeed. So far the only tutorial I've seen that covers the setup of the Java part of a blazeds project in eclipse.

Vengu said...

Glad it was of help.:-)

Tom De Winne said...

thanks for the help! The only thing a bit unclear to me was if I had to change the secure-amf en polling-amf endpoint URLs as well...

Anonymous said...

Excellent tutorial - many thanks. This is the best Flex/BlazeDS tutorial I have seen and the only one I could get to work

Vijay Vure said...

Hi Venkatesh Vellingiri,

Excellent tutorial. This is of great help. Thank you so much.

Vengu said...

Thank you. Glad it was of help.

Jagadish said...

Thanks alot for this post

Ana_tessa said...

Hi Friends,
I am not sure if this is correct for using the flex on tomcat server but it worked for me, so thought il just share it,
1. Create a flex project
2.Chosse the application server J2EE
3.UNCHECK the remote object and click next
4.web root=(C:\apache-tomcat-6.0.18\webapps\ROOT)
5.root URL = C:\apache-tomcat-6.0.18\webapps\ROOT\flexprojctname-debug

And ur all set!!! hope this works Do let me know if this is not a effective way .

Thanks

Rick said...

For us users using different flavors of nix I suggest you change the sh-bang in the install-flex-libs to
#!/bin/sh (instead of bin/bash). Also it would be nice if you could save the .sh file with unix line breaks, as it is right now the line breaks need to converted (for vim users you can just do :set filetformat=unix on the file and save it... or use dos2unix if you have that installed.)

Unknown said...

Its been a great help. Thanks a lot for such simple tutorial

Unknown said...

Hi,

can any body explain, the advantage of blazeds.

thanks

Anonymous said...

Great ... thanks for the help

Unknown said...

Thank you very much for this tutorial! Indeed it's the only tutorial that worked for me...

Have you ever tried to create a Flex/Java/BlazeDS project by choosing "Combined Java/Flex project with WTP"?

Anonymous said...

Thanks a lot for your tuto..
I could replicate easily the server-side app (no 404) but when running the client Flex app, I get an eeror :

(mx.messaging.messages::RemotingMessage)#0
body = (Array)#1
clientId = (null)
destination = ""
headers = (Object)#2
messageId = "6C4865F5-B8CB-11C4-66F2-4C6B3ECAEA07"
operation = "getData"
source = (null)
timestamp = 0
timeToLive = 0

what could be wrong (I rebuild it 5 times without any success...)

mrnoname said...

Nice tutorial. I was a newbie to Flex. So before running these make sure the FLEX SDK is setup properly in your Eclipse environment.

Anonymous said...

I am getting the following error while running it-
(mx.messaging.messages::RemotingMessage)#0
body = (Array)#1
clientId = (null)
destination = ""
headers = (Object)#2
messageId = "33FC7741-DF80-E1E1-EB77-772F986BA0E4"
operation = "getData"
source = (null)
timestamp = 0
timeToLive = 0

Any ideas what could be going on? Do I need to have FlexSDK in Tomcat's classpath at run time?

Anonymous said...

Thanks man! This is definitely the most straightforward tutorial on how to set up your own eclipse BlazeDS server.
Cheers,
Ed.

Anonymous said...

Hi

Tks very much for post:

I like it and hope that you continue posting.

Let me show other source that may be good for community.

Source: Brand interview questions

Best rgs
David

Unknown said...

Extreeeeeemly superb dear.. Thanks alot..

Unknown said...

Extreeemly super.. thanks alot..

Asim said...

I have tried more than 10 tutorials on the web to get blazeds setup in Eclipse on tomcat and do flex remoting, and faced various issues. Your tutorial worked like a charm! Keep up the good work!!

Kaustubh Baraskar said...

thanks bro..this was of great help

Anonymous said...

Brilliant tutorial. Very useful!

Liana said...

I get 404 error. What might be wrong???

Anonymous said...

for those of you who get ERROR message instead of "hi".

Look in promt lines. You may have an exception. Exception message suggests to change http into https in configuration file. It almost tells in which line exactly you have to make change.

It worked for me.

Anonymous said...

Very nice tutorial was just what I needed. You accomplished the build environment that I was looking for and had been unsuccessful at achieving. Thank you!