架设hessian服务

我需要用hessian做一个获取自增ID的服务,用于解决自增ID同步瓶颈的问题. 这里介绍一下这个服务从无到有的过程

1. 新建一个java web项目,项目名字起为:IDSeqFactroy.

2.在项目源文件目录里新建一个接口(interface), 名字为:IDSeqInf, 并在这个接口里定义一个方法: public int getIDSeq();

4.在源文件里再建一个类,名字为:IDSeqImp, 这个类实现IDSeqInf接口.  实现的 getIDSeq() 方法里返回一个测试数字, return 123;

5.接下来配置web.xml , 下面贴上web.xml的源码的servlet部分,:

  <servlet>
   <servlet-name>CreateSeqID</servlet-name>
   <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
   
     <init-param>
   <param-name>home-class</param-name>
   <param-value>com.moonflew.pirate_IDSeqFactroy.IDSeqImp</param-value>
  </init-param>
  <init-param>
   <param-name>home-api</param-name>
   <param-value>com.moonflew.pirate_IDSeqFactroy.IDSeqInf</param-value>
  </init-param>
  </servlet>
 
  <servlet-mapping>
   <servlet-name>CreateSeqID</servlet-name>
   <url-pattern>/CreateSeqID</url-pattern>
  </servlet-mapping>

6.项目引入hessian.jar包

上面就完成了一个最基本的hessian服务, 下面对这个服务做一下测试.

1.用webservice启动上面的hessian服务.

2.新建一个测试项目,在项目中添加一个测试类,引入hessian.jar包和IDSeqInf接口源文件,在main方法中添加如下代码:

public static void main(String[] args) {
  HessianProxyFactory factory = new HessianProxyFactory();
  try {
   String url = "http://localhost:9888/CreateSeqID";
   factory.setOverloadEnabled(true);
   IDSeqInf seqInf = (IDSeqInf) factory.create(IDSeqInf.class, url);
  
   int i = seqInf.getIDSeq();
   System.out.println(i);
  
  } catch (Exception e) {  
   System.out.println("hessian client exception: " + e.getMessage() + "  " + e.getStackTrace());
  }
 }

3.上一步中url中的http://localhost:9888 是我WebService的地址,CreateSeqID是servlet映射(servlet-mapping)
4.console打印: 123  测试成功!

 

测试的时候可能会报一些异常,如果遇到异常,首先检查一下hessian.jar的版本. 我的webservice是resin4, hessian.jar也是4.

 

请大神指教,下面的问题不知道该如何去查了 com.caucho.hessian.client.HessianConnectionException: 500: java.net.SocketTimeoutException: Read timed out at com.caucho.hessian.client.HessianURLConnection.sendRequest(HessianURLConnection.java:145) ~[hessian-4.0.37.jar:3.1.0] at com.caucho.hessian.client.HessianProxy.sendRequest(HessianProxy.java:296) ~[hessian-4.0.37.jar:3.1.0] at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:171) ~[hessian-4.0.37.jar:3.1.0] at com.sun.proxy.$Proxy28.insertTVStation(Unknown Source) ~[na:na] at com.kuyun.grab.epggrab.service.impl.FullDoseServiceImpl.handle(FullDoseServiceImpl.java:472) [FullDoseServiceImpl.class:na] at com.kuyun.grab.epggrab.service.impl.FullDoseServiceImpl.fullDosehandle(FullDoseServiceImpl.java:430) [FullDoseServiceImpl.class:na] at com.kuyun.grab.epggrab.thread.FullDoseThread.run(FullDoseThread.java:43) [FullDoseThread.class:na] at java.lang.Thread.run(Thread.java:722) [na:1.7.0_17] Caused by: java.net.SocketTimeoutException: Read timed out at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_17] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_17] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_17] at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[na:1.7.0_17] at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674) ~[na:1.7.0_17] at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672) ~[na:1.7.0_17] at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_17] at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670) ~[na:1.7.0_17] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243) ~[na:1.7.0_17] at com.caucho.hessian.client.HessianURLConnection.sendRequest(HessianURLConnection.java:125) ~[hessian-4.0.37.jar:3.1.0] ... 7 common frames omitted Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.7.0_17] at java.net.SocketInputStream.read(SocketInputStream.java:150) ~[na:1.7.0_17] at java.net.SocketInputStream.read(SocketInputStream.java:121) ~[na:1.7.0_17] at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) ~[na:1.7.0_17] at java.io.BufferedInputStream.read1(BufferedInputStream.java:275) ~[na:1.7.0_17] at java.io.BufferedInputStream.read(BufferedInputStream.java:334) ~[na:1.7.0_17] at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:633) ~[na:1.7.0_17] at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579) ~[na:1.7.0_17] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322) ~[na:1.7.0_17] at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468) ~[na:1.7.0_17] at com.caucho.hessian.client.HessianURLConnection.sendRequest(HessianURLConnection.java:112) ~[hessian-4.0.37.jar:3.1.0] ... 7 common frames omitted 上面试报错信息 这个问题纠结好久了,具体原因不知道该怎么查,测试服务上面总会超时,正式服务上面就没有这种现象。 请赐教, 在线等。 微笑
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页