`
raymond.chen
  • 浏览: 1419247 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

如何在Spring中集成Hessian框架

阅读更多

一、简介 
     Hessian是一个序列化协议, 他的优点在于比Java原生的对象序列化/反序列化速度更快, 序列化出来以后的数据更小。

     Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。

     Hessian是一个二进制的web服务协议,可使用Hessian发送二进制数据,同时又具有防火墙穿透能力。

     Hessian一般是通过Web应用来提供服务,因此非常类似于平时我们用的 WebService。

     Hessian主要作面向对象的消息通信。

     Hessian服务通过接口暴露。

 

下面以hessian-3.0.20版本为例演示如何将Hessian整合到Spring中。

二、配置详解 
     1、在web.xml中的配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/config/applicationContext.xml,
        /WEB-INF/Hessian-servlet.xml
    </param-value>
</context-param>
		
<servlet>
    <servlet-name>Hessian</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
		
<servlet-mapping>
    <servlet-name>Hessian</servlet-name>
    <url-pattern>/hessian/*</url-pattern>
</servlet-mapping>


       1)Hessian要求远程服务通过Servlet暴露出来,所以我们使用Spring的DispatcherServlet来暴露我们的服务。
        2)我们必须在WEB-INF目录下创建一个文件名格式为 [Servlet Name]-servlet.xml 的配置文件,由于我们设定servlet-name为Hessian,所以我们在这里创建一个名为Hessian-servlet.xml的文件。

    2、Hessian-servlet.xml文件的配置

<!-- 业务类 -->
<bean id="hessianService" class="com.cjm.webservice.hessian.HessianServiceImpl"/>
		
<!-- 远程服务 -->
<bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="hessianService"/>
    <property name="serviceInterface">
        <value>
            com.cjm.webservice.hessian.HessianService
        </value>
    </property>
</bean>


        1)实际业务类是通过Spring的HessianServiceExporter类来暴露给客户端的。
        2)service:指定服务对应的业务类。
        3)serviceInterface:指定业务类实现哪个接口。Spring推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。
        4)Hessian的远程服务名为/hessianService。笔者使用的web服务器是Tomcat-5.5.23,端口是8888,web应用名为spring2,则远程服务的URL为:http://localhost:8888/spring2/hessian/hessianService

    3、业务类源代码

//接口类:
public interface HessianService {
    public String sayHello(String username);
    public HessianModel getHessianModel(String username, String password);
}
		
//实现类:
public class HessianServiceImpl implements HessianService {
    public String sayHello(String username){
        return "Hello " + username;
    } 

    public HessianModel getHessianModel(String username, String password) {
        return new HessianModel(username, password);
    }
}
		
//领域模型类:
public class HessianModel implements Serializable{
    private String username;
    private String password;
				
    public HessianModel(String username, String password){
        this.username = username;
        this.password = password;
    }
    ……
}



    4、客户端调用服务范例
         1)在Jsp页面中调用服务

 String url = "http://localhost:8888/spring2/hessian/hessianService";
 HessianProxyFactory factory = new HessianProxyFactory();
 HessianService hessianServer = 
				(HessianService)factory.create(HessianService.class, url);
 String ret = hessianServer.sayHello("Raymond.chen");
 out.print(ret);
			
 HessianModel model = hessianServer.getHessianModel("uid", "pwd");
 out.print("username: " + model.getUsername() + "<br>");


             A)结果显示:Hello Raymond.chen
             B)客户端程序必须引用hessian-3.0.20.jar文件和远程服务对应的接口类。
             C)当调用的远程服务方法返回一个自定义类时,该自定义类必须实现Serializable接口。

       2)在Spring环境中调用服务

<bean id="testHessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://localhost:8888/spring2/hessian/hessianService"/>
    <property name="serviceInterface" value="com.cjm.webservice.hessian.HessianService"/>
</bean>
			
<!- Struts2中调用服务 -->
<bean id="orgAction" class="com.cjm.web.action.OrganizationAction" parent="baseAction">
    <property name="organizationService" ref="organizationService"/>
    <property name="testHessianService" ref="testHessianService"/>
</bean>



    OrganizationAction.java部分源代码:

 private HessianService testHessianService;
				
 HessianModel model = testHessianService.getHessianModel("uid", "pwd");
 System.out.println("username: " + model.getUsername());


        A)使用HessianProxyFactoryBean来连接服务。
        B)serviceUrl:远程服务的URL。
        C)serviceInterface:服务对应的接口类。

 

14
0
分享到:
评论
3 楼 zhaojun900128 2014-03-13  
      
2 楼 spring00 2012-10-09  
mark一下
1 楼 xiejx618 2011-10-21  
web.xml的spring mvc配置不够灵活,spring的配置文件放在web-inf下不方便使用junit测试。
使用contextConfigLocation可指定spring配置文件的位置。


<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-server.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

相关推荐

    hessian学习实例

    hessian学习实例,hessian框架例子,与Spring集成。包括了server端和client端

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Hessian 使用小结

    Hessian 是一种轻量级的二进制RPC通讯框架,基于HTTP使用servlet 暴漏web service. 本文描述了单独使用和及spring集成使用,并介绍了证书加密,签名及非证书类如DES加密方式. 文尾附官方实例链接.

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    注意:经常被使用的的Spring的DispatcherServlet也是Spring Web MVC框架中的一部分。因此,就算你只是为了远程访问(例如,暴露Hessian或者 HTTP调用服务)而使用DispatcherServlet,你也得将'spring-webmvc.jar'...

    最新最全的spring开发包

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    Spring in Action(第二版 中文高清版).part2

    第9章 在Spring中建立契约优先Web服务 9.1 介绍Spring-WS 9.2 定义契约(首先!) 9.3 使用服务端点处理消息 9.3.1 建立基于JDOM消息的端点 9.3.2 序列化消息载荷 9.4 合并在一起 9.4.1 Spring-WS:全景视图 ...

    Spring in Action(第二版 中文高清版).part1

    第9章 在Spring中建立契约优先Web服务 9.1 介绍Spring-WS 9.2 定义契约(首先!) 9.3 使用服务端点处理消息 9.3.1 建立基于JDOM消息的端点 9.3.2 序列化消息载荷 9.4 合并在一起 9.4.1 Spring-WS:全景视图 ...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring in action英文版

     3.3.1 在Spring中定义切入点  3.3.2 理解Advisor  3.3.3 使用Spring的静态切入点  3.3.4 使用动态切入点  3.3.5 切入点实施  3.4 创建引入  3.4.1 实现IntroductionInterceptor  3.4.2 创建...

    spring chm文档

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    Spring in Action(第2版)中文版

    第9章在spring中建立契约优先web服务 9.1介绍spring-ws 9.2定义契约(首先!) 9.3使用服务端点处理消息 9.3.1建立基于jdom消息的端点 9.3.2序列化消息载荷 9.4合并在一起 9.4.1spring-ws:全景视图 9.4.2将...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    2阶段提交分布式事务中间件,一致性分布式事务框架

    提供对 springcloud dubbo motan RPC 框架的支持。提供 Spring Boot Starter 方法的集成。支持嵌套事务。本地事务存储支持:redis mongodb zookeeper 文件 mysql。事务日志序列化支持:java hessian kryo protostuff...

    myth分布式开发框架-其他

    myth是一个采用消息队列解决分布式事务的开源框架, 基于java语言来开发(JDK1.8),支持dubbo,springcloud,motan等rpc框架进行分布式事务。 特征: 支持所有春季版本并无缝集成 提供对springcloud dubbo motan RPC...

    dubbo_demo.zip

    其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。 **什么是RPC?** RPC全称为remote procedure call,即**远程过程调用**。比如两台服务器A和B,A服务器上部署一个...

    dubbo-admin-0.2.0.jar

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。 Dubbo是一款基于Netty的高性能、轻量级的RPC框架,其主要功能包括:面向...

    mango:高性能,开源Java RPC框架

    易于与Spring Framework 4.x集成。 要求 快速入门的最低要求是: JDK 1.7或更高 基于Java的项目管理软件,例如或 快速开始 1.同步通话 将依赖项添加到pom。 &lt;groupId&gt;com.mindflow&lt;/groupId&gt; &lt;artifactId&gt;...

    java开源包1

    它是 Spring HttpInvoker的一个轻量级选择,特别适合于当你不想在客户端程序中使用Spring框架。 API访问授权的开放标准 OAuth OAuth 协议为用户资源的授权提供了一个安全的、开放而又简易的标准。与以往的授权方式...

Global site tag (gtag.js) - Google Analytics