Spring Integrated Hessian1

Source: Internet
Author: User
Tags getmessage object object object serialization tomcat server

Hessian is a lightweight remote calling tool that uses the binary RPC protocol, which is ideal for sending binary data and has firewall penetration capability based on HTTP. Hessian is typically provided through a Web application, so it is very similar to the Web service we use at ordinary times. It's just that it doesn't use the SOAP protocol, but it's simpler and faster than a Web service.

Burlap is similar to Hessian, except that the burlap transfer will be XML instead of binary data.

RMI is difficult to cross the firewall, the 2 terminal must be based on Java development, transfer objects also need to consider the object version of the problem. While burlap and Hessian are HTTP-based, they are easy to traverse firewalls, and clients are not limited to Java. Burlap and Hessian use the private object serialization mechanism, which is less efficient than RMI, and RMI uses the Java serialization mechanism.

Spring also provides an RPC mechanism-http invoker, HTTP-based transmission, and the use of the Java object serialization mechanism.

1. Hessian Framework jar Package download

Download the required jar package from http://hessian.caucho.com/ , here is Hessian-4.0.37.jar. The Hessian jar package is very small.

In particular, it is important to note that: Hessian-4.0.37.jar not only in the project-dependent jar package, but also in the Web-inf under the Lib directory, or will be error:

Noclassdeffounderror:com/caucho/hessian/io/hessiandebuginputstream

2. Server-side Environment Building 2.1 and RMI, you need to define an interface first:
Package Com.hessian.isay;public interface Isay {public         string SayHello (String arg1,string arg2);         Public String Getobjectmessage (Object object);}
2.2 Then write a specific implementation class:
Package Com.hessian.isay;public class Isayimpl implements Isay {public          string SayHello (String arg1, String arg2) {
   return "Hello:" + arg1 + arg2;          }         @Override public         String getobjectmessage (Object object) {                if (object instanceof hessiantestobject) {                               Return "Server react:" + ((hessiantestobject) object). GetMessage ();                }                Return "";         }
}

2.3 Create an object that proves that the object can be transferred

Package Com.hessian.isay;import Java.io.serializable;public class Hessiantestobject implements serializable{           Private static final long serialversionuid = -6273473338054054934l;           private String message;           Public String GetMessage () {                     return message;           }           public void Setmessage (String message) {                     this.message = message;           }}

2.4 Configure Web. XML, plus the following code:

<servlet>    <description>spring mvc servlet</description>    <SERVLET-NAME>SPRINGMVC </servlet-name>    <servlet-class>org.springframework.web.servlet.dispatcherservlet </ servlet-class>    <init-param>           <description>spring MVC configuration Files </description>           < param-name>contextconfiglocation</param-name>           <param-value> Classpath:springmvc-servlet.xml </param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </ servlet>  <servlet-mapping>    <servlet-name>springMvc</servlet-name>    < Url-pattern>*.do</url-pattern></servlet-mapping>

It is important to note that Dispatcherservlet's servlet-name is SPRINGMVC, so a new file called Springmvc-servlet.xml is required. Tomcat will load this file when it is started.

2.5 Creating a Springmvc-servlet.xml file
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans >    <!--interface specific implementation class--    <bean id= "Impl" class= "Com.hessian.isay.IsayImpl"/>    <!-- Use spring's hessianservice as an agent--    <bean name= "/hellospring.do"    class= " Org.springframework.remoting.caucho.HessianServiceExporter ">        <!--Service Reference specific implementation entity Bean-->        < Property name= "Service" ref= "Impl"/>        <property name= "serviceinterface"       value= "Com.hessian.isay.Isay "/>    </bean>        <!--can configure multiple hessianserviceexporter agent Beans--></beans>

Start the Tomcat server, if the exception is not reported, then the server-side environment is built.

3. Client Environment Setup

The client needs Hessian-4.0.37.jar,hessiantestobject,isay and normalclient (or springclient).

3.1 Call without spring-dependent method

Write the test class as follows:

Import Java.net.malformedurlexception;import Com.caucho.hessian.client.hessianproxyfactory;import Com.hessian.isay.isay;public class Normalclient {public        static void Main (string[] args) throws malformedurlexception {            //spring hessian proxy servelet            String url = "Http://localhost:8080/ssm/helloSpring.do";            Hessianproxyfactory factory = new Hessianproxyfactory ();            Isay API = (Isay) factory.create (isay.class, url);            System.out.println (Api.sayhello ("Hello", "Spring"));            Hessiantestobject object = new Hessiantestobject ();            Object.setmessage ("Message from Object");            System.out.println (Api.getobjectmessage (object));}        }
3.2 Relies on the implementation of spring

First write the XML file Hessian_client.xml, complete the bean definition:

<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans >        <!--client Hessian agent Factory bean--        <bean id= "clientspring" class= " Org.springframework.remoting.caucho.HessianProxyFactoryBean ">            <!--request Proxy servlet path--                    < Property name= "serviceurl" >                <value>http://localhost:8080/ssm/helloSpring.do</value>            < /property>            <!--interface definition--            <property name= "Serviceinterface" >                <value> com.hessian.isay.isay</value>            </property>        </bean></beans>

The method of writing dead URLs directly above is not very good, it is more flexible to write as follows: Http://${servername}:${httpport}/${serverpath}/${contextpath}.

Then write the test code as follows:

Package Com.hessian.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.hessian.isay.isay;public Class springclient {public    static void Main (string[] args) {        ApplicationContext Contex = new Classpathxmlapplicationcontext (                "Hessian_client.xml");        Obtain the client's Hessian agent factory bean        Isay i = (Isay) contex.getbean ("clientspring");        System.out.println (I.sayhello ("Hello", "Spring"));        Hessiantestobject object = new Hessiantestobject ();         Object.setmessage ("Message from Object");         System.out.println (I.getobjectmessage (object));}    }

  

Burlap environment construction and hessian very similar, need is also hessian-4.0.37.jar.

There are 2 needs to be changed:

One is to configure the configuration file in step 2.5 as follows:

 <! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans > <!--interface specific implementation class--<bean id= "Impl" class= "Com.hessian.isay.IsayImpl"/> <! --use spring's hessianservice as proxy, service reference concrete implementation entity Bean--> <!--<bean name= "/hellospring.do" class= "org.sp Ringframework.remoting.caucho.HessianServiceExporter "> <property name=" service "ref=" Impl "/>                     ; <property name= "Serviceinterface" value= "Com.hessian.isay.Isay"/> </bean>-&L T;bean name= "/hellospring.do" class= "Org.springframework.remoting.caucho.BurlapServiceExporter" > < ;p roperty name= "service" ref= "Impl"/> <property name= "serviceinterface" value= "Com.hessian.isay.Is Ay "/> </bean> <!--can configure multiple hessianserviceexporter agent Bean--></beans> 

The second is to change the configuration file in 3.2 to:

  <bean id= "clientspring" class= "Org.springframework.remoting.caucho.BurlapProxyFactoryBean" >            <!-- Request Proxy servlet path--                    <property name= "serviceurl" >                <value>http://localhost:8080/ssm/ hellospring.do</value>            </property>            <!--interface definition--            <property name= " Serviceinterface ">                <value>com.hessian.isay.Isay</value>            </property>        </ Bean>

Spring comes with httpinvoker based on HTTP and uses Java's own serialization. Configuration and Hessian basic, but also only need the 2 places.

The disadvantage of using httpinvoker is that spring must be used and must be based on Java.

One is to configure the configuration file in step 2.5 as follows:

<bean name= "/hellospring.do" class= "Org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >        <property name= "service" ref= "Impl"/>        <property name= "Serviceinterface" value= " Com.hessian.isay.Isay "/> </bean>

The second is to change the configuration file in 3.2 to:

<bean id= "clientspring" class= "Org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" >                 <property name= "serviceurl" >            <value>http://localhost:8080/ssm/helloSpring.do</value>        </property>                <property name= "Serviceinterface" >            <value>com.hessian.isay.isay</value >        </property>        </bean>

Spring Integrated Hessian1

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.