当然,你也许跟我一样,碰到以下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发生错误以后就可以调用。具体实现如下:
- 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");
- }
- }