Hessian instance of JAVA Binary Protocol (2) __java

Source: Internet
Author: User
Tags reflection serialization

The previous article has said some basic information of Hessian, the following is a basic example to see how the Hessian API to use. The examples are mainly from two aspects, first we need to look at the Hessian serialization function, then is Hessian RPC function, because the RPC function is based on serialization function, so we look at the serialization of the function;

Serialization of primitive type
Serialization of the primitive type is very simple, but we need to understand some of the compression capabilities of Hessian, because it is necessary to record the information of two methods when serializing a data: type and data, in order to make the total serialized data as small as possible, it is necessary to use the common primitive Type is compressed, so the type of the Java primitive type has an abbreviated form:
Boolean: No type, write directly to t/f
Byte/short/integer:' I '
Long' L '
Float/double:' D '
Look at the example code:
Serialization of primitive type
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Hessian2output hOut = new Hessian2output (out);
Hout.writeint (100);
Hout.flush ();
byte[] bytes = Out.tobytearray (); Get the contents of a serialization
Hout.close ();

Deserialization primitive Type
Bytearrayinputstream in = new Bytearrayinputstream (bytes);
Hessian2input hIn = new Hessian2input (in);
int i = Hin.readint ();
Hin.close ();

Hessian's type optimization 1:hessian actually optimizes the types that are often used, such as the Byte/short/integer type, which is also encoded as:' I ', the long type is encoded as:' L ', the float/double type is encoded as:' D ', the java.util.Date type is encoded as:' d ', the String/character/char/stringbuilder code is:' S '
Type optimization for Hessian 2:This optimization is only valid for Hessian2, Hessian not only optimizes the type information written in serialization, also, the value of the type (mainly positive and floating-point) is optimized; For example, we write a long value of 200, in fact we only need two bytes to store instead of 8 bytes, also for float /double can also save a lot of space, look at the following examples;
Hessian2output hOut = new Hessian2output (out);
Hout.writeint (200);
Hout.flush ();
byte[] bytes = Out.tobytearray (); Occupies 2 bytes instead of 4 bytes.
Hout.close ();
Bytearrayinputstream in = new Bytearrayinputstream (bytes);
Hessian2input hIn = new Hessian2input (in);
int i = Hin.readint ();
Hin.close ();

Serialization of reference Type
Hessian serializes an object using the "Com.caucho.hessian.io.JavaSerializer" and "Com.caucho.hessian.io.JavaDeserializer" types for serialization and deserialization , the principle is very simple, all the field (properties) of the type is obtained by reflection, and then the values of all the properties are written in sequence, and deserialization is reversed; it should be noted that for the object's properties, if it is primitive type or a common type, the optimization mechanism mentioned above is used;
Note: Hessian also requires the serialized type to implement the "Java.io.Serializable" interface by default, although it can be serialized by configuration for the type that implements the interface.
Simple example:
Testvo vo = new Testvo ();
Vo.seti (1);
Vo.setiobj (2);
VO.SETJ (3L);
Vo.setjobj (4L);
Vo.setbigdec (bigdecimal.valueof (5L));
Vo.setbigint (biginteger.valueof (6L));
VO.SETSTR ("123");
Serialization of Reference objects
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Hessian2output hOut = new Hessian2output (out);
Hout.writeobject (VO);
Hout.flush ();
byte[] bytes = Out.tobytearray ();
Hout.close ();
Deserialize a Reference object
Bytearrayinputstream in = new Bytearrayinputstream (bytes);
Hessian2input hIn = new Hessian2input (in);
Testvo deVo = (testvo) hin.readobject ();
Hin.close (); Serialization Array Type
Serializing an array instance is not much different from a typical reference instance, except that the encoding rule is different, and for an array instance, the type of the array element (component) is written first, followed by the length of the number, and then the value of each element is written in sequence; Call method
The above example uses only the serialization functionality provided by Hessian, and in fact Hessian provides RPC functionality that, on the basis of serialization functionality, provides the extra information that is required to package calls to the remote method, such as invoking the method's interface, method name, and so on, and serializing all parameters to bytes. Packaged together for binary streams to be sent via a socket to a remote server; On the server side, by reading the interface type, method name and all parameter information, and deserializing all the parameter instances, we can call the method of the specified object, and finally pass the return value to the client to complete the RPC call;
Simple example:
The interface that needs to be invoked, where the client only needs interface information
Public interface Testservice {
String GetName (string custno, int flag);
}
The simple implementation of the interface, implemented in the project is on the server side, the client only needs interface
public class Testserviceimpl implements Testservice {
@Override
public string GetName (string custno, int flag) {
Return "Test Name";
}
}
Encoding Call Information
Bytearrayoutputstream out = new Bytearrayoutputstream ();
Hessian2output hOut = new Hessian2output (out);
Hout.call ("GetName", New object[]{"0000000009", 1}); Because an interface in a project corresponds to a servlet, you do not need to send interface type information
Hout.flush ();
byte[] bytes = Out.tobytearray (); In the project, the generated bytes are sent through the socket to the server side
Hout.close ();

Decoding RPC information and calling functions
Bytearrayinputstream in = new Bytearrayinputstream (bytes);
Hessian2input hIn = new Hessian2input (in);
Hessianinputfactory _inputfactory = new Hessianinputfactory ();
_inputfactory.readheader (in);
Hin.readcall ();
Hin.skipoptionalcall ();
String methodname = Hin.readmethod (); Read the Calling method name
int arglength = Hin.readmethodarglength (); Get the number of method parameters
String CustName = (string) hin.readobject (String.class); Reading parameter values sequentially
int flag = (Integer) hin.readobject (Int.class);
Gets the method through reflection and calls the
Testserviceimpl instance = new Testserviceimpl ();
Method method = TestService.class.getDeclaredMethod (methodname, String.class, Int.class);
Object result = Method.invoke (instance, CustName, flag); Other
In the RPC example above, the Hessian framework provides rich and complex functionality, such as server-side control over Hessian versions (with Hessian 1 and Hessian 22), support for method overloading, and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.