NIO with timeouts != NIO?

Discussion in 'Java' started by iksrazal, Jun 17, 2004.

  1. iksrazal

    iksrazal Guest

    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
     
    iksrazal, Jun 17, 2004
    #1
    1. Advertising

  2. iksrazal

    iksrazal Guest

    (iksrazal) wrote in message news:<>...
    > 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
     
    iksrazal, Jun 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Dutoit

    Dealing with Session State timeouts

    Justin Dutoit, Aug 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    Steve C. Orr, MCSD
    Aug 5, 2003
  2. Justin Dutoit

    Setting Session State timeouts

    Justin Dutoit, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    324
    S. Justin Gengo
    Aug 7, 2003
  3. hari

    Timeouts?

    hari, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    Nauzad Kapadia
    Nov 13, 2003
  4. Do
    Replies:
    2
    Views:
    366
    srinivas moorthy
    Dec 9, 2003
  5. =?Utf-8?B?SmFzb24=?=

    forms authentication timeouts and session timeouts

    =?Utf-8?B?SmFzb24=?=, Jun 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    453
    =?Utf-8?B?SmFzb24=?=
    Jun 22, 2004
Loading...

Share This Page