Hessian Serialize Example

From Resin 4.0 Wiki

Revision as of 17:00, 14 June 2012 by Rick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Hessian Serialization

Hessian 2.0 provides cross-language binary object serialization with efficiencies better than java.io serialization. The compaction encodings added to Hessian 2.0 have improved an already-popular cross-platform binary web services protocol. With these changes, Hessian 2.0 now directly competes with java.io serialization in efficiency.

Files in this tutorial

File Description
WEB-INF/classes/example/HessianSerializeServlet.java Serialization Servlet
WEB-INF/classes/example/Car.java Serialized class
WEB-INF/classes/example/Color.java Enumeration for the car color
WEB-INF/classes/example/Model.java Enumeration for the car model

Overview

In this simple example, we'll use Hessian 2.0 to serialize three Car objects to a byte array. The serialized data could be saved in a persistent store, or sent as a message in a SOA or JMS application. Because Hessian 2.0 is cross-language, the message could be deserialized by a .NET or even a PHP application.

The efficiency of Hessian 2.0 is about twice that of java.io serialization. This is a tiny example, of course, but does show that you can send compact, cross-language messages without having to use bloated XML solutions like SOAP.

Service Size
Hessian 2.0 139 bytes
java.io 287 bytes
Hessian 2.0 with Deflation 164 bytes


Model

The example's model is a Car object with three fields: year, model, and color. The model and color are enumeration types.

Car.java

package example;

public class Car {
  private int year;
  private Model model;
  private Color color;
}

Car.java

package example;

public enum Model {
  CIVIC,
  EDSEL,
  MODEL_T,
}

Color.java

package example;

public enum Model {
  BLACK,
  GREEN,
  BLUE,
}


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. In this example, we've encapsulated the object in a Hessian 2.0 message using startMessage and completeMessage to show how you would create a message for an SOA or JMS application.

Serialization

ByteArrayOutputStream bos = new ByteArrayOutputStream();

HessianFactory factory = new HessianFactory();

Hessian2Output out = factory.createHessian2Output(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();

The deserialization is the same as serialization. Create an Hessian2Input around any InputStream and read data from the stream.

Deserialization

ByteArrayInputStream bin = new ByteArrayInputStream(data);

HessianFactory factory = new HessianFactory();

Hessian2Input in = factory.createHessianHessian2Input(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();


Hessian Compression

The <a href="http://caucho.com/resin-3.1/doc/hessian-2.0-spec.xtp">Hessian 2.0 draft specification</a> has added support for envelopes around Hessian messages. These envelopes can provide additional capabilities like compression, encryption, and message signatures. The envelope can also be used to attach routing and reliability information to a message. Since envelopes are nestable, each envelope can be simple and provide powerful capabilities when combined. For example, a secure messaging system might compress, encrypt and then securely sign a message.

The API for using envelopes is wrap() for writing a message and unwrap() for reading a message. The application serialization code itself is identical, since the envelope just creates a Hessian2Input or Hessian2Output wrapper around the original stream.

Deflation

Deflation envelope = new Deflation();

ByteArrayOutputStream bos = new ByteArrayOutputStream();

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

out = out.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();
Personal tools
TOOLBOX
LANGUAGES