使用Hessian创建接口

使用Hessian创建接口

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
详细介绍

1. Hessian集成Spring配置

加入Hessian依赖

<hessian.version>4.0.38</hessian.version>  
...
<!-- hessian -->
<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>${hessian.version}</version>
</dependency>  

Hessian依赖已SpringMVC,首先需要配置SpringMVC环境,SpringMVC配置略!
首先创建hessian.xml,引入Spring Bean schema

<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</beans> 

在SpringMVC配置文件中加入

<import resource="classpath:hessian-api.xml"/>  

2. 创建接口及实现

接口

package com.tiamaes.gjds.app.hessian.api;

import java.util.List;

import com.tiamaes.gjds.app.bean.TrafficReport;

/** * <p>类描述: 路况信息API ,Hessian接口</p> * <p>创建人:王成委 </p> * <p>创建时间:2015年1月16日 下午3:05:04 </p> * <p>版权说明: © 2015 Tiamaes </p> */
public interface TrafficReportApi {

    /** * 获取全部路况信息 * @author 王成委 * @return */
    public List<TrafficReport> getAll();

    /** * <p>方法描述:畅行指数</p> * <p>创建人: 王成委 </p> * <p>创建时间: 2015年1月10日 上午10:50:06 </p> * @return */
    public Double getCurrentTrafficIndex();

}

实现类

package com.tiamaes.gjds.app.hessian.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.tiamaes.gjds.app.bean.TrafficReport;
import com.tiamaes.gjds.app.service.TrafficReportService;

/** * <p>类描述: 路况查询 </p> * <p>创建人:王成委 </p> * <p>创建时间:2015年1月16日 下午3:15:29 </p> * <p>版权说明: © 2015 Tiamaes </p> */
public class TrafficReportApiImpl implements TrafficReportApi{

    @Autowired
    private TrafficReportService trafficReportService;

    @Override
    public List<TrafficReport> getAll() {
        try {
            List<TrafficReport> list = this.trafficReportService.getAllCongestion();
            //System.out.println(JackSonUtils.bean2Json(list));
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Double getCurrentTrafficIndex() {
        return this.trafficReportService.getCurrentTrafficIndex();
    }

}

3、Spring配置

   <bean name="busWatingApi" class="com.tiamaes.gjds.app.hessian.api.BusWaitingApiImpl" /> 
   <bean name="/hessian/busWatingApi" class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <!-- service引用具体的实现实体Bean-->  
        <property name="service" ref="busWatingApi" />
        <property name="serviceInterface" value="com.tiamaes.gjds.app.hessian.api.BusWaitingApi" />
    </bean>   

4、测试

    /** * Test method for {@link com.tiamaes.gjds.app.hessian.api.BusLineApiImpl#getLineInfo(String,String)}. * @throws ClassNotFoundException * @throws MalformedURLException * @throws AppInterfaceException */
    @Test
    public void testQueryBusStationInfo() throws MalformedURLException, ClassNotFoundException {
        String url = "http://127.0.0.1:808/AppInterface/hessian/busLineApi";
        HessianProxyFactory factory = new HessianProxyFactory();
        BusLineApi  api = (BusLineApi) factory.create(BusLineApi.class,url);
        List<GjdsBusStationInfo> reports = api.queryBusStationInfo("B2", "1");
        assertTrue(reports.size() > 0);
    }  

你可能感兴趣的:(java,spring,接口,hessian,remoting)