前言:本文主要介绍‘独立的Hessian技术’与‘结合Spring技术’的两种Hessian接口开发模式及代码示例。
一.独立的Hessian技术开发步骤
Hessian-Java服务器端必须具备以下几点:
* 1.包含Hessian的jar包(hessian-4.0.37.jar)
* 2.设计一个接口,用来给客户端调用(IHessian.java)
* 3.实现该接口的功能(IHessianImpl.java)
* 4.配置web.xml,配好相应的Servlet(web.xml)
* 5.对象必须实现Serializable接口(Foo.java)
Hessian-JAVA服务端代码预览图:
1.包含Hessian的jar包(hessian-4.0.37.jar)
re:可到Hessian官网http://hessian.caucho.com/,下载最新的Hessian包或见本文附件。
2.设计一个接口,用来给客户端调用(IHessian.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package my.hessian.service; import my.hessian.model.Foo; /** * 定义对外提供服务的接口(Hessian简单入门)[纯Hessian] * * Hessian是一个简单的连接Web服务的二进制协议 * * Hessian-JAVA服务器端必须具备以下几点: * 1.包含Hessian的jar包 * 2.设计一个接口,用来给客户端调用 * 3.实现该接口的功能 * 4.配置web.xml,配好相应的Servlet * 5.对象必须实现Serializable接口 * * @author WChao * @version [My-1.0, 2016-2-21] */ public interface IHessian { public Foo getFoo(); }
3.实现该接口的功能(IHessianImpl.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package my.hessian.service.impl; import my.hessian.model.Foo; import my.hessian.service.IHessian; /** * 接口的具体实现类[纯Hessian] * * @author WChao * @version [My-1.0, 2016-2-21] */ public class IHessianImpl implements IHessian { public Foo getFoo() { System.out.println("call getFoo method ~"); Foo foo = new Foo(); foo.setParam("Hi, This is my first Hessian demo."); return foo; } }
4.配置web.xml,配好相应的Servlet(web.xml)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <!-- 配置HessianServlet, 此处ServiceServlet名字可以自定义--> <servlet-name>ServiceServlet</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <!-- 配置接口的具体实现类 --> <init-param> <param-name>service-class</param-name> <param-value>my.hessian.service.impl.IHessianImpl</param-value> </init-param> </servlet> <!-- 映射HessianServlet的访问URL地址--> <servlet-mapping> <servlet-name>ServiceServlet</servlet-name> <url-pattern>/HessianFoo</url-pattern> </servlet-mapping> <!-- Tips: Hessian与Spring集成的重要文件为什么叫remote-servlet.xml ? 因为在web.xml中有配置:<servlet-name>remote</servlet-name> 所以remote-servlet.xml的文件名必须以<servlet-name>中配置的servlet-name作为文件名的开头, 且文件名的格式必须是[servlet-name]-servlet.xml格式,否则检测不到。 即: <param-value>remote-servlet</param-value>所以文件名为remote-servlet.xml --> <servlet> <servlet-name>remote</servlet-name> <!-- 使用Spring的代理Servlet --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- init-param的配置可选,remote-servlet.xml文件默认放置WEB-INF/下面 --> <!-- 如果想自定义remote-servlet.xml名字,修改namespace对应param-value即可 --> <!-- <init-param> <param-name>namespace</param-name> <param-value>remote-servlet</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping> </web-app>
5.对象必须实现Serializable接口(Foo.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package my.hessian.model; import java.io.Serializable; /** * 创建一个Foo类,同时实现序列化接口, * Description: Foo实现了序列化接口后就可以进行序列化,然后就可以通过网络将序列化后的Foo传输到另一台计算机上 * * @author WChao * @version [My-1.0, 2016-2-21] */ public class Foo implements Serializable { private static final long serialVersionUID = 1L; private String param = ""; public String getParam() { return param; } public void setParam(String param) { this.param = param; } public String toString() { return this.param; } }
Hessian-Java客户端必须具备以下几点:
* 1.java客户端包含hessian.jar的包(hessian-4.0.37.jar)
* 2.具有和服务器端结构一样的接口及涉及Bean对象(MyHessianServer.jar-Foo.class)
* 3.利用HessianProxyFactory调用远程接口(HessianClient.java)
Hessian-JAVA客户端端代码预览图:
1.java客户端包含hessian.jar的包(hessian-4.0.37.jar)
re: 可到Hessian官网:http://hessian.caucho.com/,下载最新的Hessian包或见本人附件。
2.具有和服务器端结构一样的接口及涉及Bean对象(MyHessianServer.jar-Foo.class)
re: 见本文附件中的MyHessianServer.jar。
3.利用HessianProxyFactory调用远程接口(HessianClient.java)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package my.hessianclient; import java.net.MalformedURLException; import my.hessian.model.Foo; import my.hessian.service.IHessian; import my.hessian.service.IHessianOfSpring; import com.caucho.hessian.client.HessianProxyFactory; /** * 调用纯Hessian的客户端 * * 客户端必须具备以下几点: * 1.java客户端包含hessian.jar的包 * 2.具有和服务器端结构一样的接口及涉及Bean对象 * 3.利用HessianProxyFactory调用远程接口 * * @author WChao * @version [My-1.0, 2016-2-21] */ public class HessianClient { public static void main(String[] args) throws MalformedURLException { /* <servlet> <!-- 配置HessianServlet, 此处ServiceServlet名字可以自定义--> <servlet-name>ServiceServlet</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <!-- 配置接口的具体实现类 --> <init-param> <param-name>service-class</param-name> <param-value>my.hessian.service.impl.IHessianImpl</param-value> </init-param> </servlet> <!-- 映射HessianServlet的访问URL地址--> <servlet-mapping> <servlet-name>ServiceServlet</servlet-name> <url-pattern>/HessianFoo</url-pattern> </servlet-mapping> */ // 使用HessianProxyFactory Hessian代理工厂直接调用 HessianProxyFactory factory = new HessianProxyFactory(); // 在服务器端的web.xml文件中配置的HessianServlet映射的访问URL地址 // 纯Hessian接口 String url = "http://localhost:8080/HessianServer/HessianFoo"; // 创建IHessian接口的实例对象 IHessian iHessian = (IHessian) factory.create(IHessian.class, url); Foo myFoo = iHessian.getFoo(); System.out.println(myFoo); System.out.println("------------------------------------------------------"); // HessianServiceExporter only supports POST requests // Hessian与Spring集成接口 url = "http://localhost:8080/HessianServer/remoting/IHessianOfSpring"; IHessianOfSpring iHessianOfSpring = (IHessianOfSpring) factory.create(IHessianOfSpring.class, url); myFoo = iHessianOfSpring.getFoo(); System.out.println(myFoo); // 打印结果: // Hi, This is my first Hessian demo. // ------------------------------------------------------ // Hi, This is my first Hessian + Spring demo. } }
二.Hessian+Spring 接口开发步骤
Hessian+Spring服务端开发步骤与独立的Hessian接口的区别:
a、web.xml配置不同
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<servlet> <servlet-name>remote</servlet-name> <!-- 使用Spring的代理Servlet --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- init-param的配置可选,remote-servlet.xml文件默认放置WEB-INF/下面 --> <!-- 如果想自定义remote-servlet.xml名字,修改namespace对应param-value即可 --> <!-- <init-param> <param-name>namespace</param-name> <param-value>remote-servlet</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping>
b、多了remote-servlet.xml文件及配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 接口的具体实现类 --> <bean id="iHessianOfSpringImpl" class="my.hessian.service.impl.IHessianOfSpringImpl" /> <!-- 使用Spring的HessianServie做代理 --> <bean name="/IHessianOfSpring" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service引用具体的实现实体Bean--> <property name="service" ref="iHessianOfSpringImpl" /> <property name="serviceInterface" value="my.hessian.service.IHessianOfSpring" /> </bean> <!-- 可以配置多个HessianServiceExporter代理Bean --> </beans>
Hessian+Spring客户端开发步骤与独立的Hessian接口的区别:
1、除了可以使用HessianProxyFactory Hessian代理工厂直接调用,还可以使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package my.hessianclient; import my.hessian.model.Foo; import my.hessian.service.IHessianOfSpring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 调用Hessian与Spring集成接口的客户端 * Description: 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用 * * @author WChao * @version [My-1.0, 2016-2-21] */ public class HessianAndSpringClient { public static void main(String[] args) { ApplicationContext contex = new ClassPathXmlApplicationContext("remote-client.xml"); // 获得客户端的Hessian代理工厂bean IHessianOfSpring i = (IHessianOfSpring) contex.getBean("myClient"); Foo myFoo = i.getFoo(); System.out.println(myFoo); // 打印结果: // Hi, This is my first Hessian + Spring demo. } }
2、多了remote-client.xml的Spring文件配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 客户端Hessian代理工厂Bean --> <bean id="myClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <!-- 请求代理Servlet路径 --> <property name="serviceUrl"> <value>http://localhost:8080/HessianServer/remoting/IHessianOfSpring</value> </property> <!-- 接口定义 --> <property name="serviceInterface"> <value>my.hessian.service.IHessianOfSpring</value> </property> </bean> </beans>
代码下载:
Hessian服务端:HessianServer, Hessian客户端:HessianClient
POI:
转:https://community.oracle.com/docs/DOC-887397
转:http://blog.csdn.net/spp_1987/article/details/13770637