Hession是一个二进制的web service 协议,因为是基于二进制,所以我们不需要学习协议,或者编写大量的框架,sub代码来提供RPC调用。
Hessian通过Servlet提供远程服务。需要将匹配某个模式的请求映射到Hessian服务。Spring的DispatcherServlet可以完成该功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务。Hessian的server端提供一个servlet基类, 用来处理发送的请求,而Hessian的这个远程过程调用,完全使用动态代理来实现的,推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。
Hessian 可以通过两种方式来使用:
1.原生Hessian
下载Hessian包,http://jarfiles.pandaidea.com/hessian.html
然后新建一个web工程,将hessian-4.0.37.jar包拷贝到WEB-INF/lib目录下。
新建一个interface和实现类:
public interface MyService {
public void sayHelloWorld();
}
public class MyServiceImpl implements MyService{
@Override
public void sayHelloWorld() {
System.out.println("hello world!");
}
}
在web.xml,添加如下配置:
<servlet>
<servlet-name>MyService</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.hujun.hession.service.impl.MyServiceImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.hujun.hession.service.impl.MyService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyService</servlet-name>
<url-pattern>/MyService/*</url-pattern>
</servlet-mapping>
这样服务端的程序就编写好了,是不是很简单啊,将web程序部署到tomcat。
开始编写client
public class MyServiceClient {
public static void main(String[] args) {
try {
HessianProxyFactory hessianProxy = new HessianProxyFactory();
hessianProxy.setReadTimeout(5000);
//hessianProxy.setOverloadEnabled(true);
MyService service0 = (MyService)hessianProxy.create(MyService.class, "http://127.0.0.1/MyService");
service0.sayHelloWorld();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
输入如下:
hello world!
2.通过spring+hession的方式来使用。
spring 通过dispatcherServlet来转发请求给HessianServiceExporter,HessianServiceExporter将一个bean转换成服务。
新建web工程,将spring和hession相关的jar包放到WEB-INF/lib目录下,服务端的代码还是和上面一样,只是要改下web.xml配置:
<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>
并增加remoting-servlet.xml(注意,这里的xml文件命令有限制,前缀必须和web.xml中servlet-name的名字相同)
<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 定义普通bean实例 -->
<bean id="serviceImpl" class="com.hujun.hession.service.impl.MyServiceImpl" />
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
<bean name="/hessianCommentService"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要导出的目标bean -->
<property name="service" ref="serviceImpl" />
<!-- Hessian服务的接口 -->
<property name="serviceInterface" value="com.hujun.hession.service.MyService" />
</bean>
</beans>
服务端编写完毕,现在开始写客户端的代码,因为将hession托管给spring管理,所以,需要在客户端新建remoting-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="myServiceClient"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost/remoting/hessianCommentService</value>
</property>
<property name="serviceInterface">
<value>com.hujun.hession.service.MyService</value>
</property>
</bean>
</beans>
注意,这里http://localhost/remoting/hessianCommentService的后缀必须和在服务端配置的HessianServiceExporter这个bean的名称一样。
接下来通过代码 来访问:
public class MyServiceClient {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("remoting-client.xml");
MyService myService = (MyService)ac.getBean("myServiceClient");
myService.sayHelloWorld();
}
}