Hessian: Flex Remoting Without AMF
Andrew Powell
For a while now, there has been a versatile remoting solution for Java applications called Hessian. It actually allows for binary transmission of data (much like AMF) between any of the following languages: Java, Flash/Flex, Python, C++, C#, D, Erlang, PHP, & Ruby. Pretty impressive, huh?
Hessian is also much more lightweight than using BlazeDS for Java remoting. It is a bit trickier to setup though. Let's start with the servlet config:
<web-app id="">
<!-- Configure the HelloWorld implementation -->
<servlet servlet-name="hello"
servlet-class="com.universalmind.helloWorld.HelloWorld"/>
<servlet-mapping url-pattern="/hello/*" servlet-name="hello"/>
</web-app>
This pushes any request coming to http://${servername}/${contextroot}/hello to our Hessian Service, defined here as:
package com.universalmind.helloWorld;
import com.caucho.hessian.server.HessianServlet;
public class HelloWorld extends HessianServlet implements IHelloWorld {
private static final long serialVersionUID = 1L;
public String hello(String who) {
if(who != null){
return "Hello, " + who + "!";
}
else{
return "Hello, nobody!";
}
}
}
Note: Notice that this extends HessianServlet.
That's how simple the server-side is. With AMF, you have a simple destination to define in the remoting-config.xml file. Hessian remoting actually requires you to create a servlet definition in the web.xml file.
The client (Flex) code is actually much simpler and straightforward:
<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>
We define our service, and the destination (the servlet defined earlier). So all requests will go to: http://${servername}/${contextroot}/hello, the servlet defined in web.xml.
Unlike BlazeDS, Hessian allows you to use not just Flex/AIR as your remoting client. It allows you to create one remoting solution for a variety of clients. If your app will have Swing, .Net, and Flex clients, then Hessian is something you should definitely look into.
Compile Note: For this to work, you must make sure you include the SWC for Hessian remoting on the Flex side and the JAR for the server-side. Both are available at http://hessian.caucho.com.
Deployment Note: A sample war file is attached to this post. It can be exploded using the "jar -xvf Hessian.war" command. The war contains all the source code needed to figure this out for yourself, and is ready to deploy to your application server to see Hessian in action.
Posted in Java | Flex | General | BlazeDS | Universal Mind | Air | Hessian |
5 comments
Jan 31, 2008 at 12:00 AM <p>Please delete the file \META-INF\context.xml.</p>
Jan 31, 2008 at 12:00 AM <p>File Fixed.</p>
Feb 1, 2008 at 12:00 AM <p>Thanks Andrew!</p>
<p>The example is very useful, simple and powerful.</p>
Feb 2, 2008 at 12:00 AM <p>Take a look at how Spring can expose Hessian service proxies from any bean (without implementing an interface or extending HessianServlet). Very simple and powerful stuff.</p>
Feb 2, 2008 at 12:00 AM <p>@Chris - I actually came to Hessian via Spring. I was using Spring to create a web service to process tens of thousands of records coming from a .NET client. SOAP wasn't cutting it, so we used the C# implementation for the client and the Spring implementation for the server-side.</p>
<p>I wanted to start out with simple examples and move up to more complex ones so people don't lose the concepts in the implementation. Next week, I'll post an example with Spring.</p>