zoukankan      html  css  js  c++  java
  • 十五分钟学会用Hessian

    了解Hessian

    Hessian是远程调用的一种技术,和WebService类似,但不同的是较WebService而言,它更轻量级,更简单,更快速。关于Hessian更详细全面的介绍可以查看http://hessian.caucho.com/。下面可以花十五分钟的时间,学会怎么简单的使用Hessian。

    使用Hessian

    在Eclipse建立一个Maven webapp项目:

    image

    右键项目添加Hessian的依赖:

    image

    Maven随后会去中央仓库下载Hessian相关的jar包。

    添加一个提供Hessian服务的接口:

       1:  /**
       2:   * @ClassName: HessianService
       3:   * @Description: Hessian服务接口
       4:   * @author liping.action@gmail.com
       5:   * @date 2014-1-3 下午10:41:26
       6:   * 
       7:   */
       8:  public interface HessianService {
       9:      public String sayHello(String name);
      10:  }

    以及现实类:

       1:  public class HessianServiceImpl implements HessianService {
       2:      /*
       3:       * (non-Javadoc)
       4:       * 
       5:       * @see
       6:       * com.cnblogs.leefreeman.hessian.service.HessianService#sayHello(java.lang
       7:       * .String)
       8:       */
       9:      @Override
      10:      public String sayHello(String name) {
      11:          // TODO Auto-generated method stub
      12:          return "hello " + name;
      13:      }
      14:  }

    最后在web.xml中增加hessian相应的配置:

       1:  <servlet>
       2:         <servlet-name>service</servlet-name>
       3:         <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
       4:         <load-on-startup>1</load-on-startup>
       5:         <init-param>
       6:             <param-name>service-class</param-name>
       7:             <param-value>com.cnblogs.leefreeman.hessian.service.impl.HessianServiceImpl</param-value>
       8:         </init-param>
       9:      </servlet>
      10:      <servlet-mapping>
      11:         <servlet-name>service</servlet-name>
      12:         <url-pattern>/service</url-pattern>
      13:      </servlet-mapping>

    现在Hessian服务端就算完成,是不是觉得很简单。启动项目:

    image

    打开该页面说明Hessian服务端启动正常。接口下来编写客户端测试用例:

       1:  public class HessianServiceTest {
       2:      private final static String SERVICE_URL = "http://localhost:8080/hessian-project/service";
       3:   
       4:      @Test
       5:      public void testSayHello() {
       6:          HessianProxyFactory proxyFactory = new HessianProxyFactory();
       7:          try {
       8:              HessianService hessianService = (HessianService) proxyFactory
       9:                      .create(HessianService.class, SERVICE_URL);
      10:              System.out.println(hessianService.sayHello("liping"));
      11:          } catch (MalformedURLException e) {
      12:              e.printStackTrace();
      13:          }
      14:      }
      15:  }

    结果:

    image

    一个简单的Hession应用就完成了,相比搭建Webservice项目,要简单得多。

  • 相关阅读:
    Educational Codeforces Round 80 (Rated for Div. 2)
    2020 CCPC Wannafly Winter Camp
    Codeforces Round #613 (Div. 2)
    Codeforces Round #612 (Div. 2)
    Hello 2020
    Good Bye 2019
    Codeforces Round #590 (Div. 3)
    依赖注入
    Spring 拦截器
    rsync服务端一键安装rsync脚本(非源码)
  • 原文地址:https://www.cnblogs.com/leefreeman/p/3504418.html
Copyright © 2011-2022 走看看