我是靠谱客的博主 沉默枕头,最近开发中收集的这篇文章主要介绍java rmi网络中断_RMI网络编程开发之三 重启server端程序,client端无法连接,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

当然,你也许跟我一样,碰到以下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.

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

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

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

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

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

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

privatestaticDataServerInterface dataServer=null;

privateDataServer(){

}

publicstaticvoidclearOldConnection(){

dataServer=null;

}

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

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

// TODO Auto-generated method stub

List weibos = null;

try{

weibos = DataServer.getRmiService().getWeibos();

for(Weibo item:weibos){

System.out.println(item.getContent());

response.getWriter().write(""+item.getContent());

}

}catch(Exception e){

System.out.println("Connection error when calling dataserver");

DataServer.clearOldConnection();

System.out.println("Due to exception,old connection has been cleaned");

}

}

最后

以上就是沉默枕头为你收集整理的java rmi网络中断_RMI网络编程开发之三 重启server端程序,client端无法连接的全部内容,希望文章能够帮你解决java rmi网络中断_RMI网络编程开发之三 重启server端程序,client端无法连接所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部