use of java.nio.channels.ClosedByInterruptException in project iobserve-analysis by research-iobserve.
the class MultipleConnectionTcpReaderStage method execute.
@Override
protected void execute() {
try {
final ServerSocketChannel serverSocket = ServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(this.inputPort));
serverSocket.configureBlocking(false);
final Selector readSelector = Selector.open();
while (this.isActive()) {
final SocketChannel socketChannel = serverSocket.accept();
if (socketChannel != null) {
this.logger.debug("Connection from {}.", socketChannel.getRemoteAddress().toString());
// add socketChannel to list of channels
socketChannel.configureBlocking(false);
final SelectionKey key = socketChannel.register(readSelector, SelectionKey.OP_READ);
final Connection connection = new Connection(socketChannel, this.bufferSize);
key.attach(connection);
}
final int readReady = readSelector.selectNow();
if (readReady > 0) {
final Set<SelectionKey> selectedKeys = readSelector.selectedKeys();
final Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
while (keyIterator.hasNext()) {
final SelectionKey key = keyIterator.next();
this.readFromSocket(key);
keyIterator.remove();
}
selectedKeys.clear();
}
}
} catch (final ClosedByInterruptException e) {
this.logger.info("External shutdown called");
} catch (final IOException e) {
this.logger.error("Cannot establish listening port", e);
} finally {
this.workCompleted();
}
}
use of java.nio.channels.ClosedByInterruptException in project Bytecoder by mirkosertic.
the class FileChannelImpl method lock.
public FileLock lock(long position, long size, boolean shared) throws IOException {
ensureOpen();
if (shared && !readable)
throw new NonReadableChannelException();
if (!shared && !writable)
throw new NonWritableChannelException();
FileLockImpl fli = new FileLockImpl(this, position, size, shared);
FileLockTable flt = fileLockTable();
flt.add(fli);
boolean completed = false;
int ti = -1;
try {
begin();
ti = threads.add();
if (!isOpen())
return null;
int n;
do {
n = nd.lock(fd, true, position, size, shared);
} while ((n == FileDispatcher.INTERRUPTED) && isOpen());
if (isOpen()) {
if (n == FileDispatcher.RET_EX_LOCK) {
assert shared;
FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
flt.replace(fli, fli2);
fli = fli2;
}
completed = true;
}
} finally {
if (!completed)
flt.remove(fli);
threads.remove(ti);
try {
end(completed);
} catch (ClosedByInterruptException e) {
throw new FileLockInterruptionException();
}
}
return fli;
}
use of java.nio.channels.ClosedByInterruptException in project fluency by komamitsu.
the class MockMultiTCPServerWithMetrics method start.
@Override
public synchronized void start() throws Exception {
super.start();
if (executorService != null) {
return;
}
executorService = Executors.newSingleThreadExecutor();
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(getLocalPort()));
executorService.execute(new Runnable() {
@Override
public void run() {
while (executorService != null && !executorService.isTerminated()) {
try {
ByteBuffer buffer = ByteBuffer.allocate(8);
SocketAddress socketAddress = channel.receive(buffer);
assertEquals(0, buffer.position());
channel.send(buffer, socketAddress);
} catch (ClosedByInterruptException e) {
// Expected
} catch (IOException e) {
LOG.warn("Failed to receive or send heartbeat", e);
}
}
}
});
}
use of java.nio.channels.ClosedByInterruptException in project evosuite by EvoSuite.
the class EvoFileChannel method read.
private int read(ByteBuffer[] dsts, int offset, int length, AtomicInteger posToUpdate) throws IOException {
if (!isOpenForRead) {
throw new NonReadableChannelException();
}
throwExceptionIfClosed();
int counter = 0;
synchronized (readWriteMonitor) {
for (int j = offset; j < length; j++) {
ByteBuffer dst = dsts[j];
int r = dst.remaining();
for (int i = 0; i < r; i++) {
int b = NativeMockedIO.read(path, posToUpdate);
if (b < 0) {
// end of stream
return -1;
}
if (closed) {
throw new AsynchronousCloseException();
}
if (Thread.currentThread().isInterrupted()) {
close();
throw new ClosedByInterruptException();
}
dst.put((byte) b);
counter++;
}
}
}
return counter;
}
use of java.nio.channels.ClosedByInterruptException in project vespa by vespa-engine.
the class MockFDispatch method run.
public void run() {
try {
ServerSocketChannel channel = createServerSocket(listenPort);
channel.socket().setReuseAddress(true);
while (!Thread.currentThread().isInterrupted()) {
try {
// can now proceed and talk to us
synchronized (barrier) {
if (barrier != null) {
barrier.notify();
}
}
SocketChannel socketChannel = channel.accept();
connectionThreads.add(new ConnectionThread(socketChannel));
} catch (ClosedByInterruptException e) {
// We'll exit
} catch (ClosedChannelException e) {
return;
} catch (Exception e) {
log.log(Level.WARNING, "Unexpected error reading request", e);
}
}
channel.close();
} catch (IOException e) {
throw new ConfigurationException("Socket channel failure", e);
}
}
Aggregations