Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


 

Resin Documentation

home company blog wiki docs 
app server web server 
health cloud java ee pro 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

hessian actionscript implementation for adobe flash


This document describes the ActionScript implementation of Hessian. Usage instructions, technical reference, and MXML instructions are given.

Download

Caucho Technology has released this Hessian implementation under an open source license (the Apache license). Anyone may freely download, use, and redistribute the Hessian implementation.

Hessian Download
BINARYSOURCEDATE
hessian-flash-4.0.0.swc (42k)
hessian-flex-4.0.0.swc (216k)
bam-flash-3.2.0.swc (43k)
hessian-flex-4.0.0-src.jar (68k) 2009/06/26

Hessian API Reference

Hessian MXML Hello World

We will look at two versions of a Hessian Hello World program, one written in MXML and one with more programmatic elements in ActionScript. (These examples can be compiled by adding the above Hessian SWC files to your library path.) The Hessian service itself will be described at the end of this document for reference. First we will look at the MXML version of the client application.

This application has a text input and a label for the result received from the server. The text from the input is sent to the server, which simply echos it back with a "Hello" greeting. The source for the application is below.

MXML Hello World source
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:word="*">

  <hessian:HessianService xmlns:hessian="hessian.mxml.*" 
    id="service" destination="hello"/>

  <mx:Panel 
    title="Caucho Hessian Hello World"
    paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">

    <mx:HBox>
      <mx:Label text='Whom do you want to say "Hello" to?'/>

      <mx:TextInput 
        id="who" 
        maxChars="20" 
        enter="service.hello.send(who.text)"/>

      <mx:Button label="Say Hello" click="service.hello.send(who.text)"/>
    </mx:HBox>

    <mx:Label text='The server said: "{service.hello.lastResult}"'/>

  </mx:Panel>    
</mx:Application>
  

Most of the above code is standard MXML formatting code. The important parts to note are the <HessianService> tag, the actions given to the TextInput and Button, and the final Label's text. The <HessianService> tag sets up a service instance which we can use to call methods on a remote Hessian service. The destination attribute specifies the URL of the service. In this case, the URL is relative, so the application will contact http://<origin server>/<swf url>/hello as the service.

The actions on the TextInput and Button are the same: they invoke the hello method on the remote Hessian service using the text from the TextInput. Note that it is not necessary to specify remote methods before using them. If the method is incorrectly called, the Hessian service will return an error.

Finally, the text attribute of the final Label uses the value service.hello.lastResult. This is a bindable value that updates automatically when the service call returns.

Hessian ActionScript Hello World

Next we will look at a more programmatic way to interact with Hessian services from ActionScript. We retain the same MXML layout, but now we perform all of the communication with embedded ActionScript code.

ActionScript Hello World source
<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:word="*"
  implements="mx.rpc.IResponder">

  <mx:Script>
    <![CDATA[
      import hessian.client.HessianService;

      import mx.rpc.AsyncToken;
      import mx.rpc.events.ResultEvent;

      private var service:HessianService = new HessianService("hello");

      private function sendText():void
      {
        var token:AsyncToken = service.hello.send(who.text);
        token.addResponder(this);
      }

      public function result(data:Object):void
      {
        var event:ResultEvent = ResultEvent(data);
        resultLabel.text = "The server said: \"" + event.result + "\"";
      }

      public function fault(info:Object):void
      {
      }
    ]]>
  </mx:Script>

  <mx:Panel 
    title="Caucho Hessian Hello World"
    paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">

    <mx:HBox>
      <mx:Label text='Whom do you want to say "Hello" to?'/>
      <mx:TextInput id="who" maxChars="20" enter="sendText()"/>
      <mx:Button label="Say Hello" click="sendText()"/>
    </mx:HBox>

    <mx:Label id="resultLabel" text='The server said: "null"'/>

  </mx:Panel>    
</mx:Application>
  

As in the previous example, we have a local HessianService instance, but now we declare it in ActionScript code. The destination is now the specified in the first argument of the constructor.

The actions of the TextInput and button now call a method sendText(). sendText() calls the hello method in the same way as the previous example. The difference now is that we use the return value of the send() method. The mx.rpc.AsyncToken returned is used to set a mx.rpc.IResponder to deal with the result. In this case, we have the application class itself implement mx.rpc.IResponder. This interface has two methods, result() and fault() that are called when a result or fault is returned by the service, respectively. In this example, we only deal with the result case. We use the result() method to set the text of the result Label.

Hessian Hello World Java Server

The Java server for the Hello World example performs a simple task: prefixing the given text with a greeting and returning it. There are two files involved, an interface which describes the service called com.caucho.helloworld.HelloWorld and the actual implementation class of the service, com.caucho.helloworld.HelloWorldImpl. Their source code is listed below.

HelloWorld interface
package com.caucho.helloworld;

public interface HelloWorld {
  public String hello(String who);
}
  
HelloWorldImpl class
package com.caucho.helloworld;

import com.caucho.hessian.server.HessianServlet;

public class HelloWorldImpl extends HessianServlet implements HelloWorld {
  public String hello(String who)
  {
    return "Hello, " + who + "!";
  }
}
  

The HelloWorld interface is not necessary for this example, but we have included it as it is used for Java clients that might connect to this service. We will instead focus on the actual implementation here.

The HelloWorldImpl class simply has methods that may be invoked by remote clients like our Flash application. By extending the com.caucho.hessian.server.HessianServlet, the service may be exposed as any other JavaEE Servlet.

To deploy this service we use a standard web.xml file:

HelloWorld web.xml
<web-app id="">
  <!-- Configure the HelloWorld implementation -->
  <servlet servlet-name="hello"
           servlet-class="com.caucho.helloworld.HelloWorldImpl"/>
  <servlet-mapping url-pattern="/hello/*" servlet-name="hello"/>
</web-app>
  

Copyright © 1998-2012 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).
Leading companies worldwide with demand for reliability and high performance web applications including SalesForce.com, CNET, DZone and many more are powered by Resin.

home company blog wiki docs 
app server web server 
health cloud java ee pro