Hessian Principle Analysis
A. The fundamentals of remote communication Protocols
Network communication needs to do is to transfer the stream from one computer to another computer, based on the transport Protocol and network IO to achieve, where the transport protocol is more famous HTTP, TCP, UDP and so on, HTTP, TCP, UDP are based on the concept of Socket for a certain type of application Exhibited in the Transmission Protocol, network IO, mainly bio, NIO, Aio three ways, all distributed application communications are based on this principle, only for the ease of use of the application, various languages will often provide some more application-friendly application layer protocol.
two. Application-level protocol BINARY-RPC
BINARY-RPC is a remote invocation protocol similar to RMI, which differs from RMI in that it defines the requested information in a standard binary format (the requested object, method, parameter, etc.), and what is the benefit of being able to use it when communicating across languages.
Take a look at a remote communication process for the BINARY-RPC protocol:
1, the client initiates the request, according to the BINARY-RPC protocol to fill the request information;
2. After filling, the binary format file is converted to stream and transmitted through the transmission protocol.
3, received after receiving the stream into a binary format file, according to the BINARY-RPC protocol to obtain the requested information and processing;
4. After processing, the results are written in a binary format file and returned in accordance with the BINARY-RPC protocol.
Summary of issues:
1. What is the standard format for transmission?
A binary file in standard format.
2. How do I convert a request to a streamed stream?
Converts binary format files to streams.
3, how to receive and process the flow?
Gets the requested stream through the listening port, translates into a binary file, obtains the requested information according to the protocol, processes it, and returns the result to the XML.
4. What is the transmission protocol?
Http.
three. Hessian --a library for remote communication
Hessian is a remote communication library provided by Caucho based on BINARY-RPC implementation.
1, is based on what protocol to achieve?
Implementation based on the BINARY-RPC protocol.
2, how to initiate the request?
The request needs to be initiated through the API provided by Hessian itself.
3, how to convert the request into a protocol-compliant format?
The Hessian uses its own serialization mechanism to serialize the request information, resulting in a binary stream.
4. What transport protocol is used for transmission?
The Hessian is transmitted based on the HTTP protocol.
5. What mechanism is the response end based on to receive requests?
The response side receives the request based on the API provided by Hessian.
6. How to restore a stream to a transfer format?
Hessian the request information according to its private serialization mechanism, which is the corresponding request information object when passed to the consumer.
7. How to respond after the processing is finished?
After processing, the Hessian returns the resulting object, which is then serialized and transmitted to the calling end.
Four. Hessian source code Analysis
Take the Hessian and Spring DM Server consolidation environment as an example.
- 1. Client initiated request
This remote procedure call from Hessian is fully implemented using dynamic proxies. There are clients to see.
In addition to the encapsulation of spring, the client mainly through the Hessianproxyfactory create method is created interface proxy class, the class implements the interface, the JDK proxy class will automatically use the Invocationhandler implementation class (the class in the H The Invoke method body that is represented as Hessianproxy in Essian to populate the method body of the generated proxy class.
When the client system starts:
Create agents based on serviceurl and Serviceinterface.
Hessianproxyfactorybean class
Hessianclientinterceptor class
Createhessianproxy (Hessianproxyfactory proxyfactory)
Hessianproxyfactory class
Public Object Create (Class API, String urlname)
Client Calls Hessian when serving:
Hessianproxy invoke of Class (object proxy, method, Object []args) Method
String methodName = Method.getname ();//Get Method name
Object value = Args[0]; Get Incoming parameters
conn = SendRequest (Manglename, args); This method and the server side get the connection
Httpconn = (httpurlconnection) conn;
Code = Httpconn.getresponsecode (); Make a request
Wait for the server side to return the corresponding ...
is = Conn.getinputstream ();
Object value = In.readobject (Method.getreturntype ()); Get return value
Hessianproxy class URLConnection SendRequest (String methodName, Object []args) Method:
URLConnection conn = _factory.openconnection (_url); Create URLConnection
OutputStream OS = Conn.getoutputstream ();
Abstracthessianoutput out = _factory.gethessianoutput (OS); Encapsulated as Hessian's own input and output API
Out.call (MethodName, args);
Return conn;
- 2. server-side receive requests and process requests
Server-side interception of the corresponding request to:
Org.springframework.remoting.caucho.HessianServiceExporter
The specific processing steps are as follows:
A) Hessianserviceexporter class
(Hessianexporter) Invoke (Request.getinputstream (), Response.getoutputstream ());
b) Hessianexporter class
(Hessian2skeletoninvoker) This.skeletonInvoker.invoke (InputStream, OutputStream);
c) Hessian2skeletoninvoker class
Convert input and output seals into hessian2input and hessian2output that are unique to Hessian
Hessian2input in = new Hessian2input (istouse);
In.setserializerfactory (this.serializerfactory);
Abstracthessianoutput out = null;
int major = In.read ();
int minor = In.read ();
out = new Hessian2output (ostouse);
out = new Hessianoutput (ostouse);
Out.setserializerfactory (this.serializerfactory);
(Hessianskeleton) This.skeleton.invoke (in, out);
D) Hessianskeleton class
Read Method name
String methodName = In.readmethod ();
method = GetMethod (MethodName);
Read method parameters
Class []args = Method.getparametertypes ();
Object []values = new Object[args.length];
Execute the appropriate method and get the results
result = Method.invoke (service, values);
Results written to the output stream
Out.writeobject (result);
Summary: by the above source analysis, the client initiated the request and server-side receive processing requests are through Hessian own API. The input and output streams are encapsulated as Hessian's own hessian2input and Hessian2output, and the next section we'll look at what Hessian's own encapsulated input and output did!
Five. serialization and deserialization implementation of Hessian
Hessian source Com.caucho.hessian.io This package is Hessian implementation of serialization and deserialization of the core package. Among them abstractserializerfactory, Abstracthessianoutput, Abstractserializer, Abstracthessianinput, AbstractDeserializer Is the core structure code for Hessian serialization and deserialization.
- Abstractserializerfactory, it has 2 abstract methods:
Depending on the class, decide which serialization tool class to use
Abstract public Serializer GetSerializer (Class cl) throws hessianprotocolexception;
Determine which deserialization tool class to use according to the class
Abstract public Deserializer Getdeserializer (Class cl) throws hessianprotocolexception;
- Serializerfactory inherits Abstractserializerfactory.
In Serializerfactory there are many static maps used to store the mappings of classes to serialization and deserialization tool classes, so that if the already used serialization tool can be used directly, it is not necessary to re-instantiate the tool class.
In Serializerfactory, the GetSerializer method of abstract classes is implemented, and different serialization tools are obtained according to different classes that need to be serialized, a total of 17 serialization tools, Hessian for different types of Java Objects implement different serialization tools, and the default serialization tool is Javaserializer.
In Serializerfactory, the Getdeserializer method of the abstract class is also implemented to obtain different deserialization tools based on different classes that need to be deserialized, and the default deserialization tool class is javadeserializer.
- Hessianoutput inherits Abstracthessianoutput as an implementation of the serialized output stream.
It implements a number of ways to do stream output.
It is important to note that the method will first call serializerfactory based on the class to get the serializer serialization tool class
public void WriteObject (Object object)
Throws IOException
{
if (object = = null) {
Writenull ();
Return
}
Serializer Serializer;
Serializer = _serializerfactory.getserializer (Object.getclass ());
Serializer.writeobject (object, this);
}
- Now let's take a look at Abstractserializer.
Its writeobject is a method that must be implemented in the subclass, Abstractserializer has 17 seed classes implemented, Hessian according to different Java object types to implement a different serialization tool class, where the default is Javaserializer.
And the implementation of the WriteObject method of Javaserializer, traversing the data members of the Java object, according to the type of data members to obtain their own fieldserializer, a total of 6 of the default Fieldserializer.
Take the default Fieldserializer example, or call Abstracthessianoutput subclass to WriteObject, this time, can certainly find the corresponding serializer to do the serialization
In the same vein, you can reverse the Hessian of the anti-serialization mechanism. Serializerfactory can get the deserialization tool class for deserialization, depending on the class that needs to be deserialized.
Summary: Thanks to the implementation mechanism of Hessian serial number and deserialization, Hessian serialization is fast and the number of bytes after serialization is less than other techniques.
Reference documents:
- "Java Remote communication Optional Technology and principles" http://java.chinaitlab.com/base/740383.html
- "Hessian-3.2.0 Source"
- On the realization of Hessian serialization http://www.javaeye.com/topic/245238
- "Hessian 2.0 Serialization Protocol Specification"
Http://blog.csdn.net/xpspace/archive/2007/10/05/1811603.aspx
Hessian protocol principle