Spring使用远程服务之Hessian

来源:互联网 发布:caj viewer for mac 编辑:程序博客网 时间:2024/05/07 01:20

Hessian像RMI一样,使用二进制消息进行客户端和服务端的交互,它的二进制消息可以移植到其他非Java的语言中包括PHP、Python、C++和C#。因为Hessian是基于HTTP的,所以HessianSeriviceExporter实现为一个Spring MVC控制器。


HessianSeriviceExporter是一个SpringMVC控制器,它可以接收Hessian请求,并将这些请求翻译成对POJO的调用来,从而将POJO导出为一个Hessian服务



为了使用导出Hessian服务,我们需要执行两个额外的配置步骤:

1、在web.xml中配置Spring的DispatcherServlet,并把我们的应用部署为Web应用;

2、在Spring的配置文件中配置一个URL处理器,将Hessian服务的URL分发给对应的Hessian服务Bean。

下面我们距离说明

我的项目分层



web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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_3_0.xsd">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>  <!-- 在此处配置刚刚写的spring-hessian.xml的位置 -->  <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>            classpath:/spring-hessian.xml      </param-value>  </context-param> <servlet>      <!-- servlet-name保持与spring-hessian.xml中一致 -->      <servlet-name>HelloServiceExporter</servlet-name>      <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>  </servlet>  <servlet-mapping>      <servlet-name>HelloServiceExporter</servlet-name>      <url-pattern>/HelloService</url-pattern>  </servlet-mapping>  </web-app>

spring-hessian.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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"default-lazy-init="true">     <context:annotation-config />  <!-- 组件扫描,使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 --><context:component-scan base-package="hessian" /><!-- 自动装配 --><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />    <!-- Name保持与web.xml中的一致,web.xml下文中描述 -->      <bean name="HelloServiceExporter"          class="org.springframework.remoting.caucho.HessianServiceExporter">          <!-- service的ref与HelloServiceImpl中@Service中配置的一致 -->          <property name="service" ref="helloService" />          <!-- 接口的路径 -->          <property name="serviceInterface"              value="hessian.HelloService" />      </bean>  </beans>  

java类

package hessian;public interface HelloService {void sayHello(String name);}


package hessian;import org.springframework.stereotype.Service;@Service("helloService")  public class HelloServiceImpl implements HelloService {        /* (non-Javadoc)      * @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String)      */      @Override      public void sayHello(String name) {          System.out.println("Hello " + name + "!");      }    } 


客户端的调用

首先将Service的打成jar包加入到客户端程序中

测试代码

package hessionclient;import hessian.HelloService;import com.caucho.hessian.client.HessianProxyFactory;public class ArticleManager {public static void main(String[] args) {          try {              String url = "http://localhost:8080/SpringTest/HelloService";              HessianProxyFactory factory = new HessianProxyFactory();              HelloService helloService = (HelloService) factory.create(                      HelloService.class, url);              helloService.sayHello("张三");          } catch (Exception e) {              e.printStackTrace();          }      }  }

客户端调用图



参考:

《Spring实战(第3版)》

PS:代码主体来源于网络

3 1
原创粉丝点击