RMI网络编程 重启server端程序,client端无法连接

来源:互联网 发布:windows远程桌面快捷键 编辑:程序博客网 时间:2024/06/07 05:25

当然,你也许跟我一样,碰到以下RefuseConnection的问题:

java.rmi.ConnectException: Connection refused to host: 146.222.94.55; nested exception is:

这个在我重启server端的程序以后,client端执行远程访问时产生的。

排查原因:

1. 有可能是client端对访问失败的历史,具有抵触情绪,一旦访问失败,第二次尝试就不行了。

一位老外,跟我遇到同样的问题:

http://stackoverflow.com/questions/1477121/reconnect-rmi-client-after-server-restart

I have an RMI server and a desktop RMI client. When I restart the server, I get errors in the client. Is it possible to restart the RMI connection without restarting the client?

网友回答:When the server is terminated, you will receive a ConnectException. After that you can just use Naming.lookup to get a new server object.

我想JDK1.5以后,这个naming的方法,我已经不适用了,抛弃。

网友回答:用Spring可以很好地解决这个问题。

If you're using Spring (namely RmiProxyFactoryBean), you can simply set the property refreshStubOnConnectFailure to true and the bean will survive a restart of the server.
<property name="refreshStubOnConnectFailure" value="true" />

没有使用spring,所以这个对我来说,没有什么意义

2.  针对程序本身进行debug,提出问题,为什么重启会产生问题呢?

百思不得其解,后来发现,是单例模式导致的。

这个注册服务器(getRegister)的过程,是在单例模式的类中执行的。第一次注册后,储存的静态变量

(stub对象)就没有被释放过。所以,重启server端程序以后,执行servlet,还是没有重新注册,从而导致错误。

正确的解决方法是,在单例模式的类中,要增加一个释放旧的stub对象的方法,在connection发生错误以后就可以调用。具体实现如下:

  1. private static DataServerInterface dataServer=null
  2.  
  3. private DataServer(){ 
  4.      
  5. public static void clearOldConnection(){ 
  6.     dataServer=null

当然,在servlet调用的过程中,要记得捕获异常,从而释放stub对象:

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  2.     // TODO Auto-generated method stub 
  3.     List<Weibo> weibos = null
  4.      
  5.     try
  6.     weibos = DataServer.getRmiService().getWeibos(); 
  7.  
  8.     for(Weibo item:weibos){ 
  9.         System.out.println(item.getContent()); 
  10.         response.getWriter().write(""+item.getContent()); 
  11.     }    
  12.      
  13.     }catch(Exception e){ 
  14.         System.out.println("Connection error when calling dataserver"); 
  15.         DataServer.clearOldConnection(); 
  16.         System.out.println("Due to exception,old connection has been cleaned"); 
  17.   
  18.     } 
  19.      
本文出自 “JAVAWeb开发” 博客,请务必保留此出处http://6221123.blog.51cto.com/6211123/1114680
原创粉丝点击