NIO with timeouts != NIO?

I

iksrazal

I have a class, DAOBySocket, used in a multi-user server environment .
Instead of a DB, my data is recieved via a client socket invokation to
a mainframe. Getting the connection typically takes several seconds.

I'd like to create the connection, and while waiting, parse my data
recieved as a big xml file into a format I can then send over the
socket, and perhaps do some other setup.

I think this can be accomplished via SocketChannel and finishConnect()
..

SocketChannel sc = SocketChannel.open();
InetSocketAddress addr new InetSocketAddress(address,port);
sc.configureBlocking(false);
while (!sc.finishConnect())
{
MyData mydata = parseXml(xmlStr);
}

sendData(sc, mydata);
sc.close();

However, I'm having a hard time visualizing how to handle a connection
time out. Also how to synch the xml task ending and a succesfull
connection - this code overwrites the same data while waiting. As I
understand it, SocketChannel doesn't have a timeout, and defaults to
blocking mode unless I invoke configureBlocking(false) .

Any ideas?
iksrazal
 
I

iksrazal

I have a class, DAOBySocket, used in a multi-user server environment .
Instead of a DB, my data is recieved via a client socket invokation to
a mainframe. Getting the connection typically takes several seconds.

I'd like to create the connection, and while waiting, parse my data
recieved as a big xml file into a format I can then send over the
socket, and perhaps do some other setup.

I think this can be accomplished via SocketChannel and finishConnect()
.

SocketChannel sc = SocketChannel.open();
InetSocketAddress addr new InetSocketAddress(address,port);
sc.configureBlocking(false);
while (!sc.finishConnect())
{
MyData mydata = parseXml(xmlStr);
}

sendData(sc, mydata);
sc.close();

However, I'm having a hard time visualizing how to handle a connection
time out. Also how to synch the xml task ending and a succesfull
connection - this code overwrites the same data while waiting. As I
understand it, SocketChannel doesn't have a timeout, and defaults to
blocking mode unless I invoke configureBlocking(false) .

Any ideas?
iksrazal


To answer my own question in case someone has the same problem, I
found this code that uses java.util.Timer that should solve the
problem.

import java.util.Timer;
import java.util.TimerTask;
import java.net.*;
import java.nio.channels.*;
import java.io.*;

/**
* Times out a socket using nio
*
*/
class TimedTask extends java.lang.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();

}

}

class TimerOut extends TimerTask {


Thread threadToTimeOut;

public TimerOut(Thread threadToTimeOut) {
this.threadToTimeOut = threadToTimeOut;
}

public void run() {
System.out.println("TimerOut running...");
threadToTimeOut.interrupt();
}
}

class Task implements Runnable {

public void run() {
try {
// Create a non-blocking socket channel on port 81
SocketChannel sChannel =
createSocketChannel("www.experts-exchange.com", 81);

// 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
}
} catch (Exception e) {
if (e instanceof InterruptedException) {
System.err.println("Connect was timed out!");
} else {
e.printStackTrace();
}
}
}

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;
}
}

Outsource to an American programmer living in brazil!
http://www.braziloutsource.com/
iksrazal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top