WebService another lightweight implementation-hessian learning notes

Source: Internet
Author: User

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
    1. Public interface Hello {
    2. Public String Seehello ();
    3. }

Write an implementation:

[Java] view Plaincopy
  1. public class Helloimpl implements Hello {
  2. Private String hellostr = "Hello World";
  3. Public String Gethellostr () {
  4. return hellostr;
  5. }
  6. public void Sethellostr (String hellostr) {
  7. This.hellostr = Hellostr;
  8. }
  9. Public String Seehello () {
  10. return hellostr;
  11. }
  12. }

Configure Web-inf.xml deployment to the Web container:

[XHTML]View Plaincopy
  1. <servlet>
  2. <servlet-name>hello</servlet-name>
  3. <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
  4. <init-param>
  5. <param-name>home-class</param-name>
  6. <param-value>com.alisoft.enet.hessian.HelloImpl</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>home-api</param-name>
  10. <param-value>com.alisoft.enet.hessian.Hello</param-value>
  11. </init-param>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>hello</servlet-name>
  15. <url-pattern>/hello.xsp</url-pattern>
  16. </servlet-mapping>

Ok, the service-side code is finished writing.

b) Writing client code

[Java] view Plaincopy
  1. public class Helloservicetest {
  2. public static void Main (string[] args) throws Exception {
  3. String url = "HTTP://LOCALHOST/HESSIAN/HELLO.XSP";
  4. Hessianproxyfactory factory = new Hessianproxyfactory ();
  5. Hello hello = (hello) factory.create (hello.class, URL);
  6. SYSTEM.OUT.PRINTLN ("Remote Call Result:" + Hello.seehello ());
  7. }
  8. }

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
  1. HttpServletRequest req = (httpservletrequest) request;
  2. HttpServletResponse res = (httpservletresponse) response;
  3. InputStream is = Request.getinputstream ();
  4. OutputStream OS = Response.getoutputstream ();
  5. Input stream
  6. Hessian2input in = new Hessian2input (IS);
  7. Serializerfactory serializerfactory = Getserializerfactory ();
  8. In.setserializerfactory (serializerfactory);
  9. Output stream
  10. Abstracthessianoutput out;
  11. int major = In.read ();
  12. int minor = In.read ();
  13. out = new Hessian2output (OS);
  14. Out.setserializerfactory (serializerfactory);
  15. _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

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.