Hessian介绍
来源:互联网 发布:mac os x leopard下载 编辑:程序博客网 时间:2024/06/07 01:36
Hessian介绍
相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。下面演示一个简单的Hessian示例程序。
14.5.1.1 Hessian的下载和安装
Hessian的下载和安装请按如下步骤进行:
(1)登陆http://www.caucho.com/hessian/下载Hessian的Java二进制包,笔者成书之时,Hessian的最新版本是Hessian 3.0.13。下载hessian-3.0.13.jar文件。
(2)将该文件复制到名为hessian的Web应用下,所有的jar文件都应该放在WEB-INF/lib下,该文件也不例外。
(3)为了编译Hessian客户端程序,建议将hessian-3.0.13.jar添加到环境变量里。
14.5.1.2 Hessian服务器端
推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。服务接口如下:
//服务接口
public interface Hello
{
//方法声明
public String hello(String name);
}
接口的实现类如下:
//服务实现类,实现Hello接口
public class HelloImpl implements Hello
{
public String hello(String name)
{
return "hello " + name + "欢迎学习Hessian";
}
}
这个接口和实现类简单得难以置信。它们没有任何特别之处,这正是Hessian的魅力,代码污染降低到最小。当然,只是示例程序,所以服务也相当简单。Hessian要求远程服务通过Servlet暴露出来,必须在web.xml文件中配置该Servlet。web.xml的详细配置如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Web配置文件的文件头,包含dtd等信息-->
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- Web配置文件的根元素-->
<web-app>
<servlet>
<!-- 配置Servlet名,后面根据该名完成远程服务映射-->
<servlet-name>hessianService</servlet-name>
<!-- Hessian远程服务需要HessianServlet暴露Hehes-->
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<!-- 随应用启动而启动>
<load-on-startup>1</load-on-startup>
<!-- 使用init-param配置服务的实现类-->
<init-param>
<param-name>service-class</param-name>
<param-value>lee.HelloImpl</param-value>
</init-param>
</servlet>
<!-- 映射Servlet的url,该Servlet的url就是Hessian服务名-->
<servlet-mapping>
<servlet-name>hessianService</servlet-name>
<!-- 远程服务名为hessianService-->
<url-pattern>/hessianService</url-pattern>
</servlet-mapping>
</web-app>
将刚才的接口和实现放Web应用的WEB-INF/class路径下,编译它们。然后将此web.xml文件放在WEB-INF下,启动Web服务器。笔者使用的Web服务器是Tomcat5.0.28,Tomcat的端口是8888。则远程服务的url为:http://localhost:8888/hessian/hessianService。
14.5.1.3 Hessian客户机端
Hessian的服务可以用HessianProxyFactory工具类调用,还可以在小型智能设备上调用。HessianProxyFactory的create方法,用于获取Hessian服务的远程引用。Hessian的客户端如下:
public class HessianClient
{
public static void main(String []args) throws Exception
{
//Hessian服务的url
String url = "http://localhost:8888/hessian/hessianService";
//创建HessianProxyFactory实例
HessianProxyFactory factory = new HessianProxyFactory();
//获得Hessian服务的远程引用
Hello d = (Hello) factory.create(Hello.class, url);
//调用远程服务。
System.out.println("下面调用Hessian服务: " + d.hello("yeeku"));
}
}
客户端仅仅需要Hello接口,而无须真实的实现类。如果使用小型智能设备作为客户端的运行环境,客户端代码片段如下:
//创建Hessian输入流,用于输入请求
MicroHessianInput in = new MicroHessianInput();
//Hessian服务的url
String url = "http://localhost:8888/hessian/hessianService";
//创建HttpSConnection实例
HttpConnection c = (HttpConnection) Connector.open(url);
//设置参数提交方式
c.setRequestMethod(HttpConnection.POST);
//打开输出流,准备调用服务器方法
OutputStream os = c.openOutputStream();
//以输出流创建MicroHessianOutput对象,该对象用于调用hessian的方法
MicroHessianOutput out = new MicroHessianOutput(os);
//调用远程方法:hello是方法名,yeeku是参数
out.call("hello", “yeeku”);
os.flush();
//打开输入流,准备接收返回值
is = c.openInputStream();
以输入流为参数,创建MicroHessianInput对象
MicroHessianInput in = new MicroHessianInput(is);
获得返回值
Object value = in.readReply(“yeeku”);
为Hessian装配DispatcherServlet
通过上面的介绍,可以看出:Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。
在web.xml文件中配置DispatcherServlet拦截所有的远程服务请求,当然需要将DispatcherServlet配置成load-on-startup Servlet。即在web.xml文件中增加如下两段
<!-- 将DispatcherServlet 配置成load-on-startup servlet-->
<servlet>
<!-- Servlet name-->
<servlet-name>remoting</servlet-name>
<!-- Serlvet实现类-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 定义Servlet url映射-->
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<!-- 所有匹配/remoting/*的请求被DispatcherServlet 截获-->
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
根据DispatcherServlet的源代码可以知道,DispatcherServlet拦截了/remote/*请求,则需要加载remoting-servlet.xml的配置文件。同时,为了在应用启动时创建ApplicationContext,则应使用ContextLoaderListener来加载Spring。修改后的web.xml文件详细代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Web配置文件的文件头,包含dtd信息 -->
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- Web配置文件的根元素-->
<web-app>
<!-- 强制指定Spring配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/remoting-servlet.xml</param-value>
</context-param>
<!-- 使用ContextLoaderListener保证启动时加载Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 将DispatcherServlet 配置成load-on-startup Servlet-->
<servlet>
<servlet-name>remoting</servlet-name>
<!-- Serlvet实现类-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 定义Servlet url映射-->
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
</web-app>
至此,依然看不出Hessian远程服务的影子,只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获,转发给context的bean处理。因此,下一步在context定义bean。
使用HessianServiceExporter提供bean服务
根据上面的描述,必须提供remoting-servlet.xml文件。通常,在remoting-servlet.xml文件里定义Hessian服务即可。Spring使用HessianServiceExporter,将一个常规bean导出成Hessian服务。类似于RmiServiceExporter,HessianServiceExporter可将一个普通bean导出成远程服务。remoting-servlet.xml的详细配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的文件头,包含dtd信息-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定义普通bean实例-->
<bean id="helloService" class="lee.HelloImpl"/>
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
<bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要导出的目标bean-->
<property name="service" ref="helloService"/>
<!-- Hessian服务的接口-->
<property name="serviceInterface" value="lee.Hello"/>
</bean>
</beans>
在该配置下,Hessian服务的url是http://localhost:8888/hessian-spring/remoting/helloService,其中hessian-spring是该应用的url。Spring使用DispatcherServlet拦截到匹配/remoting/*的请求,然后将该请求转发到对应的bean,该bean在remoting-servlet.xml文件中以HessianServiceExporter定义。
使用HessianProxyFactoryBean连接Hessian服务
Spring提供的Hessian服务是标准服务。因此,完全可以使用前面的客户端程序来访问,只需要修改url为http://localhost:8888/hessian-spring/remoting/helloService即可。
为了充分利用IoC特性,Spring还提供HessianProxyFactoryBean连接Hessian服务,HessianProxyFactoryBean用于连接Hessian服务,类似所有的FactoryBean,对它的请求不会返回实例本身, 而是它生成的实例。配置HessianProxyFactoryBean bean时候,只需要指定Hessian服务的url,以及Hessian服务实现的接口。详细的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的文件头,包含dtd信息-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Spring配置文件的根元素-->
<beans>
<!-- 依赖于远程服务的测试bean-->
<bean id="test" class="lee.Test">
<!-- 配置依赖注入-->
<property name="hello">
<ref local="helloService"/>
</property>
</bean>
<!-- 使用HessianProxyFactoryBean 连接远程Hessian服务-->
<bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!-- 确定Hessian服务的url-->
<property name="serviceUrl" value="http://localhost:8888/hessian-spring/remoting/helloService"/>
<!-- 确定Hessian服务实现的接口-->
<property name="serviceInterface" value="lee.Hello"/>
</bean>
</beans>
Test是测试bean,该bean依赖于helloService bean,其源代码如下:
public class Test
{
//依赖实例
private Hello hello;
//依赖注入的setter方法
public void setHello(Hello h)
{
this.hello = h;
}
public void test(String name)
{
System.out.println(hello.hello(name));
}
}
与RmiServiceExporter类似,Test的代码中没有丝毫远程访问的痕迹。Test完全不用理会Hello的底层实现。Test可以在远程服务的不同协议之间切换,甚至可以在远程服务和本地服务之间切换。
- Hessian 介绍
- Hessian介绍
- Hessian介绍
- Hessian介绍
- Hessian介绍
- Hessian介绍及使用说明
- Hessian RMI功能介绍
- hessian 的简单介绍
- hessian 的简单介绍
- Hessian的使用介绍
- Hessian的使用与介绍
- Hessian远程调用框架进阶教程(一) Hessian介绍JAVA使用Hessian进行编程实例
- Hessian
- hessian
- Hessian
- Hessian
- hessian
- Hessian
- 解决ActiveX部件不能创建对象的有效方法
- Android原生(Native)C(JNI/NDK)开发之二:framebuffer篇
- const 函数调用类的非const成员函数报错
- poj 2081 :Recaman's Sequence
- Control的Invoke和BeginInvoke 区别
- Hessian介绍
- 目录扫描printfdir
- 低调做人,中调做事
- 在uboot中添加新的命令
- 在freescale平台下,添加一个GPIO驱动(power key)
- fastboot命令驱动修改(uboot-imx/drivers/fastboot/fastboot.c)
- 二分搜索算法
- [usaco] Cow Pedigrees
- HDU 1022 Train Problem I