Spring3.1.2与Hessian4.0.7集成示例

Hessian是由caucho提供的一个基于binary-RPC实现的远程通讯library。

Hessian处理过程示意图:
    客户端——>序列化写到输出流——>远程方法(服务器端)——>序列化写到输出流 ——>客户端读取输入流——>输出结果

相关jar包


Hessian 调用实例

一、编写服务端

1. 写一个接口
package com.hessian;

import java.util.List;
import java.util.Map;

/**
 * 
 */
public interface IHessianService {
	String sayHello();

	Car getCar();

	List<String> getList();

	Map<String, String> getMap();
}

package com.hessian;

import java.io.Serializable;

public class Car implements Serializable {
	private static final long serialVersionUID = 1L;
	private String carName;
	private String carModel;

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

	public String getCarModel() {
		return carModel;
	}

	public void setCarModel(String carModel) {
		this.carModel = carModel;
	}

	public String toString() {
		return "my car name:[" + this.carName + "] model:[" + this.carModel + "].";
	}
}

2.  编写一个实现

package com.hessian;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HessianService implements IHessianService {

	public String sayHello() {
		return "welcom to Hessian";
	}

	public Car getCar() {
		Car car = new Car();
	    car.setCarName("BMW");
	    car.setCarModel("X5");
	    return car;
	}

	public List<String> getList() {
		List<String> list = new ArrayList<String>();
	    list.add("aaaa");
	    list.add("bbbb");
	    list.add("cccc");
	    return list;
	}

	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap<String, String>();
	    map.put("key1", "张三");
	    map.put("key2", "李四");
	    return map;
	}
}

3. 配置WEB-INF.xml 部署到Web容器中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<servlet>  
	    <servlet-name>springhessian</servlet-name>  
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	    <load-on-startup>1</load-on-startup> 
	</servlet>  
	<servlet-mapping>  
	    <servlet-name>springhessian</servlet-name>  
	    <url-pattern>/hessian/*</url-pattern>  
	</servlet-mapping>  
</web-app>

4. spring配置
<?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:context="http://www.springframework.org/schema/context"
	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 id="hessianService" class="com.hessian.HessianService" /> 
    <bean name="/springHessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <property name="service" ref="hessianService" />  
        <property name="serviceInterface" value="com.hessian.IHessianService" />  
    </bean>
</beans>

二 、编写客户端

1. 在src下创建hessian-client.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:context="http://www.springframework.org/schema/context"
	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 id="hessianClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
        <property name="serviceUrl" value="http://localhost:8080/springhessian1/hessian/springHessianService" />  
        <property name="serviceInterface" value="com.hessian.IHessianService" />  
    </bean>  
</beans>

2. 编写测试代码
package com.hessian;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hessian.Car;

/**    
 *     
 */
public class HessianSpringClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
			IHessianService hessianClient = (IHessianService) context.getBean("hessianClient");
			//String
			System.out.println("--------String----------");
			System.out.println(hessianClient.sayHello());
			//VO
			System.out.println("----------VO--------");
			Car car = hessianClient.getCar();
			System.out.println(car.toString());
			//Map
			System.out.println("----------Map--------");
			for (Map.Entry<String, String> entry : hessianClient.getMap().entrySet()) {
				System.out.println(entry.getKey() + "   " + entry.getValue());
			}
			//Collection
			System.out.println("----------Collection--------");
			for (String str : hessianClient.getList()) {
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

输出结果:
--------String----------
welcom to Hessian
----------VO--------
my car name:[BMW] model:[X5].
----------Map--------
key2   李四
key1   张三
----------Collection--------
aaaa
bbbb
cccc

说明:

1. 可能遇到的问题jar包导入,url路径, 如果项目为复制的项目,记得修改项目context root, eclipse中项目右键属性,找到Web Project settings项

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值