Operating System - HP-UX
1827802 Members
2257 Online
109969 Solutions
New Discussion

Re: java NIO select() call does not block

 
cthiebot
Occasional Advisor

java NIO select() call does not block

On HP-UX 11.11, PA-RISC server, with Java JVM greater than 1.4.1, the Java NIO select() call does not block leading to 100% cpu utilization.

I created a non-blocking SocketChannel and registered it with OP_CONNECT and OP_READ.

The following select() returns 0 without ever blocking:
java.nio.channels.Selector mxChannelsSelector;
int selectReturn = mxChannelsSelector.select(lTimeout);

The same code works with Java 1.4.1 on HP-UX 11.11. It works on Linux RedHat 5 + Java 1.6.

How to make it work on HP-UX with the latest Java JVM (1.6)?

Chris
4 REPLIES 4
cthiebot
Occasional Advisor

Re: java NIO select() call does not block

Here is a sample of my Java NIO code using the select() statement. The select() does not block on HP-UX 11.11 as well as HP-UX 11.31 (with Java 1.6).

import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.Set;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
SocketChannel mxSocketChannel = null;
Selector mxChannelsSelector = null;
try
{
mxSocketChannel = SocketChannel.open();
mxSocketChannel.configureBlocking(false);
InetSocketAddress mxRemoteAddress = new InetSocketAddress("localhost", 51421);
boolean t_connect = mxSocketChannel.connect(mxRemoteAddress);
int mnInterestOps = SelectionKey.OP_CONNECT;
//int mnInterestOps = SelectionKey.OP_CONNECT | SelectionKey.OP_READ;

//create the selector
mxChannelsSelector= SelectorProvider.provider().openSelector();

//register the channel with the selector indicating an interest in
// accepting new connections
SelectionKey x_key = mxSocketChannel.register(mxChannelsSelector, mnInterestOps);

//select loop
while(true)
{

//block until connect response is received
int selectReturn = mxChannelsSelector.select(0);

if (selectReturn == 0)
{
System.out.println("select returned 0");
}

} //end while(true)

} //end try block
catch (Exception ex)
{
System.out.println("exception " + ex);
}

}
}
Dennis Handly
Acclaimed Contributor

Re: java NIO select() call does not block

If nobody else has an answer here, you should contact the Response Center for java support.

You could also use tusc to see what system calls are being made.
cthiebot
Occasional Advisor

Re: java NIO select() call does not block

Actually, I noticed that the channel was already connected just after the connect() call. By testing the connect() return status and not setting the OP_CONNECT flag is the connection was already established, the select() call was behaving properly.

Here is the woking code sample:

public static void main(String[] args) {
SocketChannel mxSocketChannel = null;
Selector mxChannelsSelector = null;
int mnInterestOps = 0;
try
{
mxSocketChannel = SocketChannel.open();
mxSocketChannel.configureBlocking(false);
InetSocketAddress mxRemoteAddress = new InetSocketAddress("localhost", 51421);
boolean t_connect = mxSocketChannel.connect(mxRemoteAddress);

if (t_connect)
{
System.out.println("connected");
mnInterestOps = SelectionKey.OP_WRITE | SelectionKey.OP_READ ;
}
else
{
mnInterestOps = SelectionKey.OP_CONNECT;
}


//create the selector
mxChannelsSelector= SelectorProvider.provider().openSelector();

//register the channel with the selector indicating an interest
SelectionKey x_key = mxSocketChannel.register(mxChannelsSelector, mnInterestOps);


//select loop
while(true)
{

//block until connect response is received
int selectReturn = mxChannelsSelector.select(0);

if (selectReturn == 0)
{
System.out.println("select returned 0");
}

Set myKeys = mxChannelsSelector.selectedKeys();

if (!myKeys.isEmpty())
{
Iterator x_readyKeysIterator = myKeys.iterator();
// Iterate over the set of keys for which events are available
while (x_readyKeysIterator.hasNext())
{
SelectionKey x_selectionKey = (SelectionKey) x_readyKeysIterator.next();

// Remove selected key
x_readyKeysIterator.remove();

if (x_selectionKey.isValid())
{
if (x_selectionKey.isConnectable()) {
mxSocketChannel.finishConnect();
System.out.println("connection 2 accepted");

}
else if (x_selectionKey.isWritable()) {
System.out.println("writable channel, select return =" + selectReturn);
x_selectionKey.cancel();

}
else if (x_selectionKey.isReadable()) {
System.out.println("readable channel" + selectReturn);


}
}
else
{
//cancel the channel registration with this selector
x_selectionKey.cancel();
System.out.println("key not valid");
}

}
}
} //end while(true)

} //end try block
catch (Exception ex)
{
System.out.println("exception " + ex);
}

}

Chris
cthiebot
Occasional Advisor

Re: java NIO select() call does not block

See my reply above.
Chris