use of org.jboss.netty.channel.ChannelFutureListener in project jstorm by alibaba.
the class NettyClient method closeChannel.
/**
* Avoid channel double close
*/
void closeChannel(final Channel channel) {
synchronized (channelClosing) {
if (closingChannel.contains(channel)) {
LOG.info(channel.toString() + " has already been closed");
return;
}
closingChannel.add(channel);
}
LOG.debug(channel.toString() + " begin to close");
ChannelFuture closeFuture = channel.close();
closeFuture.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
synchronized (channelClosing) {
closingChannel.remove(channel);
}
LOG.debug(channel.toString() + " closed.");
}
});
}
use of org.jboss.netty.channel.ChannelFutureListener in project opentsdb by OpenTSDB.
the class HttpQuery method sendFile.
/**
* Send a file (with zero-copy) to the client.
* This method doesn't provide any security guarantee. The caller is
* responsible for the argument they pass in.
* @param status The status of the request (e.g. 200 OK or 404 Not Found).
* @param path The path to the file to send to the client.
* @param max_age The expiration time of this entity, in seconds. This is
* not a timestamp, it's how old the resource is allowed to be in the client
* cache. See RFC 2616 section 14.9 for more information. Use 0 to disable
* caching.
*/
// Clears warning about RandomAccessFile not
@SuppressWarnings("resource")
public // being closed. It is closed in operationComplete().
void sendFile(final HttpResponseStatus status, final String path, final int max_age) throws IOException {
if (max_age < 0) {
throw new IllegalArgumentException("Negative max_age=" + max_age + " for path=" + path);
}
if (!channel().isConnected()) {
done();
return;
}
RandomAccessFile file;
try {
file = new RandomAccessFile(path, "r");
} catch (FileNotFoundException e) {
logWarn("File not found: " + e.getMessage());
if (getQueryString() != null && !getQueryString().isEmpty()) {
// Avoid potential recursion.
getQueryString().remove("png");
}
this.sendReply(HttpResponseStatus.NOT_FOUND, serializer.formatNotFoundV1());
return;
}
final long length = file.length();
{
final String mimetype = guessMimeTypeFromUri(path);
response().headers().set(HttpHeaders.Names.CONTENT_TYPE, mimetype == null ? "text/plain" : mimetype);
final long mtime = new File(path).lastModified();
if (mtime > 0) {
response().headers().set(HttpHeaders.Names.AGE, (System.currentTimeMillis() - mtime) / 1000);
} else {
logWarn("Found a file with mtime=" + mtime + ": " + path);
}
response().headers().set(HttpHeaders.Names.CACHE_CONTROL, "max-age=" + max_age);
HttpHeaders.setContentLength(response(), length);
channel().write(response());
}
final DefaultFileRegion region = new DefaultFileRegion(file.getChannel(), 0, length);
final ChannelFuture future = channel().write(region);
future.addListener(new ChannelFutureListener() {
public void operationComplete(final ChannelFuture future) {
region.releaseExternalResources();
done();
}
});
if (!HttpHeaders.isKeepAlive(request())) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
Aggregations