Archives for categorie "BlazeDS"

Integration example of Spring and BlazeDS with Maven2.

Multiproject EAR:
- Flex 4
- Spring 3
- BlazeDS 3




  • Share/Bookmark

Flex project example with BlazeDS and Eclipse.
There are many tutorials on Internet very simple:

  • Create a Flex project with J2EE nature
  • Import BlazeDS war to recreate project structure
  • Configure flex remoting config files
  • Create java services and register them in the config files
  • Create RemoteObject components in Flex client pointing to these services
  • Share/Bookmark

Amf3Output class included in BlazeDS libraries allow us a simple way to serialize objects from server to client in AMF3 format.

  • Share/Bookmark

Vote at your favorite country.
¡Choose with your vote the most favorite country in the world!
- Click in the mark of the country.
- Add points to your favorite country to improve it in the ranking.
- Remove points to the other countries.

Technology:
- Flex-Cairngorm
- J2EE-Blazeds-Spring-Hibernate
- MySQL

Go to he game

  • Share/Bookmark

URLLoader class allow to interchange data with the server as text, variables encoded and binary but it donesn’t allow to use encoding options like others channels as RemoteObject in URL access. Nevertheless, it’s possible to use Java serialization capabilities embedding objects (writeObject) in binary format to deserializate it on the client side with Flex (readObject). This operation can be done as following:

  1. URLLoader object send request by GET/POST to the server waiting for a binary format response.
  2. Servlet (HttpServlet) get request and its parameters.
  3. Build the object to send.
  4. Use Amf3Output class from Flex Messaging libraries given with BlazeDS to convert the object in binary format AMF and write it in the response.
  5. Flex client get the response as ByteArray and deserialize it with the readObject() method.

Here are client and server codes of the process. (BlazeDS libraries must be included in the server project):

SerializeServlet.java

package com.servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;

@SuppressWarnings("serial")
public class SerializeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        processRequest(req, res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        processRequest(req, res);
    }

    @SuppressWarnings("unchecked")
    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            List object = new ArrayList();
            object.add("Mercury");
            object.add("Venus");
            object.add("Earth");
            object.add("Mars");
            object.add("Jupiter");
            object.add("Saturn");
            object.add("Uranus");
            object.add("Neptune");
            object.add("Pluto");

             byte[] data = getBytesAMF(object);
             ServletOutputStream out = response.getOutputStream();
             out.write(data);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static byte[] getBytesAMF(Object obj) throws java.io.IOException {
        SerializationContext context = SerializationContext.getSerializationContext();
        Amf3Output amf3Output = new Amf3Output(context);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        amf3Output.setOutputStream(bos);
        amf3Output.writeObject(obj);
        amf3Output.flush();
        amf3Output.close();
        byte[] data = bos.toByteArray();
        return data;
    }

    public static byte[] getBytes(Object obj) throws java.io.IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
        bos.close();
        byte[] data = bos.toByteArray();
        return data;
    }
}

MySerialize.mxlm

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	creationComplete="onCreationComplete()">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.rpc.events.ResultEvent;

			[Bindable]
			private var myPlanet:ArrayCollection;

			private function onCreationComplete():void {
				mySend();
			}

			private function mySend():void {
				var loader:URLLoader=new URLLoader();
				loader.dataFormat=URLLoaderDataFormat.BINARY;
				loader.addEventListener(Event.COMPLETE,onComplete);
				loader.load(new URLRequest('http://localhost:8400/DynamicWeb/serialize'));
			}

			private function onComplete(event:Event):void {
				var data : ByteArray = ByteArray(event.target.data);
				var obj : Object = data.readObject();
				myPlanet=ArrayCollection(obj);
			}
		]]>
	</mx:Script>
	<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
		<mx:List dataProvider="{myPlanet}" width="200"/>
	</mx:VBox>
</mx:Application>
  • Share/Bookmark
Creative Commons License
This blog is under Creative Commons licence, unless indicated otherwise.
Special thanks to Mark James for the icon set used in this blog.