Using the Hessian web-service protocol with the Jetty servlet container

Searching for alternatives to Java RMI, the Hessian/Burlap protocol can be a good choice. Hessian is a binary web-service protocol supported by several languages such as Java, .NET/C#, C++, PHP etc.
Here is how you easily use Hessian with the Jetty servlet container.

The servercode need to embed Jetty. Download Jetty and use these .jars in your classpath when deploying (see bottom):

jetty-continuation*.jar
jetty-http*.jar
jetty-io*.jar
jetty-security*.jar
jetty-server*.jar
jetty-servlet*.jar
jetty-servlets*.jar
jetty-util*.jar

Servercode:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class HessianTest {

    public static void main(String[] args) throws Exception {

      Server server = new Server(8080);
      ServletContextHandler context = new ServletContextHandler(
      ServletContextHandler.SESSIONS);
      server.setHandler(context);
      ServletHolder servletHolder = new ServletHolder(new BasicService());
      context.addServlet(servletHolder, "/test");
      server.start();
      server.join();

    }

}

Basic interface:

public interface Basic {

    public String hello();

}

Basic implementation:

public class BasicService extends HessianServlet implements Basic {

    public String hello() {

      return "Hello! It works!";

    }

}

Client application:

import com.caucho.hessian.client.HessianProxyFactory;

public class HessianClient {

    public static void main(String[] args) throws Exception {

      HessianProxyFactory factory = new HessianProxyFactory();
      Basic basic = (Basic) factory.create(Basic.class,
      "http://myhost:8080/test");

      System.out.println("Hello: " + basic.hello());

    }

}

That’s pretty much what you need to get it up and running !
Nb! You need the hessian-4.0.3.jar (or later) in your classpath as well.

Server startup: java -cp .;jetty-server.jar;jetty-util.jar;servlet-api-2.5.jar;jetty-http.jar;jetty-io.jar;jetty-continuation.jar;hessian-4.0.3.jar;jetty-servlet.jar;jetty-security.jar HessianTest

Client startup: java -cp .;hessian-4.0.3.jar HessianClient

Leave a Reply