目录

spring整合hessian

hessian介绍

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

加入pom依赖

pom.xml
<dependency>
	<groupId>com.caucho</groupId>
	<artifactId>hessian</artifactId>
	<version>4.0.38</version>
</dependency>

web.xml

web.xml
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		classpath*:/hessian-servlet.xml
	</param-value>
</context-param>
 
<!-- hessian -->
<servlet> 
	<servlet-name>hessian</servlet-name> 
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
	<load-on-startup>4</load-on-startup> 
</servlet> 
 
<servlet-mapping> 
	<servlet-name>hessian</servlet-name> 
	<url-pattern>/hessian/*</url-pattern> 
</servlet-mapping> 

hessian-servlet.xml

hessian-servlet.xml
<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 
	<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
	<bean id="rmiService" class="com.gxx.record.service.impl.RmiServiceImpl"/> 
	<bean name="/rmi" class="org.springframework.remoting.caucho.HessianServiceExporter"> 
		<property name="service" ref="rmiService"/> 
		<property name="serviceInterface" value="com.gxx.record.service.RmiService"/> 
	</bean>
</beans>

RmiRequest.java

RmiRequest.java
package com.gxx.record.dto;
 
import java.io.Serializable;
 
/**
 * 
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	RMI远程调用请求
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年8月2日
 * @since record
 *
 */
public class RmiRequest implements Serializable {
	private static final long serialVersionUID = 8407796832554234397L;
	public String businessCode;//业务代码
	public String name;//姓名
	public String password;//密码
	public String getBusinessCode() {
		return businessCode;
	}
	public void setBusinessCode(String businessCode) {
		this.businessCode = businessCode;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

RmiResponse.java

RmiResponse.java
package com.gxx.record.dto;
 
import java.io.Serializable;
 
/**
 * 
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	RMI远程调用返回
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年8月2日
 * @since record
 *
 */
public class RmiResponse implements Serializable {
	private static final long serialVersionUID = -1241624069216870728L;
	public boolean isSuccess;//是否成功
	public String message;//信息
	public boolean isMutiple;//是否多条
	public boolean isSuccess() {
		return isSuccess;
	}
	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public boolean isMutiple() {
		return isMutiple;
	}
	public void setMutiple(boolean isMutiple) {
		this.isMutiple = isMutiple;
	}
}

RmiService.java

RmiService.java
package com.gxx.record.service;
 
import com.gxx.record.dto.RmiRequest;
import com.gxx.record.dto.RmiResponse;
 
/**
 * 
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	RMI远程调用接口
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年8月2日
 * @since record
 *
 */
public interface RmiService {
	/**
	 * 远程调用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public RmiResponse process(RmiRequest request) throws Exception;
}

RmiServiceImpl.java

RmiServiceImpl.java
package com.gxx.record.service.impl;
 
import org.apache.commons.lang3.StringUtils;
 
import com.gxx.record.dto.RmiRequest;
import com.gxx.record.dto.RmiResponse;
import com.gxx.record.service.RmiService;
 
/**
 * 
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	RMI远程调用接口实现类
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年8月2日
 * @since record
 *
 */
public class RmiServiceImpl implements RmiService {
	public static final String BUSINESS_CODE_CHECK_USER_PASSWORD = "CHECK_USER_PASSWORD";
 
	/**
	 * 远程调用
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public RmiResponse process(RmiRequest request) throws Exception {
		RmiResponse response = new RmiResponse();
		if(StringUtils.equals(request.getBusinessCode(), BUSINESS_CODE_CHECK_USER_PASSWORD)){
			if(StringUtils.equals("gxx", request.getName())){
				if(StringUtils.equals("123456", request.getPassword())){
					response.setSuccess(true);
					response.setMessage("密码验证成功");
				} else {
					response.setSuccess(false);
					response.setMessage("该用户名[" + request.getName() + "]密码有误");
				}
			} else {
				response.setSuccess(false);
				response.setMessage("不存在该用户名[" + request.getName() + "]");
			}
		} else {
			response.setSuccess(false);
			response.setMessage("该业务代码[" + request.getBusinessCode() + "]无效");
		}
		return response;
	}
 
}

RmiServiceTest.java(测试单元调用)

RmiServiceTest.java
package com.gxx.record.rmi;
 
import junit.framework.TestCase;
 
import com.caucho.hessian.client.HessianProxyFactory;
import com.gxx.record.dto.RmiRequest;
import com.gxx.record.dto.RmiResponse;
import com.gxx.record.service.RmiService;
 
/**
 * 
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	远程调用测试类
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年8月2日
 * @since record
 *
 */
public class RmiServiceTest extends TestCase {
	/**
	 * 测试RMI
	 * @throws Exception
	 */
	public void testRmi() throws Exception{
		String url = "http://localhost/record/hessian/rmi";
		HessianProxyFactory factory = new HessianProxyFactory();
		RmiService service =  (RmiService)factory.create(RmiService.class, url);
		RmiRequest request = new RmiRequest();
		request.setBusinessCode("CHECK_USER_PASSWORD");
		request.setName("gxx");
		request.setPassword("123456");
		RmiResponse response =  service.process(request);
		System.out.println("结果:" + response.isSuccess() + ",信息:" + response.getMessage());
	}
}

application-hessian-client.xml(spring注入调用)

application-hessian-client.xml
<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 
	<bean id="rmiService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl" value="http://localhost/record/hessian/rmi"/> 
		<property name="serviceInterface" value="com.gxx.record.service.RmiService"/> 
	</bean>
 
</beans>

RmiController.java

RmiController.java
package com.gxx.record.web.rmi;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.gxx.record.dto.RmiRequest;
import com.gxx.record.dto.RmiResponse;
import com.gxx.record.service.RmiService;
 
/**
 * RmiController
 * 
 * @author gxx
 */
@Controller
@RequestMapping("/rmi/")
public class RmiController {
	/**
	 * 日志处理器
	 */
	private final Logger logger = Logger.getLogger(RmiController.class);
 
	@Autowired
	private RmiService rmiService;
 
	@RequestMapping(value = "/preRmiFtl", method = RequestMethod.GET)
	public String preRmiFtl() {
		return "rmi/preRmiFtl";
	}
 
	/**
	 * 远程调用
	 * @param request
	 * @param rmiRequest
	 * @return
	 */
	@RequestMapping(value = "/process",produces="application/json")
	public @ResponseBody RmiResponse process(HttpServletRequest request, RmiRequest rmiRequest) throws Exception {
		logger.info("远程调用:功能号=[" + rmiRequest.getBusinessCode() + "],用户名=[" + rmiRequest.getName() + "],"
				+ "密码=[" + rmiRequest.getPassword() + "]");
		return rmiService.process(rmiRequest);
	}
}

preRmiFtl.ftl

preRmiFtl.ftl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>rmi页面</title>
	</head>
	<body>
		<form action="/record/rmi/process.htm" method="post">
			<table border="1">
				<tr><td>功能号:</td><td><input type="text" name="businessCode" value="CHECK_USER_PASSWORD"/></td></tr>
				<tr><td>姓名:</td><td><input type="text" name="name" value="gxx"/></td></tr>
				<tr><td>密码:</td><td><input type="text" name="password" value="123456"/></td></tr>
				<tr><td colspan="2" align="center"><input type="submit" value="校验用户名"/></td></tr>
			</table>
		</form>
	</body>
</html>

演示