blog.ashier.com flash / flex / air development
rss rss comment

Here's how I managed to setup BlazeDS on my windows local machine.

Steps:

1. Download the latest BlazeDS.
2. Extract it.
3. Download the latest Java Development Kit (JDK), Install it.
4. Set JAVA_HOME environment variable.

Setting up Environment Variable in Windows

1. Open Control Panel
2. Click the System icon then a window will pop up
3. Go to the Advanced tab
4. Click the "Environment Variables"
5. Select "New" in System Variables.
6. In the Variable Name textbox type JAVA_HOME
7. In the Variable Location textbox type the JDK directory

you should be able seeing this:

jdk.jpg

8. Click Ok.
9. Click Ok.

4. You're done installing BlazeDS.

Running Installed Sample Applications.

1. Start Tomcat by double clicking [blazeds directory]tomcat\bin\startup.bat
2. Now, check if it works by running http://localhost:8400/ in your browser.

You should be able to see this in your browser

welcome.jpg

3. Explore!

Configure BlazeDS

1. Open [blazeds directory]tomcat\webapps\
2. Copy the blazeds folder and name it "hello". You can name it to whatever is appropriate for your project.

Note: We will be creating our HelloWorld application inside a hello folder, if you want it in root then just copy the contents of the blazeds folder in [blazeds directory]tomcat\webapps\ROOT folder.

3. Open "hello" folder.
4. You should be able to see 2 folders. WEB-INF and META-INF.
5. Inside WEB-INF, open web.xml

XML:
  1. <web-app>
  2.  
  3.     <display-name>Hello</display-name>
  4.     <description>HelloWorld Application</description>
  5.  
  6.     <!-- Http Flex Session attribute and binding listener support -->
  7.     <listener>
  8.         <listener-class>flex.messaging.HttpFlexSession</listener-class>
  9.     </listener>
  10.  
  11.     <!-- MessageBroker Servlet -->
  12.     <servlet>
  13.         <servlet-name>MessageBrokerServlet</servlet-name>
  14.         <display-name>MessageBrokerServlet</display-name>
  15.         <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
  16.         <init-param>
  17.             <param-name>services.configuration.file</param-name>
  18.             <param-value>/WEB-INF/flex/services-config.xml</param-value>
  19.        </init-param>
  20.         <load-on-startup>1</load-on-startup>
  21.     </servlet>
  22.  
  23.     <servlet-mapping>
  24.         <servlet-name>MessageBrokerServlet</servlet-name>
  25.         <url-pattern>/messagebroker/*</url-pattern>
  26.     </servlet-mapping>
  27.  
  28.     <welcome-file-list>
  29.         <welcome-file>index.html</welcome-file>
  30.         <welcome-file>index.htm</welcome-file>
  31.     </welcome-file-list>
  32.  
  33. </web-app>

- Modify display-name to "Hello"
- Modify description to "HelloWorld Application"

6. Inside the flex folder, open remoting-config.xml and add the ff:

XML:
  1. <destination id="Hello">
  2.         <properties>
  3.             <source>hello.Hello</source>
  4.         </properties>
  5.     </destination>

Note: The destination and the source values are what we'll be using in flex when we create the RemoteObject tag.

remoting-config.xml should match this

XML:
  1. <service id="remoting-service"
  2.     class="flex.messaging.services.RemotingService">
  3.  
  4.     <adapters>
  5.         <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
  6.     </adapters>
  7.    
  8.     <destination id="Hello">
  9.         <properties>
  10.             <source>hello.Hello</source>
  11.         </properties>
  12.     </destination>
  13.  
  14.     <default-channels>
  15.         <channel ref="my-amf"/>
  16.     </default-channels>
  17.  
  18. </service>

7. Save the file.
8. Modify services-config.xml to match this.

XML:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <services-config>
  3.  
  4.     <services>
  5.         <service-include file-path="remoting-config.xml" />
  6.     </services>
  7.  
  8.     <security>
  9.         <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
  10.     </security>
  11.  
  12.     <channels>
  13.  
  14.         <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
  15.             <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
  16.         </channel-definition>
  17.  
  18.     </channels>
  19.  
  20.     <logging>
  21.         <target class="flex.messaging.log.ConsoleTarget" level="Error">
  22.             <properties>
  23.                 <prefix>[BlazeDS] </prefix>
  24.                 <includeDate>false</includeDate>
  25.                 <includeTime>false</includeTime>
  26.                 <includeLevel>false</includeLevel>
  27.                 <includeCategory>false</includeCategory>
  28.             </properties>
  29.             <filters>
  30.                 <pattern>Endpoint.*</pattern>
  31.                 <pattern>Service.*</pattern>
  32.                 <pattern>Configuration</pattern>
  33.             </filters>
  34.         </target>
  35.     </logging>
  36.  
  37.     <system>
  38.         <redeploy>
  39.             <enabled>false</enabled>
  40.         </redeploy>
  41.     </system>
  42.  
  43. </services-config>

9. Delete messaging-config.xml and proxy-config.xml because you won't be needing it for now.

Creating the Hello Class in Eclipse

1. Create your project.
2. Create a package hello.
3 Inside your Package create a Hello Class. Your Hello.java should look like this:

JAVA:
  1. package hello;
  2.  
  3. public class Hello {
  4.    
  5.     public String sayHello() {
  6.         return "Hello World";
  7.     }
  8.    
  9. }

4. Build.
5. Go to your project directory and open the bin folder.
6. Copy "hello" folder and paste it inside [blazeds directory]tomcat\webapps\hello\WEB-INF\classes\
7. In your browser, open http://localhost:8400/ds-console/ click on "Generic Administration View" tab you should be able to see this:

hello.jpg
If its not there, try restarting tomcat.

8. That's it. You're done with your Hello Service.

Our endpoint / gateway for this will be http://localhost:8400/hello/messagebroker/amf

A Flex Example

1. Create a new Flex Project
2. In your main application copy this:

XML:
  1. <mx:Application
  2.     xmlns:mx="http://www.adobe.com/2006/mxml"
  3.     layout="absolute">
  4.    
  5.     <mx:RemoteObject id="blazeService" fault="faultHandler(event)" source="hello.Hello" destination="Hello">
  6.         <mx:method name="sayHello" result="resultHandler(event)" />
  7.     </mx:RemoteObject>
  8.  
  9.     <mx:Script>
  10.    
  11.         <![CDATA[
  12.             import mx.rpc.events.ResultEvent;
  13.             import mx.rpc.events.FaultEvent;
  14.            
  15.             private function faultHandler(fault:FaultEvent):void {
  16.                 result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail;
  17.             }
  18.  
  19.             private function resultHandler(evt:ResultEvent):void {
  20.                 result_text.text = evt.message.body.toString();
  21.             }
  22.         ]]>
  23.     </mx:Script>
  24.  
  25.     <mx:Button x="10" y="132" label="sayHello" width="79" click="blazeService.getOperation('sayHello').send();"/>
  26.     <mx:TextArea x="10" y="27" width="319" height="100" id="result_text"/>
  27.     <mx:Label x="10" y="10" text="Result:"/>
  28.    
  29. </mx:Application>

3. Right click your project, click Properties.
4. Click Flex compiler and add this to "Additional compiler arguments"

-services [blazeds dir]\tomcat\webapps\hello\WEB-INF\flex\services-config.xml -context-root /hello

5. Build
6. In "bin-debug" directory of your workspace, copy all files and paste it inside [blazeds dir]\tomcat\webapps\hello
7. Test it by running http://localhost:8400/hello/[filename.html]
8. You should be able to see this:

flex.jpg

That's all of it. Ciao!

Tags: , , ,

36 Responses to “BlazeDS Remoting - A HelloWorld example”

  1. Wade Arnold UNITED STATES Says:

    Thanks for writing this. I was having issues with the endpoints and this cleared it up!

    Wade Arnold

  2. » BlazeDS: Worth figuring out! HelloWorld Example > Wade Arnold Says:

    [...] like it. I took me about six hours to figure out tomcat, java in eclipse, and the whole server side endpoint thing. From there it has only taken me about an hour to figure out class mapping, authentication, [...]

  3. Ashier de Leon Says:

    No problem.

  4. Steve Levy UNITED STATES Says:

    Another Thank you for providing this example.

    Worked on the first try and really provided an excellent understanding of how to set up a remote invocation.

    Steve Levy

  5. Prakasam Says:

    Hi thanks for this example, i have try this but i’ve problem in “Additional compiler arguments” when specify following in text box it shows “no default arguments are expected” with error mark
    in the Additional complier arguments is “-services C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\hello\WEB-INF\flex\services-config.xml -context-root /hello”
    please help to over come this issue
    Thanks and Regrads
    Prakasam

  6. Ashier de Leon Says:

    try adding double quotes to your -service argument. -services “C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\hello\WEB-INF\flex\services-config.xml” -context-root /hello

    see if it works.

  7. Prakasam INDIA Says:

    code:
    Hi
    i’ve run the helloworld program, there is no problem when i click sayHello button it throws following error in textarea.
    InvokeFailed

    Message:
    [MessagingError message='Destination 'Hello' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']

    Detail:
    Couldn’t establish a connection to ‘Hello’

  8. Ashier de Leon Says:

    Have you tried adding the correct destination in your remote-config.xml? Also check that the source tag and your class package is the same, and if you happen to have installed the ds-console, you can verify it if it shows inside the Generic Administration View tab.

  9. Robert O'Toole Says:

    Thanks. It all makes sense now! Here’s what I have done so far:

    http://blogs.warwick.ac.uk/rbotoole/entry/using_blazeds_to/

  10. Ashier de Leon Says:

    np… thanks for the acknowledgment.

  11. YM Says:

    I have tried the example. But I received [object AsyncToken] instead of Hello World.
    What problem could be with my test Drive? Please Help

    Regard
    YM

  12. YM Says:

    After trying again and again by trail and error check I was able to get this exception.

    [RPC Fault faultString="Cannot invoke method 'sayHello'." faultCode="Server.ResourceUnavailable" faultDetail="Method 'sayHello' not found."]

    How to handle This?
    Regards
    YM

  13. Ashier de Leon Says:

    Hi, sorry I wasn’t able to answer back immediately. Are you still having that problem?

  14. adharsh INDIA Says:

    I have done a sample. when i right click the project and click properties i couldn’t see the flex compiler option. how to get flex compiler option in eclipse.

  15. Ashier de Leon Says:

    Select your project and click Project -> Properties. From there a Dialog window should launch. At the left side, your should be able to see “Flex Compiler”. Click “Flex Compiler” then you should be able to see “additional compiler arguments:” and an Input Box below it.

    Tell me if that works.

  16. adharsh INDIA Says:

    Thanks for your quick reply

    i have done the same as you mentioned. I click the properties. It display a dialog window. In the left side of the window there are lot of options like

    Info
    Apache XMLBeans
    BeanInfo Path
    Builders
    J2EE Module Dependancies
    Java Build Path
    Java Code Style
    Java Compiler
    Javadoc Location


    But there is no Flex Compiler in that. What i do for getting that option?

    Thanks

  17. Ashier de Leon Says:

    You’ll have to add the Flex Compiler options in your project in Flex Builder. Are you using the standalone or the plug-in?

  18. adharsh INDIA Says:

    Thanks in advance

    i have flex sdk and eclipse 3.2 and BlazeDS server.

    i don’t have the flex builder.

    i have done the sample application using commandline

    Ex :
    C:\flexsdk\bin>mxmlc sample.mxml

    but i don’t know how to run the flex data services such as webservice,httpservice,remoteobject

    is it possible to compile flex data services with out using flex builder.

    also some of the friends refered “fds express” is useful for running flex with data services.

    but where i can get.

    feel free to clarify my doubts

    Thanks.

  19. Ashier de Leon Says:

    I’m not sure if that’s possible…

    But try this if it works….
    C:\flexsdk\bin>mxmlc sample.mxml -services [blazeds dir]\tomcat\webapps\hello\WEB-INF\flex\services-config.xml

    Also, there is another way for you to be able to bypass adding compiler options. You may look at my post at http://blog.ashier.com/2008/03/06/wrapper-class-for-a-remoteobject/

    But that’s is only for RemoteObject.

    Hope this helps…

  20. adharsh INDIA Says:

    Thanks for your reply

    i compiled the mxml file using -services option and i got an swf file. when i run the swf file in tomcat server and try to call web service. i got the alert message ‘Send Failed’.

    Any suggestion?

    Thanks

  21. Ashier de Leon Says:

    It would be hard to say what the problem is based on just the alert message you got.

  22. adharsh INDIA Says:

    i compiled an mxml file using -services option and i got swf file. when i run that file in tomcat server and try to call web service today i got a new error.

    Error: Cannot assign operations into an RPC Service (deatination)
    at mx.rpc::AbstractService/http://www.adobe.com/2006/actionscript/flash/proxy::setProperty()
    at FlexWS33/_FlexWS33_WebService1_i()
    at FlexWS33()
    at _FlexWS33_mx_managers_SystemManager/create()
    at mx.managers::SystemManager/initializeTopLevelWindow()
    at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()

    feel free to clarify my doubts

    Thanks

  23. dayg PHILIPPINES Says:

    Great tutorial. Salamat Ashier!

  24. flex developers BELARUS Says:

    hey

    Really nice tips!

    will try to keep it in my mind

    thanks!

  25. seo consultant BELARUS Says:

    Hello

    nice steps mentioned

    thanks for sharing!

  26. Greg UNITED STATES Says:

    Just wanted to add another thank you. I spent many hours trying to get a simple example to work with no success. I posted on the Adobe Flex forum and got no response. I really glad I finally managed to find your blog. I had my example working within minutes. This was a HUGE help.

  27. Daniel Craig UNITED KINGDOM Says:

    Hello, I was looking around for a while searching for endpoint security and I happened upon this site and your post regarding BlazeDS Remoting - A HelloWorld example, I will definitely this to my endpoint security bookmarks!

  28. Jean-Philippe Valois Says:

    Hello,

    Just a quick word to say thanks for your tutorial. It worked on the first try for me.

    Jean-Philippe Valois

  29. Prabhu Says:

    Really Great and Nice Tutorial. Please, publish more tutorials like this.It’s very clear to understand.

  30. Tristan UNITED KINGDOM Says:

    Thank you very much for this tutorial.

    Can I suggest that you change some the class, package, and destination name from “hello” or “Hello” to something more varied so it’s easier to understand.

  31. Lowell B UNITED STATES Says:

    Ah! Many thanks for this - I’ve been struggling with this all week.

    great tutorial well explained and thorough. Thanks again

  32. jose Says:

    Hello, I did another tutorial that is very similar to yours and I got the error:

    [RPC Fault faultString="Cannot create class of type 'flex.samples.spring.mortgage.Mortgage'." faultCode="Server.ResourceUnavailable" faultDetail="Type 'flex.samples.spring.mortgage.Mortgage' not found."]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:216]
    at mx.rpc::Responder/fault()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\Responder.as:49]
    at mx.rpc::AsyncRequest/fault()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:103]
    at NetConnectionMessageResponder/statusHandler()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\channels\NetConnectionChannel.as:523]
    at mx.messaging::MessageResponder/status()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\messaging\MessageResponder.as:222]

    I can’t understand what’s happening because I’ve got my mortatge class in web-inf/samples/spring/mortgage/Mortgage.class. Why is the flex application unable to find it on the server?

    I’ve also checked that the messagebroker servlets works correctly. I just don’t now what the problem can be! Your tutorial looks very nice, it’s a good job :)

    thank in advance, any help would be apreciate

  33. jose Says:

    by the way, my .mxml looks like this, it is very simple:

  34. jose Says:

  35. jose Says:

    mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”

    mx:RemoteObject id=”ro” destination=”mortgageService”/
    mx:TextInput id=”amount”/
    mx:Button label=”Calculate” lick=”ro.calculate(Number(amount.text))”/
    mx:TextInput id=”monthlyPayment” text=”{ro.calculate.lastResult}”/

    /mx:Application

    i took out the because i didn’t apear in the forum

  36. jose Says:

    hello, I tried your example also and I get the same error:

    code:
    Server.ResourceUnavailable

    Message:
    Cannot create class of type ‘hello.Hello’.

    Detail:
    Type ‘hello.Hello’ not found.

    any ideas of what can cause de problem?

Leave a Reply