Hessian an introductory case

Source: Internet
Author: User
Tags data structures documentation requires tomcat
I. Introduction of Hessian

Hessian, like WebService, can complete a remote method call.

For example, a company already has a CRM system, and then want to develop a logistics system, and logistics system customer information needs to use the customer information in the CRM system, which requires the new logistics system to remove the method of CRM system to obtain customer data. We can usually invoke the methods within a system, but we generally cannot invoke the methods of other systems, and sometimes the other systems we need are not necessarily the same programming language that we use, and the two systems are generally not on the same server. To complete this remote method call, you can do so with Hessian.

The Hessian uses standard protocols for data transfer. Equivalent to distributed data interaction.

RMI (Java remote method invocation) Java remoting methods call.

In fact, the interface of RMI is provided inside Java. But if you use the Java-provided RMI, there is a problem, and only Java calls are limited.

Comparison of several technical efficiencies:

RMI > Httpinvoker >= hessian>>burlap >> WebService

RMI is Java-brought with limitations and is limited to remote access between Java programs.

Httpinvoker uses the Java serialization technology to transfer objects, which are essentially the same as RMI, and are equally similar in terms of efficiency.

The lowest efficiency is webservice, why there are a large number of people in use. Because the XML document is used, the Protocol standard.

Hessian:

Hessian is faster than RMI when transferring a small number of objects, but it is 20% slower than RMI to transfer objects with complex data structures or large numbers of data objects, but only when the data is extremely large.

Hessian is implemented based on the BINARY-RPC protocol and is a binary data format, so it can be used across different language platforms, so either Java or. NET can be used. Second, Hessian download

http://hessian.caucho.com/#Java
three, Hessian application simple introduction case

Build two Web projects:

One server (service side), one client (clients), simulates two different systems, the client invokes the functionality of the server.

Import the Hessian jar package into lib.

Establish an interface on the server that provides methods for external calls.

Public interface HelloService {public
	String SayHello ();
}

Provides an implementation class for this interface.

public class Helloserviceimpl implements helloservice{public
	String SayHello () {
		return "Hello Service!!!";
	}
}

As written here, the client still cannot access the functionality of the server and needs to configure the Hessian service. The configuration service can be called remotely.

Configuration can refer to Hessian's documentation: HESSIAN.MHT

Click here for standard Web. XML configuration information to get the standard example:

Xml

<web-app>
  <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>example. basicservice</param-value>
    </init-param>
    <init-param>
      <param-name>home-api </param-name>
      <param-value>example. basic</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
	<!-Configuration Access path, the current access path is the root directory of Hello, can be changed to their own needs--
<servlet-name>hello</servlet-name>
    < url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Paste this section into Web. XML on the server side. As you can see, you're actually configuring a servlet.

In this paragraph, we need to change the red bold part. Modified to:

		<init-param>
			<!--implementation class, change <param-value> to the fully qualified name of the implementation class--
			<param-name>home-class</ Param-name>
			<param-value>service. helloserviceimpl</param-value>
		</init-param>
		<init-param>
			<!--interface, will < Param-value> to the fully qualified name of the interface--
			<param-name>home-api</param-name>
			<param-value> Service. Helloservice</param-value>
		</init-param>

The configuration is complete, the server is published to Tomcat, and Tomcat is run, and this interface can be called by the client.

In the browser input: Http://localhost:8080/server/hello can be accessed.

This interface appears, saying that Hessian requires a POST request.

Next we do the client.

At the client we have introduced the Hessian jar package, but we cannot call the server directly without writing anything. The interface must be unified.

Copy the server's interface along with the package into the client, as shown in the figure:

The contents of the interface are the same:

Public interface HelloService {public
	String SayHello ();
}

With this interface, we can build our test cases. You can create a new test package in the client and build a Hessiantest testing class.

Use JUnit tests.

Now to call the server's address in the client, this address is the address we just entered in the browser: Http://localhost:8080/server/hello

Note that in the real world, localhost is an IP address because the general server and client are in different servers.

Test class how to write it.

There are sample code in the documentation for Hessian:

Hessian Client for Basic service

package example;

Import com.caucho.hessian.client.HessianProxyFactory;

public class Basicclient {public
  static void main (string []args)
    throws Exception
  {
    string url = "http:/ /www.caucho.com/hessian/test/basic ";

    Hessianproxyfactory factory = new Hessianproxyfactory ();
    Basic basic = (Basic) factory.create (Basic.class, url);

    System.out.println ("Hello:" + Basic.hello ());
  }
}

The core class used is the Hessianproxyfactory factory class.

Import java.net.MalformedURLException;

Import Org.junit.Test;

Import service. HelloService;

Import com.caucho.hessian.client.HessianProxyFactory;

public class Hessiantest {
	@Test public
	void Hessiandemo () throws Malformedurlexception {
		//define access to remote host address
		String target = "Http://localhost:8080/server/hello";
		Get Factory object
		hessianproxyfactory factory = new Hessianproxyfactory ();
		Through the factory's create method, get the implementation class of the HelloService interface,
		//The first parameter is the type of object to get, the second is the address of the host to access
		// The HelloService object obtained is actually a HelloService proxy object on the server side,
		//We actually access the method of the remote host when we use its method
		HelloService HelloService = 
(HelloService) factory.create (Helloservice.class, target);
		
		Invokes the function of the remote host.
		System.out.println (Helloservice.sayhello ());
	}
}

Console Display information:

At this point, the SayHello method of the server implementation class is successfully accessed from the client.


Hessian principle of communication:

Helloservice.sayhello ()

In the invocation of this sentence, in fact, the bottom of the access to our designated address, we specify the address is a servlet, so here is actually access to this servlet (Hessianservlet), the servlet to find the interface you specify the implementation of the registration class ( Registered in the server's Web. xml), this method is found in the implementation class, and the return value of the method is returned to the client as a binary.


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.