I appreciate your help. Sorry if I'm not expressing my self clearly.
"Why are u trying to use Timer?"
I have two problems:
1) I need to do multiple client socket invokations simulatenously, which can time out.
2) I want to use threads for these calls.
I can get sockets to time out:
import java.util.Timer;
import java.util.TimerTask;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.io.*;
/**
Times out a socket using nio
*/
class NIOTest extends Object
{
static final int TIMEOUT = 10000;
public static void main(String[] args)
{
Thread task = new Thread(new Task());
TimerOut to = new TimerOut(task);
new Timer(true).schedule((TimerTask) to,TIMEOUT);
System.out.println(TIMEOUT + " milliseconds to socket timeout...");
task.start();
}
static class TimerOut extends TimerTask
{
Thread threadToTimeOut;
public TimerOut(Thread threadToTimeOut)
{
this.threadToTimeOut = threadToTimeOut;
}
public void run()
{
System.out.println("TimerOut running...");
threadToTimeOut.interrupt();
}
}
static class Task implements Runnable
{
Charset ascii = Charset.forName("US-ASCII");
String sendme;
public Task ()
{
;
}
public Task (String var)
{
this.sendme = var;
}
public void run()
{
SocketChannel sChannel = null;
try
{
// Create a non-blocking socket channel on port 2510
sChannel = createSocketChannel("10.200.200.144", 2510);
// Before the socket is usable, the connection must be completed
// by calling finishConnect(), which is non-blocking
while (!sChannel.finishConnect())
{
Thread.sleep(100);
// Do something else
}
Thread.sleep(5000);
System.out.println(readFromChannel(sChannel));
Thread.sleep(15000);
}
catch (Exception e)
{
if (e instanceof InterruptedException)
{
System.err.println("Connecton timed out!");
}
else
{
e.printStackTrace();
}
}
finally
{
try
{
if (null != sChannel)
{
sChannel.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public SocketChannel createSocketChannel(String hostName, int port) throws IOException
{
// Create a non-blocking socket channel
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(false);
// Send a connection request to the server; this method is non-blocking
sChannel.connect(new InetSocketAddress(hostName, port));
return sChannel;
}
private void writeToChannel (SocketChannel sChannel, String send) throws IOException
{
CharBuffer cbuf = CharBuffer.wrap(send);
ByteBuffer buf = this.ascii.encode(cbuf);
sChannel.write(buf);
}
private String readFromChannel (SocketChannel sChannel) throws IOException
{
CharsetDecoder decoder = this.ascii.newDecoder();
ByteBuffer buf = ByteBuffer.allocate(1024);
sChannel.read(buf);
buf.flip();
CharBuffer cbuf = ascii.decode(buf);
return cbuf.toString();
}
}
}
Excuse the nio, but this is a simple example. It reads the data successfully
and then simulates a timeout.
Now, _WHY_ did I think I can do this in PooledExecutor?
http://altair.cs.oswego.edu/pipermail/concurrency-interest/2003-June/000459.html It's a simple example, but I thought was possible to do this. Maybe not.
Thanks for your paitence,
iksrazal