Hessian integration with spring

Source: Internet
Author: User
Tags log log

Brief introduction:

Hessian is a simple binary protocol that connects Web services.

    • The client and server are not dependent on any other jar, which is much lighter than webservice, such as using Xfire, which contains the core library and the client jar, is 10M in size, and the latest hessian-4.0.7 jar size is less than 400K.
    • More suitable for binary transmission, than the Webservice.hessian transmission speed is higher than webservice.
    • Support Java,c#,flex (Actionscrpit)

Configuration:

Hessian access is divided into the client and the server, first of all to have Hessian jar package:

        <dependency>            <groupId>com.caucho</groupId>            <artifactid>hessian</artifactid >            <version>4.0.7</version>        </dependency>
First, add the Hessian dependency in the Pom to make sure that the jar file is available on both the client and server side.

Now let's look at how the server is configured, first of all the Web. xml file:

    <!--hessian provides remote services through a servlet, a matching pattern needs to be mapped to the Hessian service, and    <!--spring Dispatcherservlet can do this. Dispatcherservlet can forward requests for matching patterns to the Hessian service,    <!--web. XML simply defines a "request Forwarder" that will match the/remoting/* request intercept. The bean processing that is forwarded to the context.    -<!--and Hessianserviceexporter provides bean service. -    <servlet>        <servlet-name>remoting</servlet-name>        <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class>        <load-on-startup>1</ load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>remoting</ servlet-name>        <url-pattern>/remoting/*</url-pattern>    </servlet-mapping>
and spring integration, we no longer use this class:

<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
Here is the interception of all requests containing remoting, the name of the servlet is remoting, note the name, and use it later.

And see what this file Remoting-servlet.xml for?

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"    Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"    xmlns:context= "Http://www.springframework.org/schema /context "    xsi:schemalocation=" Http://www.springframework.org/schema/beans/           http Www.springframework.org/schema/beans/spring-beans-3.0.xsd           Http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd ">         <!--User Service--    < Bean name= "/userclientserviceremote" class= "Org.springframework.remoting.caucho.HessianServiceExporter" >        <property name= "service" ref= "Userclientserviceremote"/>        <property name= "Serviceinterface" value= " Com.darren.comm.client.user.service.UserClientService "/>    </bean></beans>

First in the Web-inf directory, we do not have to configure in other places to refer to this file, mainly by its name, it is the name of the Servet-name + servlet, then what is Servlet-name, is the above mentioned remoting, just, So its name is Remoting-servlet.xml.

The content of the file is to provide a bean for the client to use, ref= "Userclientserviceremote"/> This dependency is a bean injected after the spring scan.

Note: This bean has a slash in its name, unlike a normal bean.

Take a look at how the implementation class of the interface is configured:

Package Com.darren.back.client.user.service.impl;import Java.util.list;import Org.apache.commons.logging.Log; Import Org.apache.commons.logging.logfactory;import org.springframework.beans.factory.annotation.Autowired; Import Org.springframework.stereotype.component;import Com.darren.back.back.user.service.userservice;import Com.darren.comm.client.user.service.userclientservice;import Com.darren.comm.exception.businessexception;import Com.darren.comm.user.po.user;import com.darren.comm.vo.resulthandle;/** * User Remote Service Interface implementation * * @author Zhangpanfeng * */@Com Ponent ("Userclientserviceremote") public class Userclientserviceimpl implements Userclientservice {private static FINA    L Log log = Logfactory.getlog (Userclientserviceimpl.class);    /** * Inject User Service * * @Autowired private userservice userservice; Public resulthandle<list<user>> findallusers () {resulthandle<list<user>> ResultHandle = NE        W resulthandle<list<user>> ();      try {      List<user> userlist = Userservice.findallusers ();        Resulthandle.setcontent (userlist);            } catch (Businessexception e) {log.error ("error method <findAllUsers>");            Log.error (e);            Resulthandle.seterrorcode (E.geterrorcode ());        Resulthandle.setmessage (E.getmessage ());    } return resulthandle; }}
OK, so far, the server is ready, and then we'll see how the client accesses

The client introduces a const.properties configuration file that reads as follows:

Darren_back.serviceurl=http://localhost:9999/darren_back/remoting
Note:The port on the server here is 9999.

This URL contains remoting, so it will take the Hessian route.

The client needs to be equipped with a bean:

    <!--user interface--    <bean id= "Userclientservice" class= " Org.springframework.remoting.caucho.HessianProxyFactoryBean ">        <property name=" serviceurl "value=" ${ Darren_back.serviceurl}/userclientserviceremote "/>        <property name=" Serviceinterface "value=" Com.darren.comm.client.user.service.UserClientService "/>        <property name=" Chunkedpost "value=" false "/ >        <property name= "overloadenabled" value= "true"/>    </bean>
In this case, the client's controller can use this bean to access the contents of the server.

Package Com.darren.web.user.action;import Java.util.list;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Com.darren.comm.client.user.service.userclientservice;import Com.darren.comm.exception.businessexception;import Com.darren.comm.user.po.user;import Com.darren.comm.utils.stringutil;import Com.darren.comm.vo.ClientMessage; Import Com.darren.comm.vo.resulthandle;import com.darren.web.user.service.userservice;/** * User Controller * * @author    Zhangpanfeng * */@Controllerpublic class Useraction {@Autowired private userclientservice userclientservice; @RequestMapping (value = "/login") public String login (model model, user user) throws Businessexception {Clientm        Essage clientmessage = new Clientmessage ();        String target = "/home"; Resulthandle<list<user>> Resulthandle = Userclientservice.Findallusers ();        List<user> userlist = Resulthandle.getcontent ();            if (userlist! = null) {for (User u:userlist) {System.out.println (U);    }} return target; }}
Let's run a look at the results:

User [ID=135E1BD1-4801-447A-AEBB-D1D807C519FD, username=222, PASSWORD=BCBE3365E6AC95EA2C0343A2395834DD, CreateTime =thu Apr 14:07:36 gmt+08:00, Updatetime=null]user [Id=2c1214b4-ea09-42f9-9daa-5bf7de1edeaf, USERNAME=QQQ, password=b2ca678b4c936f905fb82f2733f5297f, Createtime=thu Apr 13:53:54 gmt+08:00, Updatetime=null]user [id= 485786f6-7689-4f72-8c34-1539e6e3b67d, username=111111, password=96e79218965eb72c92a549dd5a330112, CreateTime=Thu APR 13:42:41 gmt+08:00, Updatetime=null]user [id=b0cab9c2-201c-4fc0-bf65-e4a8a8bb004a, UserName=darren, password=96e79218965eb72c92a549dd5a330112, createtime=wed Apr 17:31:32 gmt+08:00, Updatetime=null]user [id= E284f6d0-b871-49e8-9806-6f0118172ff5, Username=aaa, password=47bce5c74f589f4867dbd57e9ca9f808, CreateTime=Thu APR 13:56:59 gmt+08:00, Updatetime=null]
This is the printed result information, to this walk through.

Missing a question, we need to look at the Resulthandle class and the user class

package Com.darren.comm.vo;import java.io.serializable;/** * Return object for Remote call * * @author Zhangpanfeng * * @param <T> */public cl     Resulthandle<t> implements Serializable {private static final long serialversionuid = -5396872858744255371l;    /** * Return information */private String message;    /** * ERROR code */Private String ErrorCode;    /** * Return content */private T content;    Public String GetMessage () {return message;    The public void Setmessage (String message) {this.message = message;    } public String GetErrorCode () {return errorCode;    } public void Seterrorcode (String errorCode) {this.errorcode = ErrorCode;    } public T GetContent () {return content;    } public void SetContent (T content) {this.content = content; }}
Package Com.darren.comm.user.po;import Com.darren.comm.base.po.baseentity;public Class User extends BaseEntity {priva    Te static final long serialversionuid = 8380375210393218806L;    /** * User ID */private String ID;    /** * User name */private String userName;    /** * Password * */private String password;    /** * Confirm Password */private String ConfirmPassword;    Public String GetId () {return id;    } public void SetId (String id) {this.id = ID;    } public String GetUserName () {return userName;    } public void Setusername (String userName) {this.username = UserName;    } public String GetPassword () {return password;    } public void SetPassword (String password) {this.password = password;    } public String Getconfirmpassword () {return confirmpassword;    } public void Setconfirmpassword (String confirmpassword) {This.confirmpassword = ConfirmPassword; } @Override PUblic String toString () {return "User [id=" + ID + ", username=" + UserName + ", password=" + password + ", create    Time= "+ Createtime +", updatetime= "+ UpdateTime +"] "; }}
Package Com.darren.comm.base.po;import Java.io.serializable;import java.util.date;/** * Base Entity class *  * @author Zhangpanfeng *  */public class BaseEntity implements Serializable {    private static final long Serialversionuid = 7 982965810132366752L;    /**     * Creation time */    protected date createtime;    /**     * Update time */    protected Date updatetime;    Public Date Getcreatetime () {        return createtime;    }    public void Setcreatetime (Date createtime) {        this.createtime = createtime;    }    Public Date Getupdatetime () {        return updatetime;    }    public void Setupdatetime (Date updatetime) {        this.updatetime = UpdateTime;    }}
These classes are serialized, and why Serialize, let's take a look at the role of serialization:

A) When you want to save the state of an object in memory in a file or in a database;
b) When you want to use sockets to transfer objects on the network;
c) When you want to transfer the object through the RMI (remote method invocation);
So it's going to be serialized, otherwise it will be an error.

Hessian integration with spring

Related Article

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.