hessian技术的使用
2014-03-12 15:03
197 查看
一、为什么选择hessian?
在工作中有这么一种情况,两个开发团队在为同一个项目服务时,虽然功能模块不同,但需要一个团队为另一个团队提供数据,发布时只能将项目整合在一起,这样就存在让人揪心的问题,(开发环境不同,jar包冲突问题......等等),这时可以选择使用hessian。
1、hessian类型与webservice,但比webservice简单N倍
2、使用hessian需要考虑以下几点:
服务端
1)、 项目中需要包含Hessian的jar包
2)、设计一个接口,用来给客户端调用
3)、实现该接口的功能
4)、配置web.xml,配好相应的servlet
5)、对象必须实现Serializable 接口
客户端
1)、java客户端包含Hessian.jar的包
2)、具有和服务器端结构一样的接口,包括命名空间
3)、利用HessianProxyFactory调用远程接口
测试代码如下:
服务端:
新建项目hessian_test、导入hessian.jar包
创建接口类
package com.hessian.interfaces;
public interface Hessiantest {
String getData();
}
接口实现类
package com.hessian.interfaces.impl;
import com.hessian.interfaces.Hessiantest;
public class HessiantestImpl implements Hessiantest{
public String getData() {
// TODO Auto-generated method stub
return "test......";
}
}
web.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>hessian_test</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<!-- 配置接口实现类 -->
<param-value>com.hessian.interfaces.impl.HessiantestImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian_test</servlet-name>
<url-pattern>/hessian_test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
客户端:
package com.test;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.hessian.interfaces.Hessiantest;
public class Test {
public static void main(String[] args) throws MalformedURLException {
String url ="http://localhost:8080/hessian_test/hessian_test";
HessianProxyFactory factory = new HessianProxyFactory();
Hessiantest hessiantest=(Hessiantest) factory.create(Hessiantest.class, url);
System.out.println("运行结果:"+hessiantest.getData());
}
}
运行结果如下:
运行结果:test......
简单的项目调用基本就OK了,后续用到深层次会继续编写。
在工作中有这么一种情况,两个开发团队在为同一个项目服务时,虽然功能模块不同,但需要一个团队为另一个团队提供数据,发布时只能将项目整合在一起,这样就存在让人揪心的问题,(开发环境不同,jar包冲突问题......等等),这时可以选择使用hessian。
1、hessian类型与webservice,但比webservice简单N倍
2、使用hessian需要考虑以下几点:
服务端
1)、 项目中需要包含Hessian的jar包
2)、设计一个接口,用来给客户端调用
3)、实现该接口的功能
4)、配置web.xml,配好相应的servlet
5)、对象必须实现Serializable 接口
客户端
1)、java客户端包含Hessian.jar的包
2)、具有和服务器端结构一样的接口,包括命名空间
3)、利用HessianProxyFactory调用远程接口
测试代码如下:
服务端:
新建项目hessian_test、导入hessian.jar包
创建接口类
package com.hessian.interfaces;
public interface Hessiantest {
String getData();
}
接口实现类
package com.hessian.interfaces.impl;
import com.hessian.interfaces.Hessiantest;
public class HessiantestImpl implements Hessiantest{
public String getData() {
// TODO Auto-generated method stub
return "test......";
}
}
web.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>hessian_test</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>service-class</param-name>
<!-- 配置接口实现类 -->
<param-value>com.hessian.interfaces.impl.HessiantestImpl</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian_test</servlet-name>
<url-pattern>/hessian_test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
客户端:
package com.test;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
import com.hessian.interfaces.Hessiantest;
public class Test {
public static void main(String[] args) throws MalformedURLException {
String url ="http://localhost:8080/hessian_test/hessian_test";
HessianProxyFactory factory = new HessianProxyFactory();
Hessiantest hessiantest=(Hessiantest) factory.create(Hessiantest.class, url);
System.out.println("运行结果:"+hessiantest.getData());
}
}
运行结果如下:
运行结果:test......
简单的项目调用基本就OK了,后续用到深层次会继续编写。
相关文章推荐
- spring技术内幕18-Spring使用Hessian实现远程调用
- 在Spring中使用Hessian Remoting技术
- Hessian技术在java中的使用
- 在Spring中使用Hessian Remoting技术-------实例附件源码
- 在Spring中使用Hessian Remoting技术
- 在Spring中使用Hessian Remoting技术
- 在Spring中使用Hessian Remoting技术。
- oracle技术之在查询列表中使用PRIOR操作
- VB6.0中用户类和数据源类的设计和使用技术
- 学习&使用技术的四种层次
- 使用缓存技术提高效率
- 【技术】使用原生XHR(XMLHttpRequest)对象来获取并包括HTML片段
- 使用Ext JS技术拖动树结点
- 使用TCP协议的NAT穿透技术 (转)
- [C#技术] .NET平台开源JSON库LitJSON的使用方法(转载)
- 求助使用Urlrewrite技术实现Struts2+Hibernate3+Spring的项目的伪静态的方法
- 推荐系统中所使用的混合技术介绍
- Android Bluetooth蓝牙技术使用流程详解
- Java技术_Java千百问(0017)_if else如何使用
- InstallShield使用——几个技术问题介绍