hessian

hessian:

一个轻量级的RPC框架,基于http协议,使用二进制序列化,对于大数据包比较友好。它的参数和返回值都需要实现Serializable接口。

环境说明:

  • spring        version 3.0      RELEASE.
  • Hessian      version  4.0.7  WHATEVER.
  • JEE            version 2.5

实现步骤:

一、服务端

1、配置 web.xml ,添加对spring-web中 DispatcherServlet的定义.具体如下:

<servlet>
        <servlet-name>test-remote</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/test-remote.xml</param-value>
        </init-param>
        <load-on-startup>4</load-on-startup>
</servlet>

<servlet-mapping>
        <servlet-name>test-remote</servlet-name>
        <url-pattern>/remote/*</url-pattern>
</servlet-mapping>

 DispatcherServlet是可以配置多个的,我这里还有一个test-local,mapping着 "/" 路径,他们之间并不会影响。当然可以单纯的配置多个 servlet-mapping

2、传输对象类Space 的定义:

package test.remote.dto;
 
import java.io.Serializable;
import java.util.Date;
 
/**
 * Space data transfer object.
 * @author gogo
 * 
 */
public class Space implements Serializable{
    
  /**
     * auto generated serialVersionUid.
     */
    private static final long serialVersionUID = 5632812127919571530L;  
    
    /** public space name for people in the space.*/
	private String publicName;
	private String privateName;
	private boolean isActive;
	private Date lastUpdatedTime;
 
	/**
         *  setter & getter
         */
	
	@Override
	public String toString() {
	    StringBuilder buffer = new StringBuilder("Space:");
	    buffer.append("public name:").append(publicName).append(", ");
	    buffer.append("private name:").append(privateName).append(", ");
	    buffer.append("is actived:").append(isActive).append(", ");
	    buffer.append("lastUpdatedTime:").append(lastUpdatedTime.getTime());
	    return buffer.toString();
	} 
}

 3、  服务接口的定义如下:

import java.util.List;
 
import test.remote.dto.Space;
 
/**
 * 
 * @author gogo
 *
 */
public interface SpaceService {
	
	List showSpaceNames(); //返回名字集合
	
	Space findSpace(String id);//找到空间名字
	
	boolean updateSpaceName(String name); //更新空间名字
	
}

 4、服务接口实现类:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.remoting.caucho.HessianServiceExporter;
 
import test.remote.dto.Space;
import test.remote.service.SpaceService;
 
/**
 * 这个是一个远程接口的测试实现<br>
 * 该接口会由spring的 {@link HessianServiceExporter}以Http服务的形式暴露.<br>
 * 客户端会依赖Hessian实现
 * @see HessianServiceExporter
 * @author gogo 
 * 
 */
public class SpaceServiceImpl implements SpaceService{
	
	private static final Log log = LogFactory.getLog(SpaceServiceImpl.class);
	
	private Space createSpace() {
		Space space = new Space();
		space.setPublicName("roosher-space");
		space.setPrivateName("my-private-room");
		space.setActive(true);
		space.setLastUpdatedTime(new Date());
		return space;
	}
 
	public Space findSpace(String id) {
		// TODO Auto-generated method stub
		log.debug("find space id is :" + id);
		return createSpace();
	}
 
	public List showSpaceNames() {
		// TODO Auto-generated method stub
		List names = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			names.add("Hello space "+ i);
		}
		return names;
	}
 
	public boolean updateSpaceName(String name) {
		// TODO Auto-generated method stub
		log.debug("updated space name : " + name );
		return true;
	}
	
}

 5、test-remote.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 	http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean 
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
                         p:order="1"></bean>
		
	<bean id="spaceService" class="test.service.impl.SpaceServiceImpl">
        </bean>
	
	<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter">
        </bean>

	<bean name="/SpaceHessian" 
		class="org.springframework.remoting.caucho.HessianServiceExporter"
		p:serviceInterface="test.remote.service.SpaceService"
		p:service-ref="spaceService">
	</bean>
	
	<bean name="/SpaceBurlap"
             class="org.springframework.remoting.caucho.BurlapServiceExporter"
             p:serviceInterface="test.remote.service.SpaceService"
             p:service-ref="spaceService">
        </bean>
	
</beans>

二、客户端的两种实现:

1、基于spring的实现,采用org.springframework.remoting.caucho.HessianProxyFactoryBean实现,相关配置如下:

<bean id="spaceServiceHessian"
		class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
	<property name="serviceUrl"
		  value="http://localhost:8080/test/remote/SpaceHessian" />
	<property name="serviceInterface" value="test.remote.service.SpaceService" />
</bean>
 
<bean id="spaceServiceBurlap"
        class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
        <property name="serviceUrl"
                  value="http://localhost:8080/test/remote/SpaceBurlap" />
        <property name="serviceInterface" value="test.remote.service.SpaceService" />
</bean>

 注意到里面的URL了没? 其中 http://localhost:8080/test/remote/SpaceHessian 的/SpaceHessian 对应的是之前在remote-test.xml中配置的<bean name="/SpaceHessian"> </bean>的URI.
然后通过spring获取上下文之后,就可以直接注入 SpaceService service = context.getBean("spaceServiceBurlap");

2、就是基于Hessian 自己提供的客户端,这里展示的是Java版本.

package test.remote;
 
import java.net.MalformedURLException;
import java.util.List;
 
import com.caucho.hessian.client.HessianProxyFactory;
import test.remote.dto.Space;
import test.remote.service.SpaceService;
 
/**
 * 连接客户端。测试时要注意 url的ip地址.
 * @see Space
 * @author gogo
 * 
 */
public class SpaceClient {
 
    public static void main(String[] args) {
        String url = "http://192.168.0.9:8080/test/remote/SpaceHessian";
        HessianProxyFactory factory = new HessianProxyFactory();
        try {
            SpaceService spaceService = (SpaceService) factory.create(
                    SpaceService.class, url);
            System.out.println(spaceService.findSpace("testid"));
            System.out.println(spaceService.updateSpaceName("whatever"));
            
            List spaceNames = spaceService.showSpaceNames();
            System.out.println("
Get space names:");
            for (int i = 0; i < spaceNames.size(); i++) {
                System.out.print(spaceNames.get(i) + ", ");
            }
            System.out.println();
            System.out.println("space names list finished");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

 三、测试结果:

    Space:public name:roosher-space, private name:my-private-room, is actived:true,
    lastUpdatedTime:1282196778538
    true
     
    Get space names:
    Hello space 0, Hello space 1, Hello space 2, Hello space 3, Hello space 4, Hello space 5
    , Hello space 6, Hello space 7, Hello space 8, Hello space 9,
    space names list finished
原文地址:https://www.cnblogs.com/wzk-0000/p/10617479.html