| |
"Java Programming: The Java Async IO Package"
Vol. 9, Issue 10, p. 38
Listing 1: Basic Use of AsyncSocketChannel
/* Create a new AsyncSocketChannel and connect it to a
* given hostname and portnumber.
*_____________________________________________________
*/
try {
AsyncSocketChannel aChannel = AsyncSocketChannel.open();
InetSocketAddress theAddress = new InetSocketAddress( hostname, portnumber );
IAsyncFuture future = aChannel.connect( theAddress );
/* The connect operation is asynchronous
* wait for completion on the future returned by the connect
* method
*/
future.waitForCompletion();
} catch ( Exception e ) {
// Deal with exception while opening and connecting the Channel
} // end try block
// Perform a read operation
// First set up the Byte Buffer
ByteBuffer readbuf = ByteBuffer.allocateDirect( BUFFERSIZE );
try {
// Request the read operation
IAsyncFuture future = channel.read( readbuf );
// wait for completion of read, up to 15 seconds
long bytesRead = future.getByteCount( 15000 );
} catch ( AsyncTimeoutException te ) {
// the wait for the read to complete timed out
} catch ( IOException ie ) {
// an IOException happened when performing the read
} catch ( ClosedChannelException che ) {
// the AsyncSocketChannel was closed before the read
} catch ( InterruptedException inte ) {
// the thread was interrupted while waiting
} // end try block
Listing 2: Simple use of Callback with a future
public void performRead() {
try {
// read
buffer.position( 0 );
buffer.limit( buffer.capacity() );
state = READ_PENDING;
IAsyncFuture future = channel.read( buffer );
// Set up the callback
future.addCompletionListener( this, state );
} catch (Exception e) {
System.out.println("Exception occurred on read request");
// Handle exception...
} // end try
} // end method performRead
// Callback method dealing with completed operations
public void futureCompleted( IAbstractAsyncFuture theFuture, Object userState ) {
if ( userState == READ_PENDING ) {
try {
long bytesread = aFuture.getByteCount();
//.... do processing ....
} catch (Exception e) {
//.... handle exceptions ...
}
} // end if
if ( userState == WRITE_PENDING ) {
try {
long byteswritten = aFuture.getByteCount();
//.... do processing ....
} catch (Exception e) {
//.... handle exceptions ...
}
} // end if
} // end method futureCompleted
Listing 3: Use of multi read operation
// Async Socket Channel: assume open and connected in earlier code
AsyncSocketChannel s;
ByteBuffer[] bufs = new ByteBuffer[ MAXBUFS ];
// Create the AsyncSocketChannelHelper
AsyncSocketChannelHelper helper = new AsyncSocketChannelHelper( s );
//Allocate the buffer array
for ( int i = 0 ; i < MAXBUFS ; i++ ) {
bufs[i] = ByteBuffer.allocateDirect( BUFFERSIZE );
} // end for
try {
// read
IAsyncMultiFuture future = helper.read( bufs );
// Use blocking to wait for completion...
long bytesRead = future.getByteCount( );
} catch (Exception e) {
System.out.println("Exception occurred on read request");
// Handle exception...
} // end try
Listing 4: Use of read operation with time-out
// Perform a read operation with a timeout
AsyncSocketChannel schannel;
static final int TIMEOUT = 15000; // 15 second timeout
AsyncSocketChannelHelper helper = new AsyncSocketChannelHelper( schannel );
// Set up the Byte Buffer
ByteBuffer readbuf = ByteBuffer.allocateDirect( BUFFERSIZE );
try {
// Request the read operation
IAsyncFuture future = helper.read( readbuf, TIMEOUT );
// wait for completion of read, or for the timeout to happen
long bytesRead = future.getByteCount( );
} catch ( AsyncTimeoutException ) {
// deal with the timeout of the operation
} catch ( Exception e ) {
// deal with the exception...
} // end try block
|
|