Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据
在进行基于Hessian的项目开发时,应当注意以下几点: ▲JAVA服务器端必须具备以下几点: ·包含Hessian的jar包 ·设计一个接口,用来给客户端调用 ·实现该接口的功能 ·配置web.xml,配好相应的servlet ·对象必须实现Serializable 接口 ·对于复杂对像可以使用Map的方法传递 ▲客户端必须具备以下几点: ·java客户端包含Hessian.jar的包。C#中引用hessianCSharp.dll ·具有和服务器端结构一样的接口。包括命名空间都最好一样·利用HessianProxyFactory调用远程接口。
一、服务器(service)
1、实体类
1 public class CarDO implements java.io.Serializable{
2
3 /**
4 * 一定要实现Serializable因为要远程传输
5 */
6 private static final long serialVersionUID = 1L;
7
8 String color;
9
10 String length;
11
12 String name;
13
14 public String getColor() {
15 return this.color;
16 }
17 public void setColor(String color) {
18 this.color = color;
19 }
20 public String getLength() {
21 return this.length;
22 }
23 public void setLength(String length) {
24 this.length = length;
25 }
26 public String getName() {
27 return this.name;
28 }
29 public void setName(String name) {
30 this.name = name;
31 }
2
3 /**
4 * 一定要实现Serializable因为要远程传输
5 */
6 private static final long serialVersionUID = 1L;
7
8 String color;
9
10 String length;
11
12 String name;
13
14 public String getColor() {
15 return this.color;
16 }
17 public void setColor(String color) {
18 this.color = color;
19 }
20 public String getLength() {
21 return this.length;
22 }
23 public void setLength(String length) {
24 this.length = length;
25 }
26 public String getName() {
27 return this.name;
28 }
29 public void setName(String name) {
30 this.name = name;
31 }
2、接口类
1
2 import com.hessian.server.javabean.CarDO;
3 /**
4 * @author sunney
5 *
6 */
7 public interface IHello {
8
9 /**
10 * 返回字符串
11 * @return
12 */
13 String sayHello();
14
15 /**
16 * 返回一个实体
17 * @param carDO
18 * @return
19 */
20 CarDO searchCarInfo(CarDO carDO);
21 }
22
2 import com.hessian.server.javabean.CarDO;
3 /**
4 * @author sunney
5 *
6 */
7 public interface IHello {
8
9 /**
10 * 返回字符串
11 * @return
12 */
13 String sayHello();
14
15 /**
16 * 返回一个实体
17 * @param carDO
18 * @return
19 */
20 CarDO searchCarInfo(CarDO carDO);
21 }
22
3、实现类
1
2 import com.caucho.hessian.server.HessianServlet;
3 import com.hessian.server.IHello;
4 import com.hessian.server.javabean.CarDO;
5
6 public class Hello extends HessianServlet implements IHello{
7 /**
8 *
9 */
10 private static final long serialVersionUID = 1L;
11
12 public String sayHello(){
13 return "Hello world,zoomhoo again!";
14 }
15
16 public CarDO searchCarInfo(CarDO carDO) {
17 System.out.println("server Color:"+carDO.getColor());
18 System.out.println("server Name:"+carDO.getName());
19 System.out.println("server Length:"+carDO.getLength());
20 carDO.setColor("blue");
21 carDO.setName("baoma");
22 carDO.setLength("1000000");
23 return carDO;
24 }
25
26
27 }
2 import com.caucho.hessian.server.HessianServlet;
3 import com.hessian.server.IHello;
4 import com.hessian.server.javabean.CarDO;
5
6 public class Hello extends HessianServlet implements IHello{
7 /**
8 *
9 */
10 private static final long serialVersionUID = 1L;
11
12 public String sayHello(){
13 return "Hello world,zoomhoo again!";
14 }
15
16 public CarDO searchCarInfo(CarDO carDO) {
17 System.out.println("server Color:"+carDO.getColor());
18 System.out.println("server Name:"+carDO.getName());
19 System.out.println("server Length:"+carDO.getLength());
20 carDO.setColor("blue");
21 carDO.setName("baoma");
22 carDO.setLength("1000000");
23 return carDO;
24 }
25
26
27 }
4、web.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>/WEB-INF/remoting-servlet.xml</param-value>
10 </context-param>
11
12 <listener>
13 <listener-class>
14 org.springframework.web.context.ContextLoaderListener</listener-class>
15 </listener>
16
17 <servlet>
18 <servlet-name>remoting</servlet-name>
19
20 <servlet-class>
21 org.springframework.web.servlet.DispatcherServlet</servlet-class>
22 <load-on-startup>1</load-on-startup>
23 </servlet>
24
25 <servlet-mapping>
26 <servlet-name>remoting</servlet-name>
27 <url-pattern>/remoting/*</url-pattern>
28 </servlet-mapping>
29
30 <servlet>
31 <servlet-name>Hello</servlet-name>
32 <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
33 <init-param>
34 <param-name>home-class</param-name>
35 <param-value>com.hessian.server.impl.Hello</param-value>
36 </init-param>
37 <init-param>
38 <param-name>home-api</param-name>
39 <param-value>com.hessian.server.IHello</param-value>
40 </init-param>
41 <load-on-startup>1</load-on-startup>
42 </servlet>
43 <servlet-mapping>
44 <servlet-name>Hello</servlet-name>
45 <url-pattern>/Hello</url-pattern>
46 </servlet-mapping>
47 </web-app>
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>/WEB-INF/remoting-servlet.xml</param-value>
10 </context-param>
11
12 <listener>
13 <listener-class>
14 org.springframework.web.context.ContextLoaderListener</listener-class>
15 </listener>
16
17 <servlet>
18 <servlet-name>remoting</servlet-name>
19
20 <servlet-class>
21 org.springframework.web.servlet.DispatcherServlet</servlet-class>
22 <load-on-startup>1</load-on-startup>
23 </servlet>
24
25 <servlet-mapping>
26 <servlet-name>remoting</servlet-name>
27 <url-pattern>/remoting/*</url-pattern>
28 </servlet-mapping>
29
30 <servlet>
31 <servlet-name>Hello</servlet-name>
32 <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
33 <init-param>
34 <param-name>home-class</param-name>
35 <param-value>com.hessian.server.impl.Hello</param-value>
36 </init-param>
37 <init-param>
38 <param-name>home-api</param-name>
39 <param-value>com.hessian.server.IHello</param-value>
40 </init-param>
41 <load-on-startup>1</load-on-startup>
42 </servlet>
43 <servlet-mapping>
44 <servlet-name>Hello</servlet-name>
45 <url-pattern>/Hello</url-pattern>
46 </servlet-mapping>
47 </web-app>
5、remoting-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
3 "http://www.springframework.org/dtd/spring-beans.dtd">
4 <beans>
5 <!-- 定义普通bean实例-->
6 <bean id="hello" class="com.hessian.server.impl.Hello"/>
7 <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
8 <bean name="/HessianRemoting"
9 class="org.springframework.remoting.caucho.HessianServiceExporter">
10 <!-- 需要导出的目标bean-->
11 <property name="service" ref="hello"/>
12 <!-- Hessian服务的接口-->
13 <property name="serviceInterface" value="com.hessian.server.IHello"/>
14 </bean>
15 </beans>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
3 "http://www.springframework.org/dtd/spring-beans.dtd">
4 <beans>
5 <!-- 定义普通bean实例-->
6 <bean id="hello" class="com.hessian.server.impl.Hello"/>
7 <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务-->
8 <bean name="/HessianRemoting"
9 class="org.springframework.remoting.caucho.HessianServiceExporter">
10 <!-- 需要导出的目标bean-->
11 <property name="service" ref="hello"/>
12 <!-- Hessian服务的接口-->
13 <property name="serviceInterface" value="com.hessian.server.IHello"/>
14 </bean>
15 </beans>
二、客户端(clent)
1、客户端也要有对应该的实体类,最好包的路径要一样。如: com.hessian.server.javabean.CarDO
以上存在不在贴代码。
2、接口
1 import com.hessian.server.javabean.CarDO;
2 /**
3 * @author
4 *
5 */
6 public interface IHello {
7 String sayHello();
8 CarDO searchCarInfo(CarDO carDO);
9 }
2 /**
3 * @author
4 *
5 */
6 public interface IHello {
7 String sayHello();
8 CarDO searchCarInfo(CarDO carDO);
9 }
3、实现类
1
2 package com.hessian.client;
3
4 import java.net.MalformedURLException;
5
6 import com.caucho.hessian.client.HessianConnectionException;
7 import com.caucho.hessian.client.HessianProxyFactory;
8 import com.hessian.server.IHello;
9 import com.hessian.server.javabean.CarDO;
10
11 /**
12 * @author
13 *
14 */
15 public class simpleTest {
16
17 /**
18 * @param args
19 */
20 public static void main(String[] args) {
21 //String url = "http://192.168.1.10:8080/hessiantest/Hello";
22 String url = "http://localhost:8080/hessian-server/Hello";
23 HessianProxyFactory factory = new HessianProxyFactory();
24 try {
25 IHello hello = (IHello) factory.create(IHello.class, url);
26 System.out.println(hello.sayHello());
27
28 CarDO carDO =new CarDO();
29 carDO.setColor("red");
30 carDO.setLength("100mile");
31 carDO.setName("bieke");
32 CarDO returncarDO=hello.searchCarInfo(carDO);
33
34 System.out.println("client Color:"+returncarDO.getColor());
35 System.out.println("client Name:"+returncarDO.getName());
36 System.out.println("client Length:"+returncarDO.getLength());
37
38 } catch (MalformedURLException e) {
39 e.printStackTrace();
40 } catch (HessianConnectionException e) {
41 e.printStackTrace();
42 }
43 }
44 }
2 package com.hessian.client;
3
4 import java.net.MalformedURLException;
5
6 import com.caucho.hessian.client.HessianConnectionException;
7 import com.caucho.hessian.client.HessianProxyFactory;
8 import com.hessian.server.IHello;
9 import com.hessian.server.javabean.CarDO;
10
11 /**
12 * @author
13 *
14 */
15 public class simpleTest {
16
17 /**
18 * @param args
19 */
20 public static void main(String[] args) {
21 //String url = "http://192.168.1.10:8080/hessiantest/Hello";
22 String url = "http://localhost:8080/hessian-server/Hello";
23 HessianProxyFactory factory = new HessianProxyFactory();
24 try {
25 IHello hello = (IHello) factory.create(IHello.class, url);
26 System.out.println(hello.sayHello());
27
28 CarDO carDO =new CarDO();
29 carDO.setColor("red");
30 carDO.setLength("100mile");
31 carDO.setName("bieke");
32 CarDO returncarDO=hello.searchCarInfo(carDO);
33
34 System.out.println("client Color:"+returncarDO.getColor());
35 System.out.println("client Name:"+returncarDO.getName());
36 System.out.println("client Length:"+returncarDO.getLength());
37
38 } catch (MalformedURLException e) {
39 e.printStackTrace();
40 } catch (HessianConnectionException e) {
41 e.printStackTrace();
42 }
43 }
44 }
就这么简单。呵呵,大家一起讨论吧。呵呵。源码下载地址:hessian.rar