了解Hessian
Hessian是远程调用的一种技术,和WebService类似,但不同的是较WebService而言,它更轻量级,更简单,更快速。关于Hessian更详细全面的介绍可以查看http://hessian.caucho.com/。下面可以花十五分钟的时间,学会怎么简单的使用Hessian。
使用Hessian
在Eclipse建立一个Maven webapp项目:
右键项目添加Hessian的依赖:
Maven随后会去中央仓库下载Hessian相关的jar包。
添加一个提供Hessian服务的接口:
1: /**2: * @ClassName: HessianService3: * @Description: Hessian服务接口4: * @author liping.action@gmail.com5: * @date 2014-1-3 下午10:41:266: *7: */8: public interface HessianService {9: public String sayHello(String name);10: }
以及现实类:
1: public class HessianServiceImpl implements HessianService {2: /*3: * (non-Javadoc)4: *5: * @see6: * com.cnblogs.leefreeman.hessian.service.HessianService#sayHello(java.lang7: * .String)8: */9: @Override
10: public String sayHello(String name) {11: // TODO Auto-generated method stub12: return "hello " + name;13: }
14: }
最后在web.xml中增加hessian相应的配置:
1: <servlet>2: <servlet-name>service</servlet-name>3: <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>4: <load-on-startup>1</load-on-startup>5: <init-param>6: <param-name>service-class</param-name>7: <param-value>com.cnblogs.leefreeman.hessian.service.impl.HessianServiceImpl</param-value>8: </init-param>9: </servlet>10: <servlet-mapping>11: <servlet-name>service</servlet-name>12: <url-pattern>/service</url-pattern>13: </servlet-mapping>
现在Hessian服务端就算完成,是不是觉得很简单。启动项目:
打开该页面说明Hessian服务端启动正常。接口下来编写客户端测试用例:
1: public class HessianServiceTest {
2: private final static String SERVICE_URL = "http://localhost:8080/hessian-project/service";
3:
4: @Test
5: public void testSayHello() {
6: HessianProxyFactory proxyFactory = new HessianProxyFactory();
7: try {
8: HessianService hessianService = (HessianService) proxyFactory
9: .create(HessianService.class, SERVICE_URL);
10: System.out.println(hessianService.sayHello("liping"));
11: } catch (MalformedURLException e) {
12: e.printStackTrace();
13: }
14: }
15: }
结果:
一个简单的Hession应用就完成了,相比搭建Webservice项目,要简单得多。