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


 

Resin Documentation

home company blog docs 
app server 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

hessian


Hessian Protocol

Hessian is a binary serialization protocol with support for RPC. It provides cross-language binary object serialization with efficiencies better than java.io serialization

See hessian.caucho.com and Hessian on Wikipedia

Hessian Serialization

The Hessian serialization API resembles java.io ObjectOutputStream serialization. The general steps are to create a Hessian2Output around any OutputStream and write data to the stream.

Serialization

ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bos);

out.startMessage();
out.writeInt(2);

Car car1 = new Car(Model.EDSEL, Color.GREEN, 1954);
out.writeObject(car1);

Car car2 = new Car(Model.MODEL_T, Color.BLACK, 1937);
out.writeObject(car2);

out.completeMessage();
out.close();

byte []data = bos.toByteArray();

Deserialization

ByteArrayInputStream bin = new ByteArrayInputStream(data);
Hessian2Input in = new Hessian2Input(bin);

in.startMessage();

ArrayList list = new ArrayList();

int length = in.readInt();

for (int i = 0; i < length; i++) {
  list.add(in.readObject());
}

in.completeMessage();

in.close();
bin.close();

Compression

Deflation
Deflation envelope = new Deflation();

ByteArrayOutputStream bos = new ByteArrayOutputStream();

HessianFactory factory = new HessianFactory();
Hessian2Output out = factory.createHessian2Output(bos);

out = envelope.wrap(out);
out.startMessage();

Car car1 = new Car(Model.EDSEL, Color.GREEN, 1954);
out.writeObject(car1);

out.completeMessage();
out.close();

byte []data = bos.toByteArray();
Inflation
Deflation envelope = new Deflation();

ByteArrayInputStream bin = new ByteArrayInputStream(data);

HessianFactory factory = new HessianFactory();
Hessian2Input in = factory.createHessian2Input(bin);

in = envelope.unwrap(in);

in.startMessage();

Object value = in.readObject();

in.completeMessage();

Hessian Web Service

MathService.java (remote interface)
package example;

public interface MathService {
  public int add(int a, int b);
}
MathServiceImpl.java
package example;

public class MathServiceImpl implements MathService {
  public int add(int a, int b)
  {
    return a + b;
  }
}
Server configuration resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">

  <servlet-mapping url-pattern="/math/*"
                   servlet-class="example.MathService">
    <protocol uri="hessian:"/>
  </servlet-mapping>

  <remote-client name="math">
    <uri>hessian:url=${webApp.url}/math/</uri>
    <interface>example.MathService</interface>
  </remote-client>

</web-app>
JSP Java client
<%@ page import="javax.inject.Inject" %>
<%@ page import="example.MathService" %>
<%!
@Inject MathService math;
%>
<pre>
3 + 2 = <%= math.add(3, 2) %>
3 - 2 = <%= math.sub(3, 2) %>
3 * 2 = <%= math.mul(3, 2) %>
3 / 2 = <%= math.div(3, 2) %>
</pre>

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