| Author |
how to set timeout to throw an exception if the server is down
|
kishoret tata
Greenhorn
Joined: Feb 15, 2004
Posts: 12
|
|
Hi, I am using starttime, endtime calcualtion to throw an exception if the server is down. Can any one help me how to use setSoTimeout() method insted of calculating startime, elasped time etc ... I want to thow an exception if the connection is not established in 10 minutes. .. Here is my code ... int timeout = 10 *1000; long startTime = System.currentTimeMillis(); long elapsedTime = 0; boolean connected = false; InetSocketAddress isa = new InetSocketAddress("xx.xx.xx.xxx",yyy); do { try { elapsedTime = System.currentTimeMillis() - startTime; channel = SocketChannel.open(isa); connected = channel.isConnected(); }catch (java.io.IOException ioe) { if (elapsedTime > timeout) throw new CommException("socket connection timeout."); } } while (elapsedTime < timeout && !connected); Thanks.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I haven't used this NIO stuff yet. It looks pretty cool. SocketChannel for instance implements InterruptableChannel so you can do an asynch close(). You might make a class (maybe inner?) that implements Runnable, waits for a while and then closes the channel. Exceptions will ensue no doubt. Just thinking out loud, imagine what ChannelWatcher would do with a channel argument and a number of milliseconds to wait ...
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
kishoret tata
Greenhorn
Joined: Feb 15, 2004
Posts: 12
|
|
Hi, I am not clear what you are saying. I searched lot on the net about this. I didn't see any method to set time out by the time of connecting to server. I would like to know is there any way to set timeout by the time establishing socket connection (insted of calculating startime and elapsed time etc ...). One more thing is setSoTimeout() is not working if i set by the time of reading data from socket channel . Any ideas why it is not working. Thanks.
|
 |
Ramchandran Gopalratnam
Greenhorn
Joined: Jan 12, 2005
Posts: 1
|
|
Hi , I too faced a problem where i wanted to timeout while connecting on an inetAocketAddress thru a SocketChannel .Unfortunately SocketChannel does not provide a connect(timeout) method . What i did was used the underlying Socket object to do the same . The code goes this way InetSocketAddress adress = new InetSocketAddress(...... SocketChannel channel = SocketChannel.open(); channel.socket().connect(socketAddress,2); where 2 indicates 2 milliseconds .. This works perfectly for blocking and nonblocking modes of the channel . Though not a very good way to do but a good work around .. Thanks
|
Ramchandran Gopalratnam(Anand)<br /> <br />India ..
|
 |
 |
|
|
subject: how to set timeout to throw an exception if the server is down
|
|
|