`

Hessian 入门介绍

阅读更多

  Hessian,二进制Web服务协议。Hessian使得Web service的使用不需要重量级的框架,也不需要学习另一个soup协议。由于它是一个基于二进制协议的框架,所以它非常适合发送二进制数据而不需要扩展其它的协议。

  当然了,公司框架已经集成了Hessian,由于不是我集成的,所以,自己在家学习一下。

  我也是从网上搜罗了很多的资料以及拜读了许多前辈的博客才有了点眉目,今天写这一篇,主要是出于学习Hessian做笔记之用。在这篇博文里,主要介绍了Hessian的基本用法以及与Spring的集成用法:

  一、Hessian的基本用法:

     Hessian的官网上是这样介绍建立Hessian服务的步骤的:

       1)创建一个Java接口作为一个公共的API;

       2)使用HessianProxyFactory创建一个客户端;

       3)创建Service的实现类;

       4)配置Service在你的Servlet引擎中。

     通过上述步骤,我们可以看到,Hessian分为服务器端和客户端,Web Service嘛,必须的!OK,按照这个思路,我们把上述步骤分为服务器端和客户端来创建一个Hessian实例。

     服务器代码

        首先,建立一个公共的接口IGreeting:

 

public interface IGreeting {
	public 		String		sayHello();
//	public 		Car 		searchCarInfo(Car car);

}

       其次,通过继承HessianServlet创建IGreeting的实现类:

 

public class GreetingService extends HessianServlet implements IGreeting {
	private static final long serialVersionUID = 1L;

	private String greeting = "Hello,Hessian!";

	public String getGreeting() {
		return greeting;
	}

	public String sayHello() {
		return greeting;
	}
}

        这段代码也没有什么好说的,现在我们来把这个GreetingService配置到web.xml中:

 

	 <servlet>
	 	<servlet-name>Greeting</servlet-name>
	 	<servlet-class>org.hession.demo.impl.GreetingService</servlet-class>
	 </servlet>
	 <servlet-mapping>
	 	<servlet-name>Greeting</servlet-name>
	 	<url-pattern>/Greeting</url-pattern>
	 </servlet-mapping>

      OK,上述的四个Hessian创建步骤,我们现在只差通过HessianProxyFactory这个类创建一个客户端了。

    客户端代码

 

		String url = "http://localhost:8080/HessianDemo/Greeting";
		HessianProxyFactory factory = new HessianProxyFactory();
		IGreeting greeting = (IGreeting) factory.create(IGreeting.class,url);
		System.out.println(greeting.sayHello());

   把项目布署到Tomcat,启动Tomcat,运行就可看到我想到的结果了。

 二、Hessian与Spring的集成:

    相关的jar包我就不再罗列了,有兴趣的朋友,可以下载附件源码。

    服务器端代码

    Hessian是通过Spring的HTTP请求处理器导出指定的服务bean为一个Hessian服务,通过Hessian代理访问。这个导出Hessian服务的导出器类为:org.springframework.remoting.caucho.HessianServiceExporter

   服务器端的接口和实现代码我就不再罗列了,现列出remoting-servlet.xml的服务配置代码:

 

    <!--  使用HessianServiceExporter 将普通bean导出成Hessian服务--> 
   <bean name="/userservice" 
    	class="org.springframework.remoting.caucho.HessianServiceExporter">
	      <!--  需要导出的目标bean-->
	    <property name="service">
	      <ref bean="userservicetarget"/>
	    </property>
	      <!--  Hessian服务的接口-->      
	    <property name="serviceInterface">
	      <value>org.hession.demo.IUserService</value>
	    </property>
    </bean>

   可以看到,HessianServiceExporter通过指定service和serviceInterface属性,从而达到将普通类导出为Web Service的目的。

   service导出的目标bean为在applicationContext.xml中配置的实现类:org.hession.demo.impl.UserService,在此不再说明喽。

   服务器端Spring的web.xml配置可参见源代码。

   客户端代码

   客户端的代码也是通过Spring配置来完成的。

   首先呢,Client.properties里面配置了服务器端的URL信息,现罗列如下:

 

# Properties file with server URL settings for remote access.
serverName=localhost
httpPort=8080
serverPath=HessianDemo
contextPath=remoting/userservice

   在第一点中,创建Hessian的客户端通过指定服务的URL和指定的接口,通过HessianProxyFactory创建指定的服务类,在此贴出Spring客户端applicationContext_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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 					http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      		<property name="location" value="Client.properties"></property>
       </bean>
 	<bean id="proxyuserservice" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
	        <property name="serviceUrl">
	          	<value>http://${serverName}:${httpPort}/${serverPath}/${contextPath}</value>
	        </property>
	    	<property name="serviceInterface">
	      		<value>org.hession.demo.IUserService</value>
	    	</property>
  	   </bean>
       <bean id="hessinaclient" class="org.hession.demo.test.HessianClient">
	       	<property name="userSerivce">
	       		<ref local="proxyuserservice"/>
	       	</property>
       </bean>
</beans>

    HessianProxyFactoryBean通过指定serviceUrl和serviceInterface属性,创建一个远程服务到本地,HessianClient这个类呢,就是客户端代码了。

   现看看客户端HessianTest测试代码如下所示:

 

	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_client.xml");
	HessianClient hessian = (HessianClient) ctx.getBean("hessinaclient");
	System.out.println("hessinaclient:" + hessian.getUsernames());

  OK,Hessian的学习到此为止,搞定喽,收工!

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics