Recently chatting with colleagues, learned that they are using a WebService implementation method called Hessian to implement a remote method call, is lightweight, does not rely on Java EE container, but also the binary data format transmission, efficiency than the XML method of soap is higher. Feel like a restful way, curious to access the relevant information online, summarized as follows:
First, Introduction
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.
Second, Hessian invocation instance
A) write the service-side code
Write an interface:
[Java] view Plaincopy
- Public interface Hello {
- Public String Seehello ();
- }
Write an implementation:
[Java] view Plaincopy
- public class Helloimpl implements Hello {
- Private String hellostr = "Hello World";
- Public String Gethellostr () {
- return hellostr;
- }
- public void Sethellostr (String hellostr) {
- This.hellostr = Hellostr;
- }
- Public String Seehello () {
- return hellostr;
- }
- }
Configure Web-inf.xml deployment to the Web container:
[XHTML]View Plaincopy
- <servlet>
- <servlet-name>hello</servlet-name>
- <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
- <init-param>
- <param-name>home-class</param-name>
- <param-value>com.alisoft.enet.hessian.HelloImpl</param-value>
- </init-param>
- <init-param>
- <param-name>home-api</param-name>
- <param-value>com.alisoft.enet.hessian.Hello</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- <url-pattern>/hello.xsp</url-pattern>
- </servlet-mapping>
Ok, the service-side code is finished writing.
b) Writing client code
[Java] view Plaincopy
- public class Helloservicetest {
- public static void Main (string[] args) throws Exception {
- String url = "HTTP://LOCALHOST/HESSIAN/HELLO.XSP";
- Hessianproxyfactory factory = new Hessianproxyfactory ();
- Hello hello = (hello) factory.create (hello.class, URL);
- SYSTEM.OUT.PRINTLN ("Remote Call Result:" + Hello.seehello ());
- }
- }
Executing the client, you can return the corresponding result:
Remote Call Result: Hello World
The above example is based on the Hessian package provided by Caucho, in fact Spring's Hessian call simply encapsulates it for ease of use.
Iii. mechanism of Hessian
Then Hessian is to convert the Java object into a sequence of bytes, and then transmitted over HTTP to the target server (host 2), host 2 received this sequence of bytes, according to a certain protocol standard to reverse the sequence, submitted to the corresponding service processing. The data is returned in the same way after processing is complete.
Now let's look back at the configuration in the example (Web-inf.xml):
Configuration of the Servlet:com.caucho.hessian.server.HessianServlet
Corresponding parameters: Interface (HOME-API): Com.alisoft.enet.hessian.Hello
Implementation (Home-class): Com.alisoft.enet.hessian.HelloImpl
The implementation code in Hessianservlet is as follows (skipping part of the code):
[Java]View Plaincopy
- HttpServletRequest req = (httpservletrequest) request;
- HttpServletResponse res = (httpservletresponse) response;
- InputStream is = Request.getinputstream ();
- OutputStream OS = Response.getoutputstream ();
- Input stream
- Hessian2input in = new Hessian2input (IS);
- Serializerfactory serializerfactory = Getserializerfactory ();
- In.setserializerfactory (serializerfactory);
- Output stream
- Abstracthessianoutput out;
- int major = In.read ();
- int minor = In.read ();
- out = new Hessian2output (OS);
- Out.setserializerfactory (serializerfactory);
- _homeskeleton.invoke (in, out);
The following steps are performed:
L receive the input stream and convert it to Hessian-specific hessian2input via serializerfactory
L set the output stream and convert it to Hessian-specific hessian2output via serializerfactory
L invoke the service based on the configured interface and implementation parameters, and write the results to the output stream hessian2output
L Out.close ()
Hessian remote access is based on how serialization and deserialization are. When the program runs, the various objects created by the program are in memory, and when the program runs, the objects end the life cycle. There are two main uses for serialization of objects:
L permanently save the byte sequence of the object to the hard disk, usually in a file.
L Transfer byte sequences of objects on the network
Four. Advantages of Hessian:
1-The entire jar is small, more than 200 K, 3.1 version of, of course, I downloaded the for Java version.
2-Configuration is simple, basically do not need to spend any experience to configure it
3-powerful, can throw soap away, also can put EJB aside, adopt binary to pass object
4-With multiple language support, Python C + +. NET and even flex can be used as client side
Note: Hessian for Java related information: http://hessian.caucho.com/#Java.
WebService another lightweight implementation-hessian learning notes