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项