• 用spirng和hessian构建分布式应用(远程接口)的方法(1)


    最近一期的《programmer》里几乎从头至尾在讲关于“J2EE without EJB”的事情,可怜的ejb啊,居然被描述成了遗产系统的重要组成部分。。。 

    其实有上面的结论,无外乎现在java里面的新技术已经几乎能完全取代ejb的优点,而克服ejb的缺点,entity bean和有状态的session bean已经机乎被视为垃圾,hibernate和spring大行其到,看看最进n期《programmer》中篇幅的比重就知道了。本来我个人的感觉是hibernate取代了entity bean,spring取代了session bean,但是ejb的远程调用用hibernate和spring的架构还取代不了,可是在最近的一期《programmer》中我发现了Hessian!更爽的是,发现了spring原来可以和Hessian结合使用!看来真的可以say byebye to ejb了。 

    看到这么振奋人心的消息,怎么能不亲自试验一下呢,于是上http://www.caucho.com/以迅雷不及掩耳盗铃之势下载了Hessian的src jar和bin jar。create一个工程,把这些相关的jar统统扔进去,配置和coding就可以开始了。首先,开了如下几个包: 



    whao.test.hessian.server 
    放远程服务的接口 

    whao.test.hessian.server.impl 
    放远程服务的实现类 

    whao.test.hessian.client 
    放客户端应用 



    1. whao.test.hessian.server中写一个MyService接口: 
    Java代码  收藏代码
    1. /* 
    2.  
    3.  * Created on 2005-7-25 
    4.  
    5.  * 
    6.  
    7.  */  
    8.   
    9. package whao.test.hessian.server;  
    10.   
    11.    
    12.   
    13. /** 
    14.  
    15.  * @author Hao Wei 
    16.  
    17.  * 
    18.  
    19.  */  
    20.   
    21. public interface MyService {  
    22.   
    23.     public String doSomething(String s);;  
    24.   
    25. }  



    2. whao.test.hessian.server.impl中写一个实现类 

    Java代码  收藏代码
    1. /* 
    2.  
    3.  * Created on 2005-7-25 
    4.  
    5.  * 
    6.  
    7.  */  
    8.   
    9. package whao.test.hessian.server.impl;  
    10.   
    11.    
    12.   
    13. import whao.test.hessian.server.MyService;  
    14.   
    15.    
    16.   
    17. /** 
    18.  
    19.  * @author Hao Wei 
    20.  
    21.  * 
    22.  
    23.  */  
    24.   
    25. public class MyServiceImpl implements MyService {  
    26.   
    27.    
    28.   
    29.     /* (non-Javadoc); 
    30.  
    31.      * @see whao.test.hessian.server.MyService#doSomething(java.lang.String); 
    32.  
    33.      */  
    34.   
    35.     public String doSomething(String s); {  
    36.   
    37.         return "HAHAHA: " + s;  
    38.   
    39.     }  
    40.   
    41. }  
    42.   
    43.    


    3. 配置远程服务 

             Hessian的远程服务要配置成servlet,配置如下: 
    Java代码  收藏代码
    1. <servlet>  
    2.   
    3.    <servlet-name>myservice</servlet-name>  
    4.   
    5.    <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>  
    6.   
    7.    <load-on-startup>1</load-on-startup>  
    8.   
    9.    <init-param>  
    10.   
    11.        <param-name>service-class</param-name>  
    12.   
    13.        <param-value>whao.test.hessian.server.impl.MyServiceImpl</param-value>  
    14.   
    15.    </init-param>  
    16.   
    17. </servlet>  
    18.   
    19. <servlet-mapping>  
    20.   
    21.    <servlet-name>myservice</servlet-name>  
    22.   
    23.    <url-pattern>/myservice</url-pattern>  
    24.   
    25. </servlet-mapping>  


    这样,当启动了这个web应用,这个远程服务就以url http://localhost:8080/test_web/myservice 的形式发布了。然后就是开发客户端来调用这个远程服务。 

    Java代码  收藏代码
    1. /* 
    2.  
    3.  * Created on 2005-7-25 
    4.  
    5.  * 
    6.  
    7.  */  
    8.   
    9. package whao.test.hessian.client;  
    10.   
    11.    
    12.   
    13. import whao.test.hessian.server.MyService;  
    14.   
    15.    
    16.   
    17. import com.caucho.hessian.client.HessianProxyFactory;  
    18.   
    19.    
    20.   
    21. /** 
    22.  
    23.  * @author Hao Wei 
    24.  
    25.  *   
    26.  
    27.  */  
    28.   
    29. public class TestMain {  
    30.   
    31.     public static void main(String[] args); throws Exception {  
    32.   
    33.         HessianProxyFactory proxyFactory = new HessianProxyFactory();;  
    34.   
    35.         MyService service = (MyService); proxyFactory.create(MyService.class,  
    36.   
    37.                 "http://localhost:8080/test_web/myservice");;  
    38.   
    39.         System.out.println(service.doSomething("xixixixi"););;  
    40.   
    41.         System.out.println("ok!");;  
    42.   
    43.     }  
    44.   
    45. }  
    46.   
    47.    


    运行一把,显示 

    HAHAHA:xixixi 

    ok! 

    不错不错,纯Hessian的远程调用就这样搞定了。继续研究《programmer》看到上面介绍用spring的远程访问解决方案来访问ejb的远程服务。什么?spring还有远程解决方案?没听说过嘛,看了《programmer》上的介绍,发现是真的。那么既然spring能访问ejb的远程服务,那么能访问Hessian的远程服务么?打开spring.jar看看,居然发现了名曰org.springframework.remoting.caucho.HessianProxyFactoryBean的类!夏昕真不厚道啊,再他的spring中文教程中居然匿掉了spring里这么好的东西!于是打开sping英文版reference,终于找到了用spring配置Hessian客户端的方法: 
    Java代码  收藏代码
    1. <bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
    2.   
    3.    <property name="serviceUrl">  
    4.   
    5.        <value>  
    6.   
    7.        http://localhost:8080/test_web/myservice  
    8.   
    9.        </value>  
    10.   
    11.    </property>  
    12.   
    13.    <property name="serviceInterface">  
    14.   
    15.        <value>whao.test.hessian.server.MyService</value>  
    16.   
    17.    </property>  
    18.   
    19. </bean>  

    然后客户端就可以这么玩啦: 

    Java代码  收藏代码
    1. /* 
    2.  
    3.  * Created on 2005-7-25 
    4.  
    5.  * 
    6.  
    7.  */  
    8.   
    9. package whao.test.hessian.client;  
    10.   
    11.    
    12.   
    13. import whao.test.hessian.server.MyService;  
    14.   
    15. import whao.util.spirng.SpringBeanFactory;  
    16.   
    17.    
    18.   
    19. import com.caucho.hessian.client.HessianProxyFactory;  
    20.   
    21.    
    22.   
    23. /** 
    24.  
    25.  * @author Hao Wei 
    26.  
    27.  *   
    28.  
    29.  */  
    30.   
    31. public class TestMain {  
    32.   
    33.    
    34.   
    35.     public static void main(String[] args); throws Exception {  
    36.   
    37.         testWithSpring();;  
    38.   
    39.     }  
    40.   
    41.     public static void testWithSpring();{  
    42.   
    43.         MyService service = (MyService);SpringBeanFactory.getBean("myService");;  
    44.   
    45.         System.out.println(service.doSomething("lllllllll"););;  
    46.   
    47.         System.out.println("ok!");;  
    48.   
    49.     }  
    50.   
    51.     public static void testWithoutSpring(); throws Exception {  
    52.   
    53.         HessianProxyFactory proxyFactory = new HessianProxyFactory();;  
    54.   
    55.         MyService service = (MyService); proxyFactory.create(MyService.class,  
    56.   
    57.                 "http://localhost:8080/test_web/myservice");;  
    58.   
    59.         System.out.println(service.doSomething("xixixixi"););;  
    60.   
    61.         System.out.println("ok!");;  
    62.   
    63.     }  
    64.   
    65. }  
    66.   
    67.    


    执行一下,输出是: 

    HAHAHA:lllllllll 

    ok! 

    spring真是个好东东,呵呵,其中的SpringBeanFactory是这样实现的: 
    Java代码  收藏代码
    1. /* 
    2.  
    3.  * Created on 2005-7-25 
    4.  
    5.  * 
    6.  
    7.  */  
    8.   
    9. package whao.util.spirng;  
    10.   
    11.    
    12.   
    13. import javax.servlet.ServletContext;  
    14.   
    15.    
    16.   
    17. import org.apache.commons.logging.Log;  
    18.   
    19. import org.apache.commons.logging.LogFactory;  
    20.   
    21. import org.springframework.context.ApplicationContext;  
    22.   
    23. import org.springframework.context.support.FileSystemXmlApplicationContext;  
    24.   
    25. import org.springframework.web.context.support.WebApplicationContextUtils;  
    26.   
    27.    
    28.   
    29. /** 
    30.  
    31.  * @author Hao Wei 
    32.  
    33.  * 
    34.  
    35.  */  
    36.   
    37. public class SpringBeanFactory {  
    38.   
    39.     private static final Log log = LogFactory.getLog(SpringBeanFactory.class);;  
    40.   
    41.          private static ApplicationContext ctx = null;  
    42.   
    43.    
    44.   
    45.     public static Object getBean(ServletContext context, String beanID); {  
    46.   
    47.         log.info("beanID=" + beanID);;  
    48.   
    49.         ApplicationContext ac = WebApplicationContextUtils  
    50.   
    51.                 .getWebApplicationContext(context);;  
    52.   
    53.         return ac.getBean(beanID);;  
    54.   
    55.     }  
    56.   
    57.       
    58.   
    59.     public static Object getBean(String beanID);{  
    60.   
    61.                    if(ctx == null);{  
    62.   
    63.                             ctx = new FileSystemXmlApplicationContext(  
    64.   
    65.                             "D:\\whao-work\\src\\test_web\\test_web\\WEB-INF\\applicationContext.xml");;  
    66.   
    67.                    }  
    68.   
    69.                    return ctx.getBean(beanID);;  
    70.   
    71.     }  
    72.   
    73. }  
    74.   
    75.    


    看到sping.jar的包org.springframework.remoting.caucho中还有另外两个类叫HessianClientInterceptor和HessianServiceExporter,看来貌似hessian服务端也可以和spring结合,还有客户端的Interceptor估计可以实现对远程服务的AOP,要继续研究一下了,待续吧
  • 相关阅读:
    mysql在CentOS6.3上安装
    hdfs高可用性(HDFS High Availability)
    如何做个好员工
    lock(3)——更新锁(U)、排它锁(X)、死锁及如何避免死锁
    锁(1)—— 锁粒度和层次结构
    lock(2)——创建及更新表过程中SQL SERVER锁资源分配情况
    HBase体系结构
    HDFS的shell操作
    Windows 使用 net use 命令
    Windows 使用 net 命令简介
  • 原文地址:https://www.cnblogs.com/chenying99/p/2555454.html
Copyright © 2020-2023  润新知