Hessian(1)Spring整合Hessian

简介
     Hessian是一个序列化协议, 他的优点在于比Java原生的对象序列化/反序列化速度更快, 序列化出来以后的数据更小。
     Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。
     Hessian是一个二进制的web服务协议,可使用Hessian发送二进制数据,同时又具有防火墙穿透能力。
     Hessian一般是通过Web应用来提供服务,因此非常类似于平时我们用的 WebService。
     Hessian主要作面向对象的消息通信。
     Hessian服务通过接口暴露。

Spring中集成Hessian

一、server端

定义接口

public interface IGetMessage {
	public String getMsg(String key);
}
定义实现类

@Service("getMessageService") 
public class GetMessageImpl implements IGetMessage {
	SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
	public String getMsg(String key) {
		return key+" at:"+sdf.format(new Date());
	}
}
在spring配置文件applicationContext.xml增加对service的扫描

<context:component-scan base-package="com.sf.my.interfaces.*" />
新增hessian配置文件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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<bean name="getMessageExporter"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="getMessageService" />
		<property name="serviceInterface" value="com.sf.my.interfaces.IGetMessage" />
	</bean>

</beans>
web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<display-name>hessianWeb</display-name>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		classpath:applicationContext.xml
		classpath:Hessian-servlet.xml
		</param-value>
	</context-param>

	<servlet>
		<servlet-name>getMessageExporter</servlet-name>
		<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>getMessageExporter</servlet-name>
		<url-pattern>/remote/getMessageService</url-pattern>
	</servlet-mapping>
</web-app>
二、客户端

定义跟服务端一样的接口

package com.sf.my.interfaces;

public interface IGetMessage {

	public String getMsg(String key);

}
测试用例

public class Test {
	public static void main(String[] args) {
		String url = "http://localhost:8080/remote/getMessageService";
		HessianProxyFactory factory = new HessianProxyFactory();
		IGetMessage basic;
		try {
			basic = (IGetMessage) factory.create(IGetMessage.class, url);
			System.out.println("Message From Server: " + basic.getMsg("hello,my first hessian"));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}
与Spring整合,注入bean

   <bean id="getMessageService"
		class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="serviceUrl" value="${serviceUrl}" />
		<property name="serviceInterface" value="com.sf.my.interfaces.IGetMessage" />
	</bean>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值