Hessian Spirng实例

Spring实例

  之前,我们做了很简单的纯Hessian的调用,虽然到此已经能够满足远程调用的需求了,但是我听说spring也能够访问hessian的远程服务,研究了一番,废话不多说,直接上示例。

 业务场景

  servlet的例子并未涉及到复杂对象的传输,这次我们搞复杂点,设计一个服务,通过远程调用的方式来找爸爸的儿子。

服务端

 环境搭建

 引入hessian、spring-mvc的相关jar包,后面会附上相关的pom文件配置,项目结构如下:

Hessian Spirng实例

 示例代码

  复杂的对象传输时,只需要类继承Serializable,保证在数据传输时能够序列化和反序列化,如下面的Father和Child类。

父亲:

 1 package example;

 2 

 3 import java.io.Serializable;

 4 

 5 /**

 6  * @author X

 7  */

 8 public class Father implements Serializable {

 9 

10     private static final long serialVersionUID = 1L;

11 

12     public Father(String name) {

13         this.name = name;

14     }

15 

16     private String name;

17 

18     public String getName() {

19         return name;

20     }

21 }
Father.java

儿子:

 1 package example;

 2 

 3 import java.io.Serializable;

 4 

 5 /**

 6  * @author X

 7  */

 8 public class Child implements Serializable {

 9 

10     private static final long serialVersionUID = 1L;

11 

12     public Child(String name) {

13         this.name = name;

14     }

15 

16     private String name;

17 

18     public String getName() {

19         return name;

20     }

21 }
Child.java

接送接口:

 1 package example;

 2 

 3 /**

 4  * @author X

 5  */

 6 public interface ShuttleService {

 7     String getCar();

 8 

 9     Child getChild(Father father);

10 }
ShuttleService.java

接送实现:

 1 package example;

 2 

 3 

 4 /**

 5  * @author X

 6  */

 7 public class ShuttleServiceImpl implements ShuttleService {

 8 

 9     public String getCar() {

10         return "小火车";

11     }

12 

13     public Child getChild(Father father) {

14         if (father != null)

15             return new Child(father.getName() + "的儿子");

16         return null;

17     }

18 }
ShuttleServiceImpl.java

spring配置:

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"

 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 5     <bean id="shuttle" class="example.ShuttleServiceImpl"/>

 6     <bean name="/shuttleService" class="org.springframework.remoting.caucho.HessianServiceExporter">

 7         <property name="service">

 8             <ref bean="shuttle"/>

 9         </property>

10         <property name="serviceInterface">

11             <value>example.ShuttleService</value>

12         </property>

13     </bean>

14 </beans>
hessian-spring.xml

web配置:

 1 <!DOCTYPE web-app PUBLIC

 2         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 3         "http://java.sun.com/dtd/web-app_2_3.dtd" >

 4 

 5 <web-app>

 6     <display-name>Demo</display-name>

 7     <servlet>

 8         <servlet-name>shuttle</servlet-name>

 9         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

10         <init-param>

11             <param-name>contextConfigLocation</param-name>

12             <param-value>/WEB-INF/hessian-spring.xml</param-value>

13         </init-param>

14         <load-on-startup>1</load-on-startup>

15     </servlet>

16     <servlet-mapping>

17         <servlet-name>shuttle</servlet-name>

18         <url-pattern>/rpc/*</url-pattern>

19     </servlet-mapping>

20 </web-app>
web.xml

依赖配置:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

 3     <parent>

 4         <artifactId>container</artifactId>

 5         <groupId>hessian.host</groupId>

 6         <version>1.0-SNAPSHOT</version>

 7     </parent>

 8     <modelVersion>4.0.0</modelVersion>

 9     <artifactId>spring</artifactId>

10     <packaging>war</packaging>

11     <name>spring Maven Webapp</name>

12     <url>http://maven.apache.org</url>

13     <dependencies>

14         <dependency>

15             <groupId>junit</groupId>

16             <artifactId>junit</artifactId>

17             <version>3.8.1</version>

18             <scope>test</scope>

19         </dependency>

20         <dependency>

21             <groupId>com.caucho</groupId>

22             <artifactId>hessian</artifactId>

23             <version>4.0.7</version>

24         </dependency>

25         <dependency>

26             <groupId>org.springframework</groupId>

27             <artifactId>spring-webmvc</artifactId>

28             <version>3.0.6.RELEASE</version>

29         </dependency>

30     </dependencies>

31     <build>

32         <finalName>spring</finalName>

33     </build>
pom.xml

客户端

 环境搭建

  引入hessian、spring-mvc的相关jar包,项目结构如下:

Hessian Spirng实例

调用:

 1 import example.Father;

 2 import example.ShuttleService;

 3 import org.springframework.context.ApplicationContext;

 4 import org.springframework.context.support.ClassPathXmlApplicationContext;

 5 

 6 import java.net.MalformedURLException;

 7 

 8 /**

 9  * @author X

10  */

11 public class Run {

12     public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {

13         ApplicationContext context = new ClassPathXmlApplicationContext("spring-rpc.xml");

14         ShuttleService ss = (ShuttleService) context.getBean("rpcClient");

15         System.out.println(ss.getCar());

16         System.out.println(ss.getChild(new Father("王老二")).getName());

17     }

18 }
Run.java

客户端spring配置:

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"

 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 5     <bean id="rpcClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">

 6         <property name="serviceUrl">

 7             <value>http://localhost:45/rpc/shuttleService</value>

 8         </property>

 9         <property name="serviceInterface">

10             <value>example.ShuttleService</value>

11         </property>

12     </bean>

13 </beans>
spring-rpc.xml

运行后输出结果如下:

Hessian Spirng实例

  

  注意在实例中我是把Father和Child和ShuttleService分别定义了两次,实际开发中不建议这样做,把需要调用的部分作为一个公共API供分别提供给客户端和服务端使用,否则可能会照成反序列化失败。

  把spring和hessian相结合后,无论是服务端还是客户端的业务代码中,已经没有一行与hessian相关的代码了,也就是说spring让我们的开发无关远程接口的实现了,这样我们就可只关注于开发而不必去关注远程调用怎么去实现。如果说我们哪天需要切换另外一个spring支持的远程访问接口,也只需要修改下配置文件就搞定了,so easy,妈妈再也不用担心我的实现代码了!

你可能感兴趣的:(hessian)