Resin Documentationapp server |
hessian
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 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. 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(); 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(); CompressionDeflation 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(); 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(); package example; public interface MathService { public int add(int a, int b); } package example; public class MathServiceImpl implements MathService { public int add(int a, int b) { return a + b; } } <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> <%@ 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>
|