JAVA ·

SpingMVC 集成 Hession 远程服务

Hession 是什么?

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

引入Hession需要hessian jar包

Maven引入,如需单独下载jar包,请到:Maven中央库 或者官网

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

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:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/tx  
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
     http://www.springframework.org/schema/jee  
     http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
     http://www.springframework.org/schema/aop  
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.0.xsd  
     http://activemq.apache.org/schema/core  
     http://activemq.apache.org/schema/core/activemq-core.xsd"  
    default-lazy-init="true">   
      
    <context:annotation-config />  
    <!-- 组件扫描,使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->  
    <context:component-scan base-package="com.cn.xzbms.hessian" />  
  
    <!-- 自动装配 -->  
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  
      
    <!-- Name保持与web.xml中的一致,web.xml下文中描述 -->    
    <bean name="hessionServiceExport" class="org.springframework.remoting.caucho.HessianServiceExporter">    
        <!-- service的ref与HelloServiceImpl中@Service中配置的一致 -->    
        <property name="service" ref="hessionService" />    
        <!-- 接口的路径 -->    
        <property name="serviceInterface" value="com.cn.xzbms.hessian.IHessionService" />    
    </bean>    
</beans>    

web.xml

<!-- 在此处配置刚刚写的spring-hessian.xml的位置 -->    
<context-param>    
   <param-name>contextConfigLocation</param-name>    
     <param-value>      
         classpath:/spring-hessian.xml    
     </param-value>    
</context-param> 

<servlet>    
    <!-- servlet-name保持与spring-hessian.xml中一致 -->    
    <servlet-name>hessionServiceExport</servlet-name>    
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>    
    </servlet>    
<servlet-mapping>    
    <servlet-name>hessionServiceExport</servlet-name>    
    <url-pattern>/hessionService</url-pattern>    
</servlet-mapping>

Java类:接口类

package com.cn.xzbms.hessian;

/**
 * hession interface 
 * @author payne
 *
 */
public interface IHessionService {
	public void say(String name);
}

Java类:实现类

package com.cn.xzbms.hessian;

import org.springframework.stereotype.Service;

/**
 * Hession Service Impl
 * @author payne
 *
 */
@Service("hessionService")
public class HessionServiceImpl implements IHessionService{

	@Override
	public void say(String name) {
		// TODO Auto-generated method stub
		System.out.println("Hello,My name is: " + name + "!");
		System.out.println("Url : http://localhost/yixiema");
	}

}

Java类:测试类

package com.cn.xzbms.hessian;

import com.caucho.hessian.client.HessianProxyFactory;

public class hessionTest {

	public static void main(String[] args){
		try {    
            String url = "http://localhost:8080/xzbms/hessionService";    
            HessianProxyFactory factory = new HessianProxyFactory();    
            IHessionService hessionService = (IHessionService) factory.create(    
            		IHessionService.class, url);    
            hessionService.say("晓庄");    
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
	}
}

测试结果

参与评论