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

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

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

Bootstrap.<init>介绍

暂无

代码示例

代码示例来源: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: 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: fengjiachun/Jupiter

protected void init() {
  ThreadFactory workerFactory = workerThreadFactory("jupiter.connector");
  worker = initEventLoopGroup(nWorkers, workerFactory);
  bootstrap = new Bootstrap().group(worker);
  JConfig child = config();
  child.setOption(JOption.IO_RATIO, 100);
  doInit();
}

代码示例来源:origin: eclipse-vertx/vert.x

AtomicReference<Thread> channelThread = new AtomicReference<>();
CountDownLatch connectLatch = new CountDownLatch(1);
Bootstrap bootstrap = new Bootstrap();
bootstrap.channelFactory(((VertxInternal)vertx).transport().channelFactory(false));
bootstrap.group(vertx.nettyEventLoopGroup());
bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup());
bootstrap.handler(new ChannelInitializer<Channel>() {
 @Override
 protected void initChannel(Channel ch) throws Exception {
ChannelFuture channelFut = bootstrap.connect("localhost", 1234);
awaitLatch(connectLatch);
channelFut.addListener(v -> {

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

Bootstrap createBootstrap() {
  final Bootstrap b = new Bootstrap().group(group);
  b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
  return b;
}

代码示例来源:origin: eclipse-vertx/vert.x

ContextInternal context = vertx.getOrCreateContext();
sslHelper.validate(vertx);
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(context.nettyEventLoop());
bootstrap.channelFactory(vertx.transport().channelFactory(remoteAddress.path() != null));

代码示例来源: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: linkedin/flashback

/**
 * Establishing TCP connection to server
 *
 * @param remoteAddress remote address
 * */
public ChannelFuture connectToServer(final InetSocketAddress remoteAddress) {
 if (remoteAddress == null) {
  throw new IllegalStateException("remote address is null");
 }
 Bootstrap bootstrap = new Bootstrap().group(_upstreamWorkerGroup);
 bootstrap.channelFactory(NioSocketChannel::new);
 ServerChannelHandler serverChannelHandler = new ServerChannelHandler(this);
 bootstrap.handler(new ChannelInitializer<Channel>() {
  protected void initChannel(Channel ch)
    throws Exception {
   initChannelPipeline(ch.pipeline(), serverChannelHandler, _serverConnectionIdleTimeoutMsec);
   _serverChannel = ch;
  }
 });
 LOG.debug("Server channel is ready. About to connect....");
 return bootstrap.connect(remoteAddress);
}

代码示例来源:origin: fengjiachun/Jupiter

protected void init() {
  ThreadFactory workerFactory = workerThreadFactory("jupiter.connector");
  worker = initEventLoopGroup(nWorkers, workerFactory);
  bootstrap = new Bootstrap().group(worker);
  JConfig child = config();
  child.setOption(JOption.IO_RATIO, 100);
  doInit();
}

代码示例来源: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: io.vertx/vertx-core

AtomicReference<Thread> channelThread = new AtomicReference<>();
CountDownLatch connectLatch = new CountDownLatch(1);
Bootstrap bootstrap = new Bootstrap();
bootstrap.channelFactory(((VertxInternal)vertx).transport().channelFactory(false));
bootstrap.group(vertx.nettyEventLoopGroup());
bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup());
bootstrap.handler(new ChannelInitializer<Channel>() {
 @Override
 protected void initChannel(Channel ch) throws Exception {
ChannelFuture channelFut = bootstrap.connect("localhost", 1234);
awaitLatch(connectLatch);
channelFut.addListener(v -> {

代码示例来源: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: 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: 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: apache/incubator-pinot

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

代码示例来源: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-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;
}

代码示例来源:origin: dreamhead/moco

public final void run(final String host, final int port, final ChannelHandler pipelineFactory) {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group)
        .channel(NioSocketChannel.class)
        .remoteAddress(host, port)
        .option(ChannelOption.TCP_NODELAY, true)
        .handler(pipelineFactory);

    try {
      Channel channel = bootstrap.connect().sync().channel();
      ChannelFuture future = channel.closeFuture().sync();
      future.addListener(ChannelFutureListener.CLOSE);
    } catch (InterruptedException e) {
      throw new MocoException(e);
    } finally {
      group.shutdownGracefully();
    }
  }
}

相关文章