io.netty.bootstrap.Bootstrap.channel()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(181)

本文整理了Java中io.netty.bootstrap.Bootstrap.channel()方法的一些代码示例,展示了Bootstrap.channel()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bootstrap.channel()方法的具体详情如下:
包路径:io.netty.bootstrap.Bootstrap
类名称:Bootstrap
方法名:channel

Bootstrap.channel介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

private Bootstrap buildBootstrap(URI uri, boolean isSecure) {
  Bootstrap bootstrap = new Bootstrap();
  bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class)
      .handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel channel) throws Exception {
          configureChannel(channel.config());
          ChannelPipeline pipeline = channel.pipeline();
          if (isSecure) {
            Assert.notNull(sslContext, "sslContext should not be null");
            pipeline.addLast(sslContext.newHandler(channel.alloc(), uri.getHost(), uri.getPort()));
          }
          pipeline.addLast(new HttpClientCodec());
          pipeline.addLast(new HttpObjectAggregator(maxResponseSize));
          if (readTimeout > 0) {
            pipeline.addLast(new ReadTimeoutHandler(readTimeout,
                TimeUnit.MILLISECONDS));
          }
        }
      });
  return bootstrap;
}

代码示例来源:origin: apache/incubator-shardingsphere

private void groupsEpoll(final Bootstrap bootstrap) {
  bootstrap.group(workerGroup)
      .channel(EpollSocketChannel.class)
      .option(EpollChannelOption.TCP_CORK, true)
      .option(EpollChannelOption.SO_KEEPALIVE, true)
      .option(EpollChannelOption.SO_BACKLOG, 128)
      .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

代码示例来源:origin: testcontainers/testcontainers-java

public EventLoopGroup kqueueGroup() {
  EventLoopGroup nioEventLoopGroup = new KQueueEventLoopGroup(0, createThreadFactory());
  bootstrap.group(nioEventLoopGroup).channel(KQueueDomainSocketChannel.class)
      .handler(new ChannelInitializer<KQueueDomainSocketChannel>() {
        @Override
        protected void initChannel(final KQueueDomainSocketChannel channel) throws Exception {
          channel.pipeline().addLast(new HttpClientCodec());
          channel.pipeline().addLast(new HttpContentDecompressor());
        }
      });
  return nioEventLoopGroup;
}

代码示例来源:origin: line/armeria

(AddressResolverGroup<InetSocketAddress>) addressResolverGroupFactory.apply(workerGroup);
final Bootstrap baseBootstrap = new Bootstrap();
baseBootstrap.channel(TransportType.socketChannelType(workerGroup));
baseBootstrap.resolver(addressResolverGroup);
  @SuppressWarnings("unchecked")
  final ChannelOption<Object> castOption = (ChannelOption<Object>) option;
  baseBootstrap.option(castOption, value);
});

代码示例来源:origin: apache/tinkerpop

public Connection(final URI uri, final ConnectionPool pool, final int maxInProcess) throws ConnectionException {
  this.uri = uri;
  this.cluster = pool.getCluster();
  this.client = pool.getClient();
  this.pool = pool;
  this.maxInProcess = maxInProcess;
  this.keepAliveInterval = pool.settings().keepAliveInterval;
  connectionLabel = String.format("Connection{host=%s}", pool.host);
  if (cluster.isClosing()) throw new IllegalStateException("Cannot open a connection with the cluster after close() is called");
  final Bootstrap b = this.cluster.getFactory().createBootstrap();
  try {
    if (channelizerClass.get() == null) {
      channelizerClass.compareAndSet(null, (Class<Channelizer>) Class.forName(cluster.connectionPoolSettings().channelizer));
    }
    channelizer = channelizerClass.get().newInstance();
    channelizer.init(this);
    b.channel(NioSocketChannel.class).handler(channelizer);
    channel = b.connect(uri.getHost(), uri.getPort()).sync().channel();
    channelizer.connected();
    logger.info("Created new connection for {}", uri);
  } catch (Exception ie) {
    logger.debug("Error opening connection on {}", uri);
    throw new ConnectionException(uri, "Could not open connection", ie);
  }
}

代码示例来源:origin: lettuce-io/lettuce-core

protected void channelType(ConnectionBuilder connectionBuilder, ConnectionPoint connectionPoint) {
  LettuceAssert.notNull(connectionPoint, "ConnectionPoint must not be null");
  connectionBuilder.bootstrap().group(getEventLoopGroup(connectionPoint));
  if (connectionPoint.getSocket() != null) {
    NativeTransports.assertAvailable();
    connectionBuilder.bootstrap().channel(NativeTransports.domainSocketChannelClass());
  } else {
    connectionBuilder.bootstrap().channel(Transports.socketChannelClass());
  }
}

代码示例来源:origin: org.springframework/spring-web

private Bootstrap buildBootstrap(URI uri, boolean isSecure) {
  Bootstrap bootstrap = new Bootstrap();
  bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class)
      .handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel channel) throws Exception {
          configureChannel(channel.config());
          ChannelPipeline pipeline = channel.pipeline();
          if (isSecure) {
            Assert.notNull(sslContext, "sslContext should not be null");
            pipeline.addLast(sslContext.newHandler(channel.alloc(), uri.getHost(), uri.getPort()));
          }
          pipeline.addLast(new HttpClientCodec());
          pipeline.addLast(new HttpObjectAggregator(maxResponseSize));
          if (readTimeout > 0) {
            pipeline.addLast(new ReadTimeoutHandler(readTimeout,
                TimeUnit.MILLISECONDS));
          }
        }
      });
  return bootstrap;
}

代码示例来源:origin: apache/incubator-shardingsphere

private void groupsNio(final Bootstrap bootstrap) {
  bootstrap.group(workerGroup)
      .channel(NioSocketChannel.class)
      .option(ChannelOption.SO_KEEPALIVE, true)
      .option(ChannelOption.TCP_NODELAY, true)
      .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
      .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

代码示例来源:origin: docker-java/docker-java

public EventLoopGroup kqueueGroup() {
  EventLoopGroup nioEventLoopGroup = new KQueueEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
  bootstrap.group(nioEventLoopGroup).channel(KQueueDomainSocketChannel.class)
      .handler(new ChannelInitializer<KQueueDomainSocketChannel>() {
        @Override
        protected void initChannel(final KQueueDomainSocketChannel channel) throws Exception {
          channel.pipeline().addLast(new LoggingHandler(getClass()));
          channel.pipeline().addLast(new HttpClientCodec());
          channel.pipeline().addLast(new HttpContentDecompressor());
        }
      });
  return nioEventLoopGroup;
}

代码示例来源:origin: blynkkk/blynk-server

public void start() {
  Bootstrap b = new Bootstrap();
  b.group(nioEventLoopGroup).channel(NioSocketChannel.class).handler(getChannelInitializer());
  try {
    // Start the connection attempt.
    this.channel = b.connect(host, port).sync().channel();
  } catch (InterruptedException e) {
    log.error(e);
  }
}

代码示例来源:origin: apache/incubator-shardingsphere

private void groupsEpoll(final Bootstrap bootstrap) {
  bootstrap.group(workerGroup)
      .channel(EpollSocketChannel.class)
      .option(EpollChannelOption.TCP_CORK, true)
      .option(EpollChannelOption.SO_KEEPALIVE, true)
      .option(EpollChannelOption.SO_BACKLOG, 128)
      .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

代码示例来源:origin: normanmaurer/netty-in-action

public LogEventBroadcaster(InetSocketAddress address, File file) {
  group = new NioEventLoopGroup();
  bootstrap = new Bootstrap();
  bootstrap.group(group).channel(NioDatagramChannel.class)
     .option(ChannelOption.SO_BROADCAST, true)
     .handler(new LogEventEncoder(address));
  this.file = file;
}

代码示例来源:origin: apache/incubator-shardingsphere

private void groupsNio(final Bootstrap bootstrap) {
  bootstrap.group(workerGroup)
      .channel(NioSocketChannel.class)
      .option(ChannelOption.SO_KEEPALIVE, true)
      .option(ChannelOption.TCP_NODELAY, true)
      .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
      .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

代码示例来源:origin: a2888409/face2face

public static void startAuthLogicConnection(String ip, int port) {
    EventLoopGroup group = new NioEventLoopGroup();

    Bootstrap bootstrap = new Bootstrap()
        .group(group)
        .channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<SocketChannel>() {
          @Override
          protected void initChannel(SocketChannel channel)
              throws Exception {
            ChannelPipeline pipeline = channel.pipeline();

            pipeline.addLast("MessageDecoder", new PacketDecoder());
            pipeline.addLast("MessageEncoder", new PacketEncoder());
            pipeline.addLast("AuthLogicConnectionHandler", new AuthLogicConnectionHandler());  //Auth -> gate
          }
        });

    bootstrap.connect(ip, port);
  }
}

代码示例来源:origin: a2888409/face2face

public static void startGateAuthConnection(String ip, int port) {
    EventLoopGroup group = new NioEventLoopGroup();

    Bootstrap bootstrap = new Bootstrap()
        .group(group)
        .channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<SocketChannel>() {
          @Override
          protected void initChannel(SocketChannel channel)
              throws Exception {
            ChannelPipeline pipeline = channel.pipeline();

            pipeline.addLast("MessageDecoder", new PacketDecoder());
            pipeline.addLast("MessageEncoder", new PacketEncoder());
            pipeline.addLast("GateAuthConnectionHandler", new GateAuthConnectionHandler());  //Auth -> gate
          }
        });

    bootstrap.connect(ip, port);
  }
}

代码示例来源:origin: a2888409/face2face

public static void startGateLogicConnection(String ip, int port) {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap()
        .group(group)
        .channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<SocketChannel>() {
          @Override
          protected void initChannel(SocketChannel channel)
              throws Exception {
            ChannelPipeline pipeline = channel.pipeline();

            pipeline.addLast("MessageDecoder", new PacketDecoder());
            pipeline.addLast("MessageEncoder", new PacketEncoder());

            pipeline.addLast("GateLogicConnectionHandler", new GateLogicConnectionHandler());  //logic -> gate
          }
        });

    bootstrap.connect(ip, port);

  }
}

代码示例来源:origin: alibaba/Sentinel

private Bootstrap initClientBootstrap() {
  Bootstrap b = new Bootstrap();
  eventLoopGroup = new NioEventLoopGroup();
  b.group(eventLoopGroup)
    .channel(NioSocketChannel.class)
    .option(ChannelOption.TCP_NODELAY, true)
    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout())
    .handler(new ChannelInitializer<SocketChannel>() {
      @Override
      public void initChannel(SocketChannel ch) throws Exception {
        clientHandler = new TokenClientHandler(currentState, disconnectCallback);
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
        pipeline.addLast(new NettyResponseDecoder());
        pipeline.addLast(new LengthFieldPrepender(2));
        pipeline.addLast(new NettyRequestEncoder());
        pipeline.addLast(clientHandler);
      }
    });
  return b;
}

代码示例来源:origin: apache/incubator-pinot

private void init() {
 _bootstrap = new Bootstrap();
 _bootstrap.group(_eventGroup).channel(NioSocketChannel.class).handler(new ChannelHandlerInitializer(_handler));
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected void doOpen() throws Throwable {
  final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this);
  bootstrap = new Bootstrap();
  bootstrap.group(nioEventLoopGroup)
      .option(ChannelOption.SO_KEEPALIVE, true)
      .option(ChannelOption.TCP_NODELAY, true)
      .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
      //.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout())
      .channel(NioSocketChannel.class);
  if (getConnectTimeout() < 3000) {
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000);
  } else {
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout());
  }
  bootstrap.handler(new ChannelInitializer() {
    @Override
    protected void initChannel(Channel ch) throws Exception {
      int heartbeatInterval = UrlUtils.getIdleTimeout(getUrl());
      NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
      ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
          .addLast("decoder", adapter.getDecoder())
          .addLast("encoder", adapter.getEncoder())
          .addLast("client-idle-handler", new IdleStateHandler(heartbeatInterval, 0, 0, MILLISECONDS))
          .addLast("handler", nettyClientHandler);
    }
  });
}

代码示例来源:origin: ReactiveX/RxNetty

public Bootstrap newBootstrap(final EventPublisher eventPublisher, final ClientEventListener eventListener) {
  final Bootstrap nettyBootstrap = new Bootstrap().group(eventLoopGroup)
                          .channel(channelClass)
                          .option(ChannelOption.AUTO_READ, false);// by default do not read content unless asked.
  for (Entry<ChannelOption<?>, Object> optionEntry : options.entrySet()) {
    // Type is just for safety for user of ClientState, internally in Bootstrap, types are thrown on the floor.
    @SuppressWarnings("unchecked")
    ChannelOption<Object> key = (ChannelOption<Object>) optionEntry.getKey();
    nettyBootstrap.option(key, optionEntry.getValue());
  }
  nettyBootstrap.handler(new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel ch) throws Exception {
      ch.pipeline().addLast(ClientChannelActiveBufferingHandler.getName(),
                 new ChannelActivityBufferingHandler(eventPublisher, eventListener));
    }
  });
  return nettyBootstrap;
}

相关文章