ContextHandlerBuilder.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package cn.nosum.gateway.handler.build;
  2. import cn.nosum.common.annotation.Adaptive;
  3. import cn.nosum.gateway.handler.*;
  4. import io.netty.channel.ChannelInitializer;
  5. import io.netty.channel.socket.SocketChannel;
  6. import io.netty.handler.codec.http.HttpObjectAggregator;
  7. import io.netty.handler.codec.http.HttpRequestDecoder;
  8. import io.netty.handler.codec.http.HttpResponseEncoder;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. @Adaptive
  12. public class ContextHandlerBuilder implements HandlerBuilder{
  13. private Logger logger= LoggerFactory.getLogger(this.getClass());
  14. private static ChannelInitializer<SocketChannel> channelInitializer=null;
  15. @Override
  16. public ChannelInitializer<SocketChannel> build() {
  17. if (channelInitializer==null){
  18. channelInitializer = new ChannelInitializer<SocketChannel>() {
  19. // 客户端初始化处理
  20. protected void initChannel(SocketChannel client) throws Exception {
  21. // Netty对HTTP协议的封装,顺序有要求
  22. client.pipeline().addLast(new HttpRequestDecoder());
  23. client.pipeline().addLast(new HttpObjectAggregator(65535));//将多个消息转化成一个
  24. client.pipeline().addLast(new HttpResponseEncoder());
  25. client.pipeline().addLast(new FullHttpRequestHandler());
  26. client.pipeline().addLast(new SlotProcessHandler());
  27. // TODO 根据激活扩展点优化
  28. client.pipeline().addLast(new PreProcessHandler());
  29. client.pipeline().addLast(new FinalProcessHandler());
  30. }
  31. };
  32. }
  33. return channelInitializer;
  34. }
  35. }