Search in sources :

Example 21 with ChannelFutureListener

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.");
        }
    });
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener)

Example 22 with ChannelFutureListener

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);
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) RandomAccessFile(java.io.RandomAccessFile) FileNotFoundException(java.io.FileNotFoundException) DefaultFileRegion(org.jboss.netty.channel.DefaultFileRegion) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)22 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)22 Channel (org.jboss.netty.channel.Channel)10 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)6 ConnectException (java.net.ConnectException)5 InetSocketAddress (java.net.InetSocketAddress)4 URL (java.net.URL)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 CamelException (org.apache.camel.CamelException)3 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)3 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)3 FetchException (com.continuuity.weave.kafka.client.FetchException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CamelExchangeException (org.apache.camel.CamelExchangeException)2 ChannelException (org.jboss.netty.channel.ChannelException)2 ChannelHandlerContext (org.jboss.netty.channel.ChannelHandlerContext)2