ALL is Well!

敏捷是一条很长的路,摸索着前进着

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  30 随笔 :: 23 文章 :: 71 评论 :: 0 Trackbacks
本文为原创,欢迎转载,转载请注明出处BlogJava

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

本文将分为以下几个内容:
一、一个简单的Hessian调用例子
二、Spring+Hessian的例子
三、使我们的调用变得通用

第一部分:一个简单的Hessian调用例子
建立web工程HessianService。
这里为了简单起见,我将远程接口类、接口实现类都定义在此web工程下。

个人觉得一个比较好的做法是将web工程分开,此web工程只定义对外的接口并提供服务,而实现类新建一个Java工程存放。
这样web工程依赖此Java工程,且客户端也依赖此Java工程。

1) 远程接口类 ServiceRemote.java

 

package com.al;

import java.util.Map;

@SuppressWarnings(
"unchecked")
public interface ServiceRemote  {
    
public Map callService(Map inputMap) ;
}


2) 实现类 Service.java

package com.al;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings(
"unchecked")
public class Service implements ServiceRemote {

    
public Map callService(Map inputMap) {
        
if(inputMap == null{
            inputMap 
= new HashMap();
        }

        
// do something
        
// 
        inputMap.put("NAME""Hessian");
        
return inputMap;
    }

}


3) web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
<servlet>
        
<servlet-name>hessianService</servlet-name>
        
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
        
<init-param>
            
<param-name>service-class</param-name>
            
<param-value>com.al.Service</param-value>
        
</init-param>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>hessianService</servlet-name>
        
<url-pattern>/hessianService</url-pattern>
    
</servlet-mapping>
</web-app>


经过以上3步,hessian服务部署算是完成了,在tomcat下发布。

4) 调用方代码

package com.ai.client;

import com.al.ServiceRemote;
import com.caucho.hessian.client.HessianProxyFactory;

public class ClientTest {
    
public static void main(String[] args) throws Exception {
        String url 
= "http://localhost:8080/HessianService/hessianService";
        HessianProxyFactory factory 
= new HessianProxyFactory();
        ServiceRemote rmt 
= (ServiceRemote) factory.create(ServiceRemote.class, url);
        System.out.println(rmt.callService(
null));
    }

}


执行代码,结果如下:
{NAME=Hessian}
说明调用远程代码成功了。

二、Spring+Hessian的例子
1) web工程HessianService 的ServiceRemote 和Service类不变。对web.xml进行修改:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    
</context-param>
    
<servlet>
        
<servlet-name>dispatcher</servlet-name>
        
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
<load-on-startup>1</load-on-startup>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>dispatcher</servlet-name>
        
<url-pattern>/remote/*</url-pattern>
    
</servlet-mapping>
    
<welcome-file-list>
        
<welcome-file>index.jsp</welcome-file>
    
</welcome-file-list>
</web-app>


这里要注意的是:
a)dispatcher-servlet.xml这个文件的命名。servlet配置为dispatcher,则此文件定义规则为****-servlet.xml.

b)另外就是Spring和hessian的版本问题。
spring版本是2.5.6,需要在此web工程下引入:spring-2.5.6.jar、spring-webmvc-2.5.6.jar、commons-logging-1.1.1.jar。
hessian的版本是hessian-3.1.6.jar,hessian的低版本如 hessian-3.0.13和此Spring版本不合。(我试验了是配置不成功的。)

2) dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    
<description>hessian server properties</description>
    
<bean id="serviceImpl" class="com.al.Service" />
    
<bean name="/service" class="org.springframework.remoting.caucho.HessianServiceExporter">
        
<property name="service">
            
<ref bean="serviceImpl" />
        
</property>
        
<property name="serviceInterface">
            
<value>com.al.ServiceRemote</value>
        
</property>
    
</bean>
</beans>


3) 客户端代码可以保持原样不变,把调用hessian服务的url变换一下即可:
http://localhost:8080/HessianService/remote/service
http://IP:port/发布工程名/web.xml中配置的url-pattern/****-servlet.xml bean的ID。

三、使我们的调用变得通用 将在下一篇中介绍。

posted on 2010-10-17 21:11 李 明 阅读(2805) 评论(0)  编辑  收藏 所属分类: J2EESpring

只有注册用户登录后才能发表评论。


网站导航: