Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to set timeout to throw an exception if the server is down

 
kishoret tata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
kishoret tata
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic