Exception on reconnect attempt

Post here all your questions related with SmartFoxServer Java API

Moderators: Bax, Lapo, rjgtav

Exception on reconnect attempt

Postby Tekkman » 22 May 2009, 13:00

Hi all,

I'm writing a java class that needs to be very robust in recovering from potential network outages/pc crashes etc.

One of the tests is how it reconnects if smartfox server is closed suddenly.

I've added the SFSEvent.onConnection and SFSEvent.onConnectionLost event listeners and set it so a reconnect is attempted when either of the above get called:

Code: Select all
if (event.getName().equals(SFSEvent.onConnection))
                        {
         if (event.getParams().getBool("success"))
                                  {
              System.out.println("Great, successfull connected!"+event.getParams().getString("error"));
            //next we try to login...
            sfs.login(sfoxzone, sfoxuser, "");
                   }
                        else
                                 {
            System.out.println("Ouch, connection failed because of: "+event.getParams().getString("error"));
            //retry connect
            sfs.connect(IP, 9339);
            }

                       }
else if (event.getName().equals(SFSEvent.onConnectionLost))
                        {
         System.out.println("connection lost - reconnect...");
         sfs.connect(IP, 9339);
         }



the class will keep requesting the connect once smartfox server is closed (onConnectionLost event) and keep trying as the onConnection listener returns "fail"

However once the server is restarted it throws the exception below - it looks like the connect requests are coming in too fast before the server is fully initialised:

java.net.ConnectException: Connection refused: connect
at sun.nio.ch.Net.connect(Native Method)

I can sort of make this more reliable by putting in a wait command before each connect retry, it still throws the exception but will retry again and connect successfully but this is not a great solution,

I notice there's a sfs.onError(Exception e) method but not sure how to use this to capture and handle the exception

what's the best way of checking the server is ready to recieve a connect request?

Thanks,
Tekkman
 
Posts: 3
Joined: 22 May 2009, 12:37

Postby patso » 25 May 2009, 06:32

Hi,

Could you provide the full stack trace, please.

Also does you only see this exception in the console(in the debug output) or it's not caught by the java client API code(stops the execution of the program for example)?

About the sfs.onError method - it's for internal use - all methods of SmartFoxClient class that are not documented are not intended to be used from external code.
patso
 
Posts: 380
Joined: 13 Nov 2006, 13:44
Location: Sofia, Bulgaria

Postby Tekkman » 26 May 2009, 10:37

Hi, thanks for the reply

Here's the stack trace below, the exception is'nt caught by the java code so will stop the execution of the program.
I have only put a simple try/catch(Exception e) aound the sfs.connect() call so far. It does'nt enter it.

The only way to improve it has been to put a thread.sleep(5000) before the sfs.connect(), it still throws the same exception but not as many times (the exception is duplicated several times in the debug output) and usually will then connect ok.

Is there a connect exception limit for the server or smartfox client object?
maybe it succeeds when using the thread.sleep because i'm not hitting to connect exceptions as many times?

Thanks,


Code: Select all
26-May-2009 12:41:16 it.gotoandplay.smartfoxclient.http.HttpConnection send
INFO: [ Send ]: sfsHttp=connect
java.net.ConnectException: Connection refused: connect
   at sun.nio.ch.Net.connect(Native Method)
   at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
   at sun.nio.ch.SocketAdaptor.connect(Unknown Source)
   at sun.nio.ch.SocketAdaptor.connect(Unknown Source)
   at org.xsocket.stream.io.impl.IoProvider.openSocket(IoProvider.java:356)
   at org.xsocket.stream.io.impl.IoProvider.createClientIoHandler(IoProvider.java:219)
   at org.xsocket.stream.Connection.<init>(Connection.java:162)
   at org.xsocket.stream.NonBlockingConnection.<init>(NonBlockingConnection.java:396)
   at org.xsocket.stream.NonBlockingConnection.<init>(NonBlockingConnection.java:209)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.connect(XMLSocket.java:134)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.connect(SmartFoxClient.java:1237)
   at com.nhm.dlltest.SocketConnector.handleEvent(SocketConnector.java:301)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.dispatchConnectionError(SmartFoxClient.java:4550)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.handleEvent(SmartFoxClient.java:4431)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.send(HttpConnection.java:165)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.connect(HttpConnection.java:68)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.tryBlueBoxConnection(SmartFoxClient.java:4498)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.onError(SmartFoxClient.java:4471)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.fireErrorEvent(XMLSocket.java:206)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.connect(XMLSocket.java:140)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.connect(SmartFoxClient.java:1237)
   at com.nhm.dlltest.SocketConnector.handleEvent(SocketConnector.java:301)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.dispatchConnectionError(SmartFoxClient.java:4550)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.handleEvent(SmartFoxClient.java:4431)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.send(HttpConnection.java:165)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.connect(HttpConnection.java:68)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.tryBlueBoxConnection(SmartFoxClient.java:4498)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.onError(SmartFoxClient.java:4471)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.fireErrorEvent(XMLSocket.java:206)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.connect(XMLSocket.java:140)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.connect(SmartFoxClient.java:1237)
   at com.nhm.dlltest.SocketConnector.handleEvent(SocketConnector.java:301)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.dispatchConnectionError(SmartFoxClient.java:4550)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.handleEvent(SmartFoxClient.java:4431)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.send(HttpConnection.java:165)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.connect(HttpConnection.java:68)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.tryBlueBoxConnection(SmartFoxClient.java:4498)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.onError(SmartFoxClient.java:4471)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.fireErrorEvent(XMLSocket.java:206)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.connect(XMLSocket.java:140)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.connect(SmartFoxClient.java:1237)
   at com.nhm.dlltest.SocketConnector.handleEvent(SocketConnector.java:301)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.dispatchConnectionError(SmartFoxClient.java:4550)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.handleEvent(SmartFoxClient.java:4431)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.send(HttpConnection.java:165)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.connect(HttpConnection.java:68)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.tryBlueBoxConnection(SmartFoxClient.java:4498)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.onError(SmartFoxClient.java:4471)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.fireErrorEvent(XMLSocket.java:206)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.connect(XMLSocket.java:140)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.connect(SmartFoxClient.java:1237)
   at com.nhm.dlltest.SocketConnector.handleEvent(SocketConnector.java:301)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.dispatchConnectionError(SmartFoxClient.java:4550)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.handleEvent(SmartFoxClient.java:4431)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.send(HttpConnection.java:165)
   at it.gotoandplay.smartfoxclient.http.HttpConnection.connect(HttpConnection.java:68)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.tryBlueBoxConnection(SmartFoxClient.java:4498)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.onError(SmartFoxClient.java:4471)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.fireErrorEvent(XMLSocket.java:206)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.connect(XMLSocket.java:140)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.connect(SmartFoxClient.java:1237)
   at com.nhm.dlltest.SocketConnector.handleEvent(SocketConnector.java:337)
   at it.gotoandplay.smartfoxclient.SFSEventDispatcher.dispatchEvent(SFSEventDispatcher.java:96)
   at it.gotoandplay.smartfoxclient.SmartFoxClient.onClose(SmartFoxClient.java:4465)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocket.fireDisconnectEvent(XMLSocket.java:196)
   at it.gotoandplay.utils.net.xmlsocket.XMLSocketClientHandler.onDisconnect(XMLSocketClientHandler.java:36)
   at org.xsocket.stream.NonBlockingConnection.onDisconnectEvent(NonBlockingConnection.java:856)
   at org.xsocket.stream.Connection$HandlerCallback.onDisconnect(Connection.java:1589)
   at org.xsocket.stream.io.impl.IoMultithreadedHandler$IOEventHandler$3.run(IoMultithreadedHandler.java:181)
   at org.xsocket.stream.io.impl.IoMultithreadedHandler$TaskQueueProcessor.processTask(IoMultithreadedHandler.java:277)
   at org.xsocket.stream.io.impl.IoMultithreadedHandler$TaskQueueProcessor.run(IoMultithreadedHandler.java:265)
   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
   at java.lang.Thread.run(Unknown Source)
Tekkman
 
Posts: 3
Joined: 22 May 2009, 12:37

Postby patso » 27 May 2009, 12:15

Would you try with the latest version of the API.
patso
 
Posts: 380
Joined: 13 Nov 2006, 13:44
Location: Sofia, Bulgaria

Postby Tekkman » 01 Jun 2009, 16:16

Thanks patso,

I'm using the new Mina API now, I can catch the IOExceptions on connect fails now, however i've got a new uncaught exception when i kill the smartfox server (only seeing it in log4J as a warning and it's not stopping the application's execution):

Code: Select all
[ ] 2009-06-01 18:15:50,156  WARN oService-8 andplay.utils.net.xmlsocket.XMLSocketClientHandler - [/127.0.0.1:9339] EXCEPTION, please implement it.gotoandplay.utils.net.xmlsocket.XMLSocketClientHandler.exceptionCaught() for proper handling:
java.io.IOException: An existing connection was forcibly closed by the remote host
   at sun.nio.ch.SocketDispatcher.read0(Native Method)
   at sun.nio.ch.SocketDispatcher.read(Unknown Source)
   at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
   at sun.nio.ch.IOUtil.read(Unknown Source)
   at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor.read(SocketIoProcessor.java:218)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor.process(SocketIoProcessor.java:198)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor.access$400(SocketIoProcessor.java:45)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor$Worker.run(SocketIoProcessor.java:485)
   at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
   at java.lang.Thread.run(Unknown Source)


I'm trying to implement the
it.gotoandplay.utils.net.xmlsocket.XMLSocketClientHandler.exceptionCaught() with my own custom exception class that extends the super XMLSocketClientHandler but can't see how to get my custom class to override the one being called from within the xmlsocket.jar library since all my application is calling is the sfs.connect() method.

Apologies if i'm being a bit dim, not too experienced with overrides etc.


thanks,
Tekkman
 
Posts: 3
Joined: 22 May 2009, 12:37

Postby EricB » 19 Jul 2010, 16:05

Tekkman wrote:Thanks patso,

I'm using the new Mina [...], however i've got a new uncaught exception when i kill the smartfox server [...]

Code: Select all
[ ] 2009-06-01 18:15:50,156  WARN oService-8 andplay.utils.net.xmlsocket.XMLSocketClientHandler - [/127.0.0.1:9339] EXCEPTION, please implement it.gotoandplay.utils.net.xmlsocket.XMLSocketClientHandler.exceptionCaught() for proper handling:
java.io.IOException: An existing connection was forcibly closed by the remote host
   at sun.nio.ch.SocketDispatcher.read0(Native Method)
   at sun.nio.ch.SocketDispatcher.read(Unknown Source)
   at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
   at sun.nio.ch.IOUtil.read(Unknown Source)
   at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor.read(SocketIoProcessor.java:218)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor.process(SocketIoProcessor.java:198)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor.access$400(SocketIoProcessor.java:45)
   at org.apache.mina.transport.socket.nio.SocketIoProcessor$Worker.run(SocketIoProcessor.java:485)
   at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
   at java.lang.Thread.run(Unknown Source)



Ditto, I get that exception too, also when there SFX disconnects users; my client shows that exception too.
Any hint on where to catch it? The SFS client is hiding the XML socket implementation and it seems that it's around there...
EricB
 
Posts: 1
Joined: 19 Jul 2010, 15:59


Return to Java2SE / Android Client API

Who is online

Users browsing this forum: No registered users and 2 guests