use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project flink by apache.
the class HandlerUtils method transferFile.
public static void transferFile(ChannelHandlerContext ctx, File file, HttpRequest httpRequest) throws FlinkException {
final RandomAccessFile randomAccessFile;
try {
randomAccessFile = new RandomAccessFile(file, "r");
} catch (FileNotFoundException e) {
throw new FlinkException("Can not find file " + file + ".", e);
}
try {
final long fileLength = randomAccessFile.length();
final FileChannel fileChannel = randomAccessFile.getChannel();
try {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_TYPE, "text/plain");
if (HttpHeaders.isKeepAlive(httpRequest)) {
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
HttpHeaders.setContentLength(response, fileLength);
// write the initial line and the header.
ctx.write(response);
// write the content.
final ChannelFuture lastContentFuture;
final GenericFutureListener<Future<? super Void>> completionListener = future -> {
fileChannel.close();
randomAccessFile.close();
};
if (ctx.pipeline().get(SslHandler.class) == null) {
ctx.write(new DefaultFileRegion(fileChannel, 0, fileLength), ctx.newProgressivePromise()).addListener(completionListener);
lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
} else {
lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(randomAccessFile, 0, fileLength, 8192)), ctx.newProgressivePromise()).addListener(completionListener);
// HttpChunkedInput will write the end marker (LastHttpContent) for us.
}
// close the connection, if no keep-alive is needed
if (!HttpHeaders.isKeepAlive(httpRequest)) {
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
} catch (IOException ex) {
fileChannel.close();
throw ex;
}
} catch (IOException ioe) {
try {
randomAccessFile.close();
} catch (IOException e) {
throw new FlinkException("Close file or channel error.", e);
}
throw new FlinkException("Could not transfer file " + file + " to the client.", ioe);
}
}
use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project herddb by diennea.
the class NettyChannel method sendOneWayMessage.
@Override
public void sendOneWayMessage(ByteBuf message, SendResultCallback callback) {
io.netty.channel.Channel _socket = this.socket;
if (_socket == null || !_socket.isOpen()) {
callback.messageSent(new Exception(this + " connection is closed"));
return;
}
if (LOGGER.isLoggable(Level.FINEST)) {
StringBuilder dumper = new StringBuilder();
ByteBufUtil.appendPrettyHexDump(dumper, message);
LOGGER.log(Level.FINEST, "Sending to {}: {}", new Object[] { _socket, dumper });
}
_socket.writeAndFlush(message).addListener(new GenericFutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
callback.messageSent(null);
} else {
LOGGER.log(Level.SEVERE, this + ": error " + future.cause(), future.cause());
callback.messageSent(future.cause());
close();
}
}
});
unflushedWrites.incrementAndGet();
}
Aggregations