shenyijian
2024-12-02 eed72349910ec181532e5f7ce13677788a769d21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cn.gistack.by.nettyUpload;
 
 
import cn.gistack.by.nettyUpload.config.NettyHttpConfig;
import cn.gistack.by.picUpload.service.BaiyiPicService;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.RejectedExecutionHandler;
import io.netty.util.concurrent.SingleThreadEventExecutor;
import lombok.extern.slf4j.Slf4j;
import cn.gistack.resource.feign.IOssClient;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.concurrent.ThreadFactory;
 
/**
 *
 * @author chenhaijian
 * @date 2020-05-21 14:42
 */
@Slf4j
public class HttpUploadFileInitialzer extends ChannelInitializer<SocketChannel> {
    /**
     * 所有的工作线程的线程池。
     */
    private static EventExecutorGroup executorGroup = null;
 
    private NettyHttpConfig nettyHttpConfig;
 
 
    private  IOssClient iOssClient;
 
    private BaiyiPicService baiyiPicService;
 
 
    public HttpUploadFileInitialzer(NettyHttpConfig nettyHttpConfig,IOssClient iOssClient,BaiyiPicService baiyiPicService) {
        this.nettyHttpConfig = nettyHttpConfig;
        this.iOssClient = iOssClient;
        this.baiyiPicService = baiyiPicService;
 
        if (executorGroup == null) {
            ThreadFactory threadFactory = (new ThreadFactoryBuilder()).setNameFormat("netty-business-%d")
                .setDaemon(false).build();
            executorGroup = new DefaultEventExecutorGroup(nettyHttpConfig.getBusinessThreadsCoreNum(), threadFactory,
                nettyHttpConfig.getBusinessThreadsQueueCapacity(),
                new RejectedExecutionHandler() {
                    @Override
                    public void rejected(Runnable runnable, SingleThreadEventExecutor singleThreadEventExecutor) {
                        log.error("Server will discard the runnale {}.", runnable.toString());
                        singleThreadEventExecutor.shutdownGracefully();
                    }
                });
        }
    }
 
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("encoder", new HttpResponseEncoder());
        pipeline.addLast(new HttpContentCompressor());
        // 聚合器,负责将http聚合成完整的消息,而不是原始的多个部分
        pipeline.addLast(new HttpObjectAggregator(nettyHttpConfig.getMaxContentLength()));
        pipeline.addLast(executorGroup, "httpUploadFileHandler", new HttpUploadFileHandler(nettyHttpConfig,iOssClient,baiyiPicService));
    }
}