use of java.nio.channels.AsynchronousCloseException in project gradle by gradle.
the class CyclicBarrierHttpServer method start.
void start() {
// Note: this is implemented using raw sockets. Originally implemented using Jetty, but some concurrency problems there caused Jetty to hang
try {
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(0));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
executor = Executors.newCachedThreadPool();
executor.execute(new Runnable() {
public void run() {
int i = 0;
while (true) {
try {
SocketChannel connection;
try {
connection = serverSocket.accept();
} catch (AsynchronousCloseException e) {
// Socket has been closed, so we're stopping
return;
} catch (ClosedChannelException e) {
// Socket has been closed, so we're stopping
return;
}
try {
OutputStream outputStream = Channels.newOutputStream(connection);
System.out.println("Handle connection request no." + (++i));
handleConnection(outputStream);
outputStream.flush();
} finally {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleConnection(OutputStream outputStream) throws IOException {
System.out.println("Handling HTTP request");
synchronized (lock) {
if (connected) {
System.out.println("Received unexpected connection.");
outputStream.write("HTTP/1.1 500 Received an unexpected connection.\r\nConnection: close\r\nContent-length: 0\r\n\r\n".getBytes());
return;
}
System.out.println("Connection received");
connected = true;
lock.notifyAll();
long expiry = monotonicClockMillis() + 30000;
while (!released && !stopped) {
long delay = expiry - monotonicClockMillis();
if (delay <= 0) {
System.out.println("Timeout waiting for client to be released.");
outputStream.write("HTTP/1.1 500 Timeout waiting for client to be released.\r\nConnection: close\r\nContent-length: 0\r\n\r\n".getBytes());
return;
}
try {
lock.wait(delay);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
if (stopped) {
System.out.println("Releasing client on stop.");
outputStream.write("HTTP/1.1 500 Server stopped.\r\nConnection: close\r\nContent-length: 0\r\n\r\n".getBytes());
return;
}
connected = false;
released = false;
lock.notifyAll();
}
System.out.println("Sending response to client");
outputStream.write("HTTP/1.1 200 Ok.\r\nConnection: close\r\nContent-length: 0\r\n\r\n".getBytes());
}
});
}
use of java.nio.channels.AsynchronousCloseException in project hadoop by apache.
the class DataXceiverServer method run.
@Override
public void run() {
Peer peer = null;
while (datanode.shouldRun && !datanode.shutdownForUpgrade) {
try {
peer = peerServer.accept();
// Make sure the xceiver count is not exceeded
int curXceiverCount = datanode.getXceiverCount();
if (curXceiverCount > maxXceiverCount) {
throw new IOException("Xceiver count " + curXceiverCount + " exceeds the limit of concurrent xcievers: " + maxXceiverCount);
}
new Daemon(datanode.threadGroup, DataXceiver.create(peer, datanode, this)).start();
} catch (SocketTimeoutException ignored) {
// wake up to see if should continue to run
} catch (AsynchronousCloseException ace) {
// but not in other circumstances
if (datanode.shouldRun && !datanode.shutdownForUpgrade) {
LOG.warn(datanode.getDisplayName() + ":DataXceiverServer: ", ace);
}
} catch (IOException ie) {
IOUtils.cleanup(null, peer);
LOG.warn(datanode.getDisplayName() + ":DataXceiverServer: ", ie);
} catch (OutOfMemoryError ie) {
IOUtils.cleanup(null, peer);
// DataNode can run out of memory if there is too many transfers.
// Log the event, Sleep for 30 seconds, other transfers may complete by
// then.
LOG.error("DataNode is out of memory. Will retry in 30 seconds.", ie);
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException e) {
// ignore
}
} catch (Throwable te) {
LOG.error(datanode.getDisplayName() + ":DataXceiverServer: Exiting due to: ", te);
datanode.shouldRun = false;
}
}
// Close the server to stop reception of more requests.
try {
peerServer.close();
closed = true;
} catch (IOException ie) {
LOG.warn(datanode.getDisplayName() + " :DataXceiverServer: close exception", ie);
}
// if in restart prep stage, notify peers before closing them.
if (datanode.shutdownForUpgrade) {
restartNotifyPeers();
// Each thread needs some time to process it. If a thread needs
// to send an OOB message to the client, but blocked on network for
// long time, we need to force its termination.
LOG.info("Shutting down DataXceiverServer before restart");
// Allow roughly up to 2 seconds.
for (int i = 0; getNumPeers() > 0 && i < 10; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// ignore
}
}
}
// Close all peers.
closeAllPeers();
}
use of java.nio.channels.AsynchronousCloseException in project robovm by robovm.
the class ServerSocketChannelTest method test_accept_configureBlocking.
/**
* Regression test for HARMONY-6375
*/
public void test_accept_configureBlocking() throws Exception {
InetSocketAddress localAddr = new InetSocketAddress("localhost", 0);
serverChannel.socket().bind(localAddr);
// configure the channel non-blocking
// when it is accepting in main thread
new Thread() {
public void run() {
try {
Thread.sleep(TIME_UNIT);
serverChannel.configureBlocking(false);
serverChannel.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
try {
serverChannel.accept();
fail("should throw AsynchronousCloseException");
} catch (AsynchronousCloseException e) {
// expected
}
serverChannel.close();
}
use of java.nio.channels.AsynchronousCloseException in project robovm by robovm.
the class AsynchronousCloseExceptionTest method test_Constructor.
/**
* @tests {@link java.nio.channels.AsynchronousCloseException#AsynchronousCloseException()}
*/
public void test_Constructor() {
AsynchronousCloseException e = new AsynchronousCloseException();
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
use of java.nio.channels.AsynchronousCloseException in project robovm by robovm.
the class DatagramChannelTest method testReadWrite_changeBlock_Empty.
public void testReadWrite_changeBlock_Empty() throws Exception {
// empty buf
byte[] sourceArray = "".getBytes();
byte[] targetArray = new byte[CAPACITY_NORMAL];
// bind and connect
this.channel1.socket().bind(localAddr2);
this.channel1.connect(localAddr1);
this.channel2.socket().bind(localAddr1);
this.channel2.connect(localAddr2);
// write
ByteBuffer sourceBuf = ByteBuffer.wrap(sourceArray);
assertEquals(0, this.channel1.write(sourceBuf));
// read
ByteBuffer targetBuf = ByteBuffer.wrap(targetArray);
// empty message let the reader blocked
new Thread() {
public void run() {
try {
Thread.sleep(TIME_UNIT);
channel2.configureBlocking(false);
Thread.sleep(TIME_UNIT * 5);
channel2.close();
} catch (Exception e) {
// do nothing
}
}
}.start();
try {
assertTrue(this.channel2.isBlocking());
this.channel2.read(targetBuf);
fail("Should throw AsynchronousCloseException");
} catch (AsynchronousCloseException e) {
assertFalse(this.channel2.isBlocking());
// OK.
}
}
Aggregations