This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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.
 
(instanceof Sidekick)
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.
 
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
Boost this thread!