zoukankan      html  css  js  c++  java
  • Spring使用远程服务之Hessian

    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

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="3.0"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    7.   <display-name></display-name>   
    8.   <welcome-file-list>  
    9.     <welcome-file>index.jsp</welcome-file>  
    10.   </welcome-file-list>  
    11.   <listener>  
    12. <listener-class>  
    13. org.springframework.web.context.ContextLoaderListener  
    14. </listener-class>  
    15. </listener>  
    16.       <!-- 在此处配置刚刚写的spring-hessian.xml的位置 -->    
    17.     <context-param>    
    18.         <param-name>contextConfigLocation</param-name>    
    19.         <param-value>      
    20.             classpath:/spring-hessian.xml    
    21.         </param-value>    
    22.     </context-param>   
    23.       
    24.       
    25.     <servlet>    
    26.     <!-- servlet-name保持与spring-hessian.xml中一致 -->    
    27.     <servlet-name>HelloServiceExporter</servlet-name>    
    28.     <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>    
    29.     </servlet>    
    30.     <servlet-mapping>    
    31.         <servlet-name>HelloServiceExporter</servlet-name>    
    32.         <url-pattern>/HelloService</url-pattern>    
    33.     </servlet-mapping>    
    34.   
    35. </web-app>  


    spring-hessian.xml

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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.     xmlns:jee="http://www.springframework.org/schema/jee"  
    5.     xmlns:tx="http://www.springframework.org/schema/tx"   
    6.     xmlns:context="http://www.springframework.org/schema/context"  
    7.     xmlns:aop="http://www.springframework.org/schema/aop"  
    8.     xmlns:p="http://www.springframework.org/schema/p"  
    9.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    10.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    11.      http://www.springframework.org/schema/tx  
    12.      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    13.      http://www.springframework.org/schema/jee  
    14.      http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
    15.      http://www.springframework.org/schema/aop  
    16.      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
    17.      http://www.springframework.org/schema/context  
    18.      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    19.      http://activemq.apache.org/schema/core  
    20.      http://activemq.apache.org/schema/core/activemq-core.xsd"  
    21.     default-lazy-init="true">   
    22.       
    23.     <context:annotation-config />  
    24.     <!-- 组件扫描,使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->  
    25.     <context:component-scan base-package="hessian" />  
    26.   
    27.     <!-- 自动装配 -->  
    28.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  
    29.       
    30.       
    31.     <!-- Name保持与web.xml中的一致,web.xml下文中描述 -->    
    32.     <bean name="HelloServiceExporter"    
    33.         class="org.springframework.remoting.caucho.HessianServiceExporter">    
    34.         <!-- service的ref与HelloServiceImpl中@Service中配置的一致 -->    
    35.         <property name="service" ref="helloService" />    
    36.         <!-- 接口的路径 -->    
    37.         <property name="serviceInterface"    
    38.             value="hessian.HelloService" />    
    39.     </bean>    
    40. </beans>    


    java类

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package hessian;  
    2.   
    3. public interface HelloService {  
    4.   
    5.     void sayHello(String name);  
    6.   
    7. }  



    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package hessian;  
    2.   
    3. import org.springframework.stereotype.Service;  
    4.   
    5. @Service("helloService")    
    6. public class HelloServiceImpl implements HelloService {    
    7.     
    8.     /* (non-Javadoc)  
    9.      * @see com.gsoft.geloin.service.HelloService#sayHello(java.lang.String)  
    10.      */    
    11.     @Override    
    12.     public void sayHello(String name) {    
    13.         System.out.println("Hello " + name + "!");    
    14.     }    
    15.     
    16. }   



    客户端的调用

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

    测试代码

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package hessionclient;  
    2.   
    3. import hessian.HelloService;  
    4.   
    5. import com.caucho.hessian.client.HessianProxyFactory;  
    6.   
    7. public class ArticleManager {  
    8.     public static void main(String[] args) {    
    9.         try {    
    10.             String url = "http://localhost:8080/SpringTest/HelloService";    
    11.             HessianProxyFactory factory = new HessianProxyFactory();    
    12.             HelloService helloService = (HelloService) factory.create(    
    13.                     HelloService.class, url);    
    14.             helloService.sayHello("张三");    
    15.         } catch (Exception e) {    
    16.             e.printStackTrace();    
    17.         }    
    18.     }    
    19. }  


    客户端调用图

  • 相关阅读:
    solr dataimport 数据导入源码分析(九)
    正确理解ThreadLocal
    solr dataimport 数据导入源码分析(六)
    solr dataimport 数据导入源码分析(七)
    solr dataimport 数据导入源码分析(八)
    solr dataimport 数据导入源码分析(一)
    solr dataimport 数据导入源码分析(五)
    OpenGL光照、键盘
    OpenGL着色
    OpenGL纹理映射
  • 原文地址:https://www.cnblogs.com/eaglepan/p/4646791.html
Copyright © 2011-2022 走看看