java - Spring RMI refreshStubOnConnectFailure not working -
i read following blog entry here, reconnect rmi client after server restart
it states when property refreshstubonconnectfailure set true client rmi try reconnect server. wrote following code test out before put changes system. started server , test program. after few messages server , client talking stopped server. got exception trace none of catch blocks entered. usual confused because thought @ least 1 of catch blocks executed. program output follows.
main starting hi there received hi there received hi there received jan 05, 2016 9:32:06 pm org.springframework.remoting.rmi.rmiproxyfactorybean handleremoteconnectfailure warning: not connect rmi service [rmi://127.0.0.1:1099/ping] - retrying exception in thread "main" org.springframework.remoting.remotelookupfailureexception: lookup of rmi stub failed; nested exception java.rmi.connectexception: connection refused host: 127.0.0.1; nested exception is: java.net.connectexception: connection refused @ org.springframework.remoting.rmi.rmiclientinterceptor.lookupstub(rmiclientinterceptor.java:215) @ org.springframework.remoting.rmi.rmiclientinterceptor.refreshandretry(rmiclientinterceptor.java:326) @ org.springframework.remoting.rmi.rmiclientinterceptor.handleremoteconnectfailure(rmiclientinterceptor.java:307) @ org.springframework.remoting.rmi.rmiclientinterceptor.invoke(rmiclientinterceptor.java:267) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:208) @ com.sun.proxy.$proxy1.sayhello(unknown source) @ com.edvs.main.testit.main(testit.java:34) caused by: java.rmi.connectexception: connection refused host: 127.0.0.1; nested exception is: java.net.connectexception: connection refused @ sun.rmi.transport.tcp.tcpendpoint.newsocket(tcpendpoint.java:619) @ sun.rmi.transport.tcp.tcpchannel.createconnection(tcpchannel.java:216) @ sun.rmi.transport.tcp.tcpchannel.newconnection(tcpchannel.java:202) @ sun.rmi.server.unicastref.newcall(unicastref.java:342) @ sun.rmi.registry.registryimpl_stub.lookup(unknown source) @ java.rmi.naming.lookup(naming.java:101) @ org.springframework.remoting.rmi.rmiclientinterceptor.lookupstub(rmiclientinterceptor.java:200) ... 7 more caused by: java.net.connectexception: connection refused @ java.net.plainsocketimpl.socketconnect(native method) @ java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:350) @ java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:206) @ java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:188) @ java.net.sockssocketimpl.connect(sockssocketimpl.java:392) @ java.net.socket.connect(socket.java:589) @ java.net.socket.connect(socket.java:538) @ java.net.socket.<init>(socket.java:434) @ java.net.socket.<init>(socket.java:211) @ sun.rmi.transport.proxy.rmidirectsocketfactory.createsocket(rmidirectsocketfactory.java:40) @ sun.rmi.transport.proxy.rmimastersocketfactory.createsocket(rmimastersocketfactory.java:148) @ sun.rmi.transport.tcp.tcpendpoint.newsocket(tcpendpoint.java:613) ... 13 more my code follows:
package com.edvs.main; import java.rmi.connectexception; import java.rmi.remoteexception import org.springframework.context.applicationcontext; import org.springframework.context.configurableapplicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.edvs.service.pingservice; public class testit { public static void main(string[] args) { string file = "bean.xml"; applicationcontext context = null; try { context = new classpathxmlapplicationcontext(file); } catch (exception e) { system.out.println("exception:" + e.getmessage()); system.exit(99); } system.out.println("main starting"); pingservice ps = (pingservice) context.getbean("pingbean"); boolean done = false; int count = 30; if (ps != null) { while (!done && count > 0) { try { if (ps.sayhello("annapolis").equals("hi there")) { system.out.println("hi there received"); } } catch (connectexception ce) { system.out.println("connectionexception"); /* * happened server see if * should retry connection. */ if (ce.getmessage().contains("connection refused")) { system.out.println("connection refused"); } } catch (remoteexception e) { system.out.println("remoteexception:" + e.getmessage()); } count--; try { thread.yield(); thread.sleep(3000); } catch (interruptedexception e) { done = true; } } } ((configurableapplicationcontext) context).close(); system.out.println("test has ended."); } } bean.xml:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="pingbean" class="org.springframework.remoting.rmi.rmiproxyfactorybean"> <property name="serviceurl" value="rmi://127.0.0.1:1099/ping" /> <property name="serviceinterface" value="com.edvs.service.pingservice" /> <property name="refreshstubonconnectfailure" value="true" /> <property name="lookupstubonstartup" value="false" /> </bean> none of exceptions caught. application terminates. if comment out refreshstubonconnectfailure connectionexception exception thrown, don't know how restart connection once server restarted.
for completeness , others gaze at, adding final test code. tests server , continues converse server.
package com.edvs.main; import java.net.malformedurlexception; import java.rmi.connectexception; import java.rmi.naming; import java.rmi.notboundexception; import java.rmi.remote; import java.rmi.remoteexception; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.context.applicationcontext; import org.springframework.context.configurableapplicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.remoting.rmi.rmiproxyfactorybean; import com.edvs.service.pingservice; public class testit { static log log = logfactory.getlog(testit.class.getname()); public static void main(string[] args) { string file = "bean.xml"; applicationcontext context = null; try { context = new classpathxmlapplicationcontext(file); } catch (exception e) { system.out.println("exception:" + e.getmessage()); system.exit(99); } system.out.println("main starting"); log.info("foo"); boolean done = false; int count = 30; rmiproxyfactorybean rpfb = (rmiproxyfactorybean) context.getbean("&pingbean"); string serviceurl = rpfb.getserviceurl(); /* * see if server there startup. */ while (true) { if (null != isserverthere(serviceurl)) { /* * server there continue our processing. */ break; } else { /* * server not there sleep , try again. */ try { thread.yield(); thread.sleep(3000); } catch (interruptedexception e) { system.exit(33); } } } pingservice ps = (pingservice) context.getbean("pingbean"); try { log.info("service name:" + ps.getservicename()); log.info("all:" + ps.tostring()); log.info("serviceurl:" + rpfb.getserviceurl()); } catch (remoteexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } while (!done && count > 0) { try { if (ps.sayhello("annapolis").equals("hi there")) { log.info("hi there received"); } } catch (connectexception ce) { log.info("connectionexception"); /* * happened server see if * should retry connection. */ if (ce.getmessage().contains("connection refused")) { log.info("connection refused"); ps = (pingservice) isserverthere(serviceurl); } } catch (remoteexception e) { system.out.println("remoteexception:" + e.getmessage()); } count--; try { thread.yield(); thread.sleep(3000); } catch (interruptedexception e) { done = true; } } ((configurableapplicationcontext) context).close(); system.out.println("test has ended."); } static private remote isserverthere(string serviceurl) { remote remote = null; try { remote = naming.lookup(serviceurl); } catch (malformedurlexception | remoteexception | notboundexception e) { log.error("exception:" + e.getmessage()); } return remote; } }
you're getting connection refused to registry. no amount of re-looking going fix that, because regsitry isn't running. that's need fix.
the property you're setting takes effect when connection refusal to remote object.
Comments
Post a Comment