Search in sources :

Example 6 with AsynchronousCloseException

use of java.nio.channels.AsynchronousCloseException in project jimfs by google.

the class JimfsFileChannelTest method testAsynchronousClose.

@Test
public void testAsynchronousClose() throws Exception {
    RegularFile file = regularFile(10);
    final FileChannel channel = channel(file, READ, WRITE);
    // ensure all operations on the channel will block
    file.writeLock().lock();
    ExecutorService executor = Executors.newCachedThreadPool();
    CountDownLatch latch = new CountDownLatch(BLOCKING_OP_COUNT);
    List<Future<?>> futures = queueAllBlockingOperations(channel, executor, latch);
    // wait for all the threads to have started running
    latch.await();
    // then ensure time for operations to start blocking
    Uninterruptibles.sleepUninterruptibly(20, MILLISECONDS);
    // close channel on this thread
    channel.close();
    // AsynchronousCloseException
    for (Future<?> future : futures) {
        try {
            future.get();
            fail();
        } catch (ExecutionException expected) {
            assertThat(expected.getCause()).named("blocking thread exception").isInstanceOf(AsynchronousCloseException.class);
        }
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) FileChannel(java.nio.channels.FileChannel) ExecutorService(java.util.concurrent.ExecutorService) SettableFuture(com.google.common.util.concurrent.SettableFuture) Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 7 with AsynchronousCloseException

use of java.nio.channels.AsynchronousCloseException in project jimfs by google.

the class JimfsFileChannelTest method testCloseByInterrupt.

@Test
public void testCloseByInterrupt() throws Exception {
    RegularFile file = regularFile(10);
    final FileChannel channel = channel(file, READ, WRITE);
    // ensure all operations on the channel will block
    file.writeLock().lock();
    ExecutorService executor = Executors.newCachedThreadPool();
    final CountDownLatch threadStartLatch = new CountDownLatch(1);
    final SettableFuture<Throwable> interruptException = SettableFuture.create();
    // This thread, being the first to run, will be blocking on the interruptible lock (the byte
    // file's write lock) and as such will be interrupted properly... the other threads will be
    // blocked on the lock that guards the position field and the specification that only one method
    // on the channel will be in progress at a time. That lock is not interruptible, so we must
    // interrupt this thread.
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            threadStartLatch.countDown();
            try {
                channel.write(ByteBuffer.allocate(20));
                interruptException.set(null);
            } catch (Throwable e) {
                interruptException.set(e);
            }
        }
    });
    thread.start();
    // let the thread start running
    threadStartLatch.await();
    // then ensure time for thread to start blocking on the write lock
    Uninterruptibles.sleepUninterruptibly(10, MILLISECONDS);
    CountDownLatch blockingStartLatch = new CountDownLatch(BLOCKING_OP_COUNT);
    List<Future<?>> futures = queueAllBlockingOperations(channel, executor, blockingStartLatch);
    // wait for all blocking threads to start
    blockingStartLatch.await();
    // then ensure time for the operations to start blocking
    Uninterruptibles.sleepUninterruptibly(20, MILLISECONDS);
    // interrupting this blocking thread closes the channel and makes all the other threads
    // throw AsynchronousCloseException... the operation on this thread should throw
    // ClosedByInterruptException
    thread.interrupt();
    // get the exception that caused the interrupted operation to terminate
    assertThat(interruptException.get(200, MILLISECONDS)).named("interrupted thread exception").isInstanceOf(ClosedByInterruptException.class);
    // different thread, closed the channel)
    for (Future<?> future : futures) {
        try {
            future.get();
            fail();
        } catch (ExecutionException expected) {
            assertThat(expected.getCause()).named("blocking thread exception").isInstanceOf(AsynchronousCloseException.class);
        }
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) FileChannel(java.nio.channels.FileChannel) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) SettableFuture(com.google.common.util.concurrent.SettableFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 8 with AsynchronousCloseException

use of java.nio.channels.AsynchronousCloseException in project gradle by gradle.

the class InputForwarder method start.

public InputForwarder start() {
    lifecycleLock.lock();
    try {
        if (started) {
            throw new IllegalStateException("input forwarder has already been started");
        }
        disconnectableInput = new DisconnectableInputStream(input, bufferSize);
        outputBuffer = new LineBufferingOutputStream(handler, bufferSize);
        forwardingExecuter = executorFactory.create("forward input");
        forwardingExecuter.execute(new Runnable() {

            public void run() {
                byte[] buffer = new byte[bufferSize];
                int readCount;
                Throwable readFailure = null;
                try {
                    while (true) {
                        try {
                            readCount = disconnectableInput.read(buffer, 0, bufferSize);
                            if (readCount < 0) {
                                break;
                            }
                        } catch (AsynchronousCloseException e) {
                            break;
                        } catch (IOException e) {
                            readFailure = e;
                            break;
                        }
                        outputBuffer.write(buffer, 0, readCount);
                    }
                    // will flush any unterminated lines out synchronously
                    outputBuffer.flush();
                } catch (IOException e) {
                    // should not happen
                    throw UncheckedException.throwAsUncheckedException(e);
                } finally {
                    handler.endOfStream(readFailure);
                }
            }
        });
        started = true;
    } finally {
        lifecycleLock.unlock();
    }
    return this;
}
Also used : DisconnectableInputStream(org.gradle.util.DisconnectableInputStream) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) LineBufferingOutputStream(org.gradle.internal.io.LineBufferingOutputStream) IOException(java.io.IOException)

Example 9 with AsynchronousCloseException

use of java.nio.channels.AsynchronousCloseException in project jetty.project by eclipse.

the class InputStreamResponseListener method onContent.

@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
    if (content.remaining() == 0) {
        if (LOG.isDebugEnabled())
            LOG.debug("Skipped empty content {}", content);
        callback.succeeded();
        return;
    }
    boolean closed;
    synchronized (lock) {
        closed = this.closed;
        if (!closed) {
            if (LOG.isDebugEnabled())
                LOG.debug("Queueing content {}", content);
            chunks.add(new DeferredContentProvider.Chunk(content, callback));
            lock.notifyAll();
        }
    }
    if (closed) {
        if (LOG.isDebugEnabled())
            LOG.debug("InputStream closed, ignored content {}", content);
        callback.failed(new AsynchronousCloseException());
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException)

Example 10 with AsynchronousCloseException

use of java.nio.channels.AsynchronousCloseException in project jetty.project by eclipse.

the class HttpDestination method close.

public void close() {
    abort(new AsynchronousCloseException());
    if (LOG.isDebugEnabled())
        LOG.debug("Closed {}", this);
    connectionPool.close();
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException)

Aggregations

AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)15 IOException (java.io.IOException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)6 SocketException (java.net.SocketException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 ExecutorService (java.util.concurrent.ExecutorService)3 Test (org.junit.Test)3 SettableFuture (com.google.common.util.concurrent.SettableFuture)2 InetSocketAddress (java.net.InetSocketAddress)2 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 NotYetBoundException (java.nio.channels.NotYetBoundException)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 SocketChannel (java.nio.channels.SocketChannel)2 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)2 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 EOFException (java.io.EOFException)1