1

在 ChannelPipeline 中引入了解压器和压缩器,但运行时间太大,与特定类中引入的两种方法的执行时间相比。

@Override 
public ChannelPipeline getPipeline() throws Exception { 
    ChannelPipeline pipeline = pipeline(); 
    pipeline.addLast("decoder",new IcapRequestDecoder(maxInitialLineLength, maxIcapHeaderSize, maxHttpHeaderSize, maxChunkSize)); 
    pipeline.addLast("chunkAggregator",new IcapChunkAggregator(maxContentLength)); 
    pipeline.addLast("decompressor",new IcapContentDecompressor()); 
    pipeline.addLast("encoder",new IcapResponseEncoder()); 
    pipeline.addLast("chunkSeparator",new IcapChunkSeparator(maxContentLength)); 
    pipeline.addLast("handler", handler); 
    pipeline.addLast("compressor",new IcapContentCompressor()); 
    return pipeline; 
} 

可能是什么原因?

4

1 回答 1

0

我将扩展信息。

IcapContentCompress 类:

public class IcapContentCompressor extends SimpleChannelDownstreamHandler {

...

/**
 * Invoked when {@link Channel#write(Object)} is called.
 */
@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {        

            ...

    IcapResponse icapResponse = (IcapResponse) msg;

    // Get response message
    HttpResponse httpMessage = icapResponse.getHttpResponse();

    ...

    boolean compressed = isCompressed(httpMessage);
    if (compressed) {           
        String acceptEncoding = httpMessage.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
        compress(httpMessage, acceptEncoding);
    }

    }       

    ctx.sendDownstream(e);

}

压缩方法:

private void compress(HttpResponse httpMessage, String acceptEncoding) throws Exception {                       
    ZlibWrapper wrapper = determineWrapper(acceptEncoding);
    if (wrapper == null) {
       return;
    }

    EncoderEmbedder<ChannelBuffer>  encoder = new EncoderEmbedder<ChannelBuffer>(new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel));

    httpMessage.setHeader(
            HttpHeaders.Names.CONTENT_ENCODING,
            getTargetContentEncoding(wrapper));

    ChannelBuffer contentCompressed = ChannelBuffers.wrappedBuffer(
                                            encode(httpMessage.getContent(), encoder), 
                                            finishEncode(encoder));

    // Replace the content.
    httpMessage.setContent(contentCompressed);  
}   

当我们在特定的类中使用相同的方法而不在类 ChannelPipeline 中引入时,执行时间要少得多。

于 2012-12-05T08:19:57.140 回答