Sending ActionScript Object To Java

There is a lot of source about how to communicate between ActionScript and Java via BlazeDs, but almost all of them unfortunately are classic scenarios.  Of course in development we always don’t work with the ideal classic ways, we need different solutions for different problems. One problem I was face to couple months ago sending ActionScript class from ActionScript to Java using RemoteObject, which is actually quite simple but I couldn’t find any example, so I thought it will be good to write about it now.


I will only show the code of small example over here, the idea is sending a sending a ActionScript class values to Java, first I will create a class called Book;

package{
        public class Book       {
                public var isdn:String;
                public var authorName:String;
                public var bookName:String;
                public var bookID:int;
                public function Book(){
                }
        }
}

And than I will create a RemoteObject with the destination string bookshop;

var bookRemoteObj:RemoteObject = new RemoteObject("bookshop");
bookRemoteObj.addEventListener(ResultEvent.RESULT, bookResultEvent);
bookRemoteObj.addEventListener(FaultEvent.FAULT, bookFaultEvent);

private function bookResultEvent(e:ResultEvent):void{
        trace(e.message);
}
                       
private function  bookFaultEvent(e:FaultEvent):void{
        trace(e.message);
}

Next I will create a new Book Object and store information that I need, and than I will use the bookRemoteObj and send it to Java function called recordBook()

var book:Book = new Book();
book.authorName = "Orhan Pamuk";
book.bookName = "My Name is Red";
book.bookID = 1576;
book.isdn = "978-0375706851";
bookRemoteObj.recordBook(book);

And than I will create a RemoteObject destination including id bookshop in remoting-config.xml, and as a source I will create a Java class called BookShopInfoBridge

<destination id="bookshop">
        <properties>
                <source>com.enginyoyen.sample.bookshop.BookShopInfoBridge</source>
                <scope>application</scope>
        </properties>
</destination>

Next is creating Java Class, in Java Class I will create a function called recordBook() which except ASObject as a parameter. ASObject in Java will represent a Actionscript Object. This will be the parameter that I want to send from ActionScript. And this is pretty much all, next all you have to do is reach the variables in the Book class, in Java for accessing those information’s, we need to use the get() method and in get() method we have specified which variable we want to get it. So we have to give the variable name as a String to get() method for retrieving the value. If you try this example you should see the output in your tomcat server log, or if you are using the WTP(Web tools platform) with your eclipse you can see the results from Console view.

package com.enginyoyen.sample.bookshop;
import flex.messaging.io.amf.ASObject;

public class BookShopInfoBridge {
        public void recordBook(ASObject bookInfo){
                System.out.println(bookInfo.get("isdn"));
                System.out.println(bookInfo.get("authorName"));
                System.out.println(bookInfo.get("bookName"));
                System.out.println(bookInfo.get("bookID"));
               
        }
}

One last thing, in case if you need certain data type you have to convert to types from ASObject get() method to what you need. For instance if you want to use the bookID as and Integer in Java you can use it as follows;

int bookID = (Integer)bookInfo.get("bookID");

That’s all you need it.
Take Care
Engin Yöyen!

7 Responses to “Sending ActionScript Object To Java”

  1. Aran 16 November 2009 at 03:39 #

    Your example seems pretty “classic” to me. Sending a VO through RemoteObject is the normal way Flex devs communicate over AMF.

    The only thing I see which makes you example different is that you are doing some weird typing on your Java class, and instead of passing a native Book VO class, you are typing it as an anonymous ASObject and having to get properties / convert it back into a useful VO.

    You should have a look at how to explicitly map Flash and Java Objects (which is what AMF is designed to do)

    http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=serialize_data_3.html

    specifically the [RemoteClass(alias=" ")] metadata tag on the Flex side.

    You should not need to use ASObject except for when your Java service requires a Map in its interface.

    Am I missing something (else) which makes you example different?

  2. Engin Yöyen 16 November 2009 at 08:58 #

    @Aran
    Mapping classes are perfect, but the problem comes when you need to send more than one Object. Sending multiple objects, along with other informations you cannot use mapping.
    Of course you can make an Array in class to but that doesn’t mean it will give you the perfect control that you need.

    On the other hand examples can be build around Class Mapping but you will have problem when you want to use singleton classes. And this was the problem that I had, so solution I found was sending objects in array to Java, and in Java I could use to ASObject to get the variables.

  3. Aran 17 November 2009 at 05:53 #

    But you can make your VO’s as nested/complex as you need. You can have a parent VO which has a child property which is actaully another VO class.

    (or as you say, send back an array of the required VO classes - the Array could also be a property on your parent VO)

    Not that you would ever want to, but you could have hibernate serialze your entire data model into 1 object and send it over AMF!

    I am not sure what more control you’d ever want?

    In regards to singletons, this can be acomplished with the destination scope on the Blaze DS config.

    You can set a level of request (default, once per request), session (one instance per user session), or application (one instance for entire application / all users)

    http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=rpc_remoteobject_3.html

  4. Engin Yöyen 19 November 2009 at 09:36 #

    @Aran
    Hi Aran, of course by default you can do all those things, mapping and so on. But it is or sure that there will be time that creating class would save you any time, or make your code better so you have to send your object to server side at it is.

    Multiple objects in array, without having class for all objects you are sending.

    At the end, there are more so many different operations, for deleting information from database you only need the table id, for updating you only need to send which field you want to update and the ID, for inserting you need more.

    In some point it will make sense the send the raw object, which will help to reduce the amount of data that you are sending. And of course is not a way to do it for a one piece of information, but if you have to update, insert and delete, hundreds of information at a time, I think you can save a lot of time and make operation faster.

  5. Shweta 17 December 2009 at 14:00 #

    Hi Engin,
    ASObject is not working in Java its giving error that package flex.messaging.io.amf.ASObject does not exist

  6. Engin Yöyen 24 December 2009 at 10:53 #

    @Shweta
    flex.messaging.io.amf.ASObject class is in the flex-messaging-core.jar archive file. You have to attach librarys for using those classes. You can find those library in blaze-ds server files. All the archive files are under
    [BlazeDSFolder]\tomcat\webapps\blazeds\WEB-INF\lib

    In case if you dont want to deal with classes and archives, you can install WTP(Web tools project). After you install WTP all you have to do is create a new Flex project, and your eclipse will prepare all the neccessary files and sstructures for you.

    Here is the link that show how you can use and install WTP;

    http://corlan.org/2008/06/05/creating-a-combined-flexjava-project-in-flex-builder-wo-lcdsblazeds/

  7. Alan Messias 18 March 2010 at 13:20 #

    It was very usefull.


Leave a Reply