Hessian知识学习

来源:互联网 发布:visio 数据库图标 编辑:程序博客网 时间:2024/06/01 19:31

简介

Hessian是一个轻量级的remoting on http工具,使用简单的方法提供了RMI(Remote Method Invocation,远程方法调用)的功能。采用的是二进制RPC(Remote Procedure Call Protocol,远程过程调用协议)协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

在进行基于Hessian的项目开发时,应当注意以下几点:

  ▲Java服务器端必须具备以下几点:

  ·包含Hessian的jar包。

  ·设计一个接口,用来给客户端调用。

  ·实现该接口的功能。

  ·配置web.xml,配好相应的servlet。

  ·对象必须实现Serializable 接口。

  ·对于复杂对像可以使用Map的方法传递。

  ▲客户端必须具备以下几点:

  ·java客户端包含Hessian.jar的包。

  ·具有和服务器端结构一样的接口。

·利用HessianProxyFactory调用远程接口

下载Hessian

在pom.xml文件中下载hessian-4.0.37.jar

    <dependency>        <groupId>com.caucho</groupId>        <artifactId>hessian</artifactId>        <version>4.0.37</version>    </dependency>

或者直接在hessian官网http://hessian.caucho.com/ 下载加载到lib目录下。

搭建Hessian的Server

提供服务接口ITestHessianService,如下所示:

package com.shen.test.service;public interface ITestHessianService {    /**     * 姓名测试方法     *      * @param name 姓名     */    public void testName(String name);}

实现ITestHessianService接口,实现类为TestHessianService:

package com.shen.test.service.impl;import com.shen.test.service.ITestHessianService;public class TestHessianService implements ITestHessianService{    public void testName(String name) {        System.out.println("我的姓名是:"+name);    }}

配置web.xml,添加对HessianServlet的配置(采用Spring):

  <servlet>       <servlet-name>hessian</servlet-name>       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>       <init-param>          <param-name>namespace</param-name>        <param-value>classes/spring/spring-hessian</param-value>       </init-param>     <load-on-startup>3</load-on-startup>    </servlet>    <servlet-mapping>       <servlet-name>hessian</servlet-name>       <url-pattern>/hessian/*</url-pattern>    </servlet-mapping>  <servlet-mapping>       <servlet-name>hessian</servlet-name>       <url-pattern>*.do</url-pattern>  </servlet-mapping>

spring-hessian.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:jaxrs="http://cxf.apache.org/jaxrs"    xsi:schemaLocation="http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"    default-lazy-init="true">    <description>hessian server配置</description>    <!-- hessian测试 -->    <bean id="testHessianService" class="com.shen.test.service.impl.TestHessianService"></bean>    <bean name="/testHessian.do"  class="org.springframework.remoting.caucho.HessianServiceExporter">          <property name="service" ref="testHessianService" />          <property name="serviceInterface" value="com.shen.test.service.ITestHessianService" />      </bean></beans>

此时,Hessian Server的配置已经完成了,接下来将应用部署在tomcat并启动。访问链接http://localhost:8080/mavenTest/hessian/testHessian.do,得到信息如下:

这里写图片描述

实现Hessian的client

调用Hessian的客户端,创建TestHessianClient类,代码如下:

package com.shen.test.controller;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;import com.shen.test.service.ITestHessianService;public class TestHessianClient {    public static void main(String[] args) throws MalformedURLException {        ITestHessianService  service = TestHessianClient.getServerService();        service.testName("张三");    }    /**     *      * @return     * @throws MalformedURLException     */    public static ITestHessianService getServerService() throws MalformedURLException{        HessianProxyFactory hessianProxyFactory = new HessianProxyFactory();        hessianProxyFactory.setOverloadEnabled(true);        String url = "http://localhost:8080/mavenTest/hessian/testHessian.do";        return (ITestHessianService)hessianProxyFactory.create(ITestHessianService.class, url);    }}

执行结果如下:

这里写图片描述

0 0
原创粉丝点击