RMI网络编程开发之三 重启server端程序,client端无法连接
2013-01-11 14:51
573 查看
当然,你也许跟我一样,碰到以下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发生错误以后就可以调用。具体实现如下:
当然,在servlet调用的过程中,要记得捕获异常,从而释放stub对象:
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发生错误以后就可以调用。具体实现如下:
private static DataServerInterface dataServer=null; private DataServer(){ } public static void clearOldConnection(){ dataServer=null; }
当然,在servlet调用的过程中,要记得捕获异常,从而释放stub对象:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub List<Weibo> 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"); } }
相关文章推荐
- RMI网络编程 重启server端程序,client端无法连接
- Java在Client/Server网络中的应用-Java基础-Java-编程开发
- 详解Linux服务器最大tcp连接数 网络编程 在tcp应用中,server事先在某个固定端口监听,client主动发起连接,经过三路握手后建立tcp连接。那么对单机,其最大并发tcp连接数是多少?
- RMI网络编程开发之二 如何搭建基于JDK1.5的分布式JAVA RMI 程序
- RMI网络编程开发之二 如何搭建基于JDK1.5的分布式JAVA RMI 程序
- RMI网络编程开发之二 如何搭建基于JDK1.5的分布式JAVA RMI 程序
- Java 在Client/Server 网络中的应用-Java基础-Java-编程开发
- RMI网络编程开发之二 如何搭建基于JDK1.5的分布式JAVA RMI 程序
- Linux网络编程:基于TCP的程序开发回顾篇
- 解决 web服务器部署常见问题,server application unavailable 和 程序无法连接数据库 的问题(asp.net 2.0 + oracle9i + winXP)
- 启动SQL 2005的SQL Server Configuration Manager时显示无法连接到WMI提供程序
- NAT网络部分客户端无法连接Server的解决方法
- 实现server程序,支持多个client同时连接,为每一个client分配一个进程
- Python案例-网络编程-socket入门-server&client
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- Delphi 10.2 Linux 程序开发环境部署的基本步骤(网络连接方式要选择桥接或者是Host Only)
- Linux网络编程:基于UDP的程序开发回顾篇
- 解决 web服务器部署常见问题,server application unavailable 和 程序无法连接数据库 的问题(asp.net 2.0 + oracle9i + winXP)
- Linux网络编程:基于TCP的程序开发回顾篇