Simple application of Hessian servlet and Hessian Spring

Source: Internet
Author: User

Turn from:

http://lancui.iteye.com/blog/935578

Brief introduction


Simpler and faster than Webservice,hessian. The Binary RPC Protocol (binary) is used because the binary protocol is used, so it is well suited for sending binary data. Hessian is typically serviced through a Web application, so it is very similar to webservice. It's just that it doesn't use the SOAP protocol.
Hessian provides remote services through Servlets. A request that matches a pattern needs to be mapped to the Hessian service. Spring's Dispatcherservlet can complete this function, and Dispatcherservlet can forward requests for matching patterns to the Hessian service. The server side of Hessian provides a servlet base class to handle sent requests, and this remote procedure call for Hessian, which is implemented entirely using dynamic proxies, is recommended for interface-oriented programming, so the Hessian service recommends exposing through interfaces.
Hessian processing Process:
The client-to-serialization writes to the output stream--The remote method (server side)--serialization writes to the output stream--the client reads the input stream and outputs the result


Environment Construction
To download and install Hessian, please follow the steps below:
(1) Login http://www.caucho.com/hessian/download Hessian.
(2) put Hessian the corresponding jar package into the Web application, all the jar files should be placed under the Web-inf/lib, the file is no exception.
Two different ways
Pure Hessian
This approach is mainly applicable to projects where there is no application like the spring Framework, the benefit is the configuration is convenient, but when the content of the case, the configuration of a lot of content.
I'll take a look at some of the steps I've made in this example:
1: Put the Hessian corresponding jar package into the project.
2: Because the Hessian is programming with interface-oriented, write an interface because the client only needs the interface, without the real implementation class.
Package Jzh.demo;
Public interface Ihello {
String SayHello ();
}
3: Write a class to implement this interface.
Package Jzh.demo.impl;
Import Jzh.demo.IHello;
Import Com.caucho.hessian.server.HessianServlet;
public class Hello extends Hessianservlet implements Ihello {
Public String SayHello () {
Return "Hello World";
}
}
Detailed configuration of the 4:web.xml
<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>jzh.demo.imple.Hello</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>jzh.demo.IHello</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>

5: The client remotely invokes the interface provided by the server side, using the Hessian hessianproxyfactory, to implement the remote agent.
1) Put the server-side generated jar package into the project.
2) The corresponding fragment program is as follows:
String url = "Http://220.114.108.185:8080/Hessian/Hello";
Hessianproxyfactory factory = new Hessianproxyfactory ();
try {
Ihello Hello = (ihello) factory.create (Ihello.class,url);
System.out.println (Hello.sayhello ());
} catch (Malformedurlexception e) {
E.printstacktrace ();
}
6: Function complete.


Hessian and Spring integration


This is a bit of a hassle compared to the way it was. Hessian provides remote services through Servlets. A request that matches a pattern needs to be mapped to the Hessian service. Spring's Dispatcherservlet can do this, Dispatcherservlet can forward requests for matching patterns to the Hessian service, and Web. XML simply defines the "request Forwarder" that will match the/remoting/* The request is intercepted and forwarded to the context bean for processing. And Hessianserviceexporter provides the bean service.
So the Hessian and spring integration is mainly two jobs:
1: Block URL requests via Dispatcherservlet.
2:hessianserviceexporter provides the bean service, and spring uses hessianserviceexporter to export a regular bean as a Hessian service.
I'll take a look at some of the steps I've made in this example:
1: The same as above.
2: The same as above.
3: The same as above.
Detailed configuration of the 4:web.xml
<servlet>
<servlet-name>remoting</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--default is to load the web-inf/servlet-name+ "-servlet.xml" file, where it can be configured to load web-inf/classes/remoting-servlet.xml this file--

<init-param>
<param-name>namespace</param-name>
<param-value>classes/remoting-servlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
5: Configure the Remoting-servlet.xml file
<!--define normal bean instances--
<bean id= "Hello" class= "Jzh.demospring.impl.Hello"/>
<!--use Hessianserviceexporter to export ordinary beans to hessian service--
<bean name= "/hessianremoting" class= "Org.springframework.remoting.caucho.HessianServiceExporter" >
<!--the target bean--> to be exported
<property name= "service" ref= "Hello"/>
<!--Hessian Service interface---
<property name= "Serviceinterface" value= "Jzh.demospring.IHello"/>
</bean>
6: Client defines a remoting-client.xml file
<bean id= "myserviceclient" class= "Org.springframework.remoting.caucho.HessianProxyFactoryBean" >
<property name= "serviceurl" >
<value>http://220.114.99.62:8080/HessianSpring/remoteing/HessianRemoting</value>
</property>
<property name= "Serviceinterface" >
<value>jzh.demospring.IHello</value>
</property>
</bean>
7: Client call.
Try
{
ApplicationContext context = new Classpathxmlapplicationcontext ("Remote-client.xml");

Ihello Hello = (Ihello) context.getbean ("Myserviceclient");

System.out.println (Hello.sayhello ());
}
catch (Exception e)
{
E.printstacktrace ();
}
Precautions
1: When you start Tomcat, the following issue occurs: Java.lang.ClassNotFoundException:org.springframework.web.servlet.DispatcherServlet. , and in engineering it is possible to find the corresponding jar file, which is exactly what the problem is.
WORKAROUND: The corresponding jar file may not be loaded under the web-inf/lib of the project
2:org.springframework.remoting.remoteaccessexception:cannot Access Hessian service at [http://61.152.162.173/remote /remoteservice];
WORKAROUND: This exception is typically caused by an exception in the service-side operation





Release date: 2008-08-13 Thank you, is learning how to build Hessian service, looked after the help is very big!


Release date: 2008-08-19 I think RPC has two ways to invoke the remote method:
1, the method is executed remotely, the operation results are passed back to the client. Advantages: Small server load, small amount of data transfer, the client does not need to know the server-side defined service interface. Cons: Complex operations such as assembling and parsing data are required.
2. The method is loaded to the client to run. Advantages: Can be combined with the cache, each run is only transmitted once, high reliability, some methods do not return a value, just for example print statements can be run on the client. Disadvantage: The transfer of data is large, the client needs to know the service interface.

I guess
Ihello Hello = (ihello) factory.create (Ihello.class,url);
System.out.println (Hello.sayhello ());
It's the second way





Release date: 2008-08-29 In fact, it's the first way to do it remotely, not the second one you're talking about.




5: The client remotely invokes the interface provided by the server side, using the Hessian hessianproxyfactory, to implement the remote agent.
1) Put the server-side generated jar package into the project.

Simple application of Hessian servlet and Hessian Spring

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.