Hacking on GraphHopper - a Java road routing engine. Peter has posted 62 posts at DZone. You can read more from them at their website. View Full User Profile

Hessian Web Service Protocol - Hello World Example

04.13.2009
| 20061 views |
  • submit to reddit

Yesterday I took a quick look to the binary communication protocol: Hessian. With Hessian your admins will not have any troubles with port-activation for your (’desktop’) application, if they have to access remote services (DB etc.).

The Java implemenation from Caucho is released under the Apache License 2.0. Hessian is well integrated into Spring and seems to perform well for version 3.2 (one year old!). An ORM tool which supports Hessian out of the box is Cayenne.

Example

Now to our hello world example. It is a maven project and you can get it from here (public domain of course …).

If you don’t want to use maven you should get the following jars:
hessian-libs

But maybe you have the same problems with downloading a none-corrupted hessian.jar from Caucho (Why that?) - then you will be forced to install maven or NetBeans.

The usage is simple: In NetBeans you can directly open maven projects with the maven plugin. Then start the jetty-server: go into the MyServlet class and press SHIFT+F6. You should see in the output window:
… INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
… INFO:  jetty-6.1.16
… INFO:  Started SocketConnector@0.0.0.0:8080

Then go to the Client class and press SHIFT+F6. You should see a new window coming up:

hessian-client

Change the text and press ENTER will push the text to the server and get the response text at the bottom of the window updated:
hessian-client-result

Now let us understand what is necessary:

  1. An interface which has to be available on the server and on the client side:
    public interface CommunicationService {
    String communicate(String str);
    }
  2. An implementation of this interface on the server side:
    public class MyServlet extends HessianServlet implements CommunicationService {
    public String communicate(String str) {
    return "Hello World! " + str;
    }
    ...
    }

    (Here only the ‘implements CommunicationService’ is important)

  3. Now the server code:
    public static void main(String[] args) throws Exception {
            Server server = new Server(8080);
            Context context = new Context(server, "/", Context.SESSIONS);
            context.addServlet(MyServlet.class, "/communication-service");
            server.start();
    }
  4. And the client code:
    String url = "http://localhost:8080/communication-service";
    HessianProxyFactory factory = new HessianProxyFactory();
    final CommunicationService basic = (CommunicationService) factory.create(CommunicationService.class, url);
    ...
    resultLabel.setText("Server said : " + basic.communicate(field.getText()));
  5. Thats it!

Looks really like RPC of Java but it is language independent - there are implementations for ruby, phyton … and it seems to be more performant than language independent solutions like XML-RPC. Are there other advantages or disadvantages?

Published at DZone with permission of its author, Peter Karussell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Joseph Schmidt replied on Mon, 2009/04/13 - 10:12am

> Are there other advantages or disadvantages?

Hessian is a nice solution but it is missing authentication and session management to be really usable as an intra-application communication protocol, thus being useless for most real scenarios that would like to use it as it is.

Cayenne has implemented authentication, session management (and other goodies) for it (as part of their ROP framework), but they are not willing to release that part as a reusable and independent library:see discussion thread

Caucho on the other hand is not going to do any improvements for it.

So all in one, the idea is nice, but it remains largely that - until those real problems (mentioned above) are solved.

I suppose this is also the reason it has such a narrow adoption (despite of it's other advantages like: simplicity, great speed, wonderful idea, etc.).

Jean-Francois P... replied on Mon, 2009/04/13 - 10:50pm in response to: Joseph Schmidt

Well, it is not such a big deal to add authentication and session management to Hessian (although I admit that Hessian classes are not really easy to extend).
I have done that a long time ago in my HiveRemoting project. IIRC, the main change was to use commons-httpclient instead of Java default URL sockets that are too limited.

Jean-Francois P... replied on Mon, 2009/04/13 - 10:53pm

It's a long time I didn't play with Hessian, but I am surprised by the subclassing of HessianServlet. IIRC, you don't have to do so to have your Hessian services managed by HessianServlet. But maybe my memory fails me?

Manjuka Soysa replied on Tue, 2009/04/14 - 6:12pm

If you are using Hessian with Spring, you can use the Acegi security interceptors for authentication.

With a desktop application, session management should not be an issue - session information can be stored on the client side.

However if I was using Spring, I would use Spring's own HTTPInvoker for remoting, not Hessian.

Peter Karussell replied on Thu, 2009/04/16 - 4:33pm

Hi all, thanks for your feedback!

@jschmidt71 "but they are not willing to release that part as a reusable and independent library"

Shouldn't this be done easily? Or are other dependencies to the cayenne project?

@Jean-Francois P: "Java default URL sockets that are too limited."

What is the limitation?

@Manjuka Soysa: "However if I was using Spring, I would use Spring's own HTTPInvoker for remoting, not Hessian."

What is the difference? Are they both identical in its usage?

@all:
Which solution would you go, if you develop a rich client for a customer? I am using the spring rich client ...

Peter.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.