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

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

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

Bootstrap.channelFactory介绍

暂无

代码示例

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

@Override
public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerClientConfig) {
  EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, createThreadFactory());
  // TODO do we really need BouncyCastle?
  Security.addProvider(new BouncyCastleProvider());
  ChannelFactory<NioSocketChannel> factory = () -> configure(new NioSocketChannel());
  bootstrap.group(nioEventLoopGroup).channelFactory(factory)
      .handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(final SocketChannel channel) throws Exception {
          channel.pipeline().addLast(new HttpClientCodec());
          channel.pipeline().addLast(new HttpContentDecompressor());
        }
      });
  return nioEventLoopGroup;
}

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

public EventLoopGroup epollGroup() {
  EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, createThreadFactory());
  ChannelFactory<EpollDomainSocketChannel> factory = () -> configure(new EpollDomainSocketChannel());
  bootstrap.group(epollEventLoopGroup).channelFactory(factory).handler(new ChannelInitializer<UnixChannel>() {
    @Override
    protected void initChannel(final UnixChannel channel) throws Exception {
      channel.pipeline().addLast(new HttpClientCodec());
      channel.pipeline().addLast(new HttpContentDecompressor());
    }
  });
  return epollEventLoopGroup;
}

代码示例来源:origin: mpusher/mpush

private void createClient(Listener listener, EventLoopGroup workerGroup, ChannelFactory<? extends Channel> channelFactory) {
  this.workerGroup = workerGroup;
  this.bootstrap = new Bootstrap();
  bootstrap.group(workerGroup)//
      .option(ChannelOption.SO_REUSEADDR, true)//
      .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)//
      .channelFactory(channelFactory);
  bootstrap.handler(new ChannelInitializer<Channel>() { // (4)
    @Override
    public void initChannel(Channel ch) throws Exception {
      initPipeline(ch.pipeline());
    }
  });
  initOptions(bootstrap);
  listener.onSuccess();
}

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

protected void initChannelFactory() {
  SocketChannelProvider.SocketType socketType = socketType();
  switch (socketType) {
    case NATIVE_EPOLL_DOMAIN:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_EPOLL_DOMAIN_CONNECTOR);
      break;
    case NATIVE_KQUEUE_DOMAIN:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_KQUEUE_DOMAIN_CONNECTOR);
      break;
    default:
      throw new IllegalStateException("Invalid socket type: " + socketType);
  }
}

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

protected void initChannelFactory() {
  SocketChannelProvider.SocketType socketType = socketType();
  switch (socketType) {
    case NATIVE_EPOLL_DOMAIN:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_EPOLL_DOMAIN_CONNECTOR);
      break;
    case NATIVE_KQUEUE_DOMAIN:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_KQUEUE_DOMAIN_CONNECTOR);
      break;
    default:
      throw new IllegalStateException("Invalid socket type: " + socketType);
  }
}

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

protected void initChannelFactory() {
  SocketChannelProvider.SocketType socketType = socketType();
  switch (socketType) {
    case NATIVE_EPOLL:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_EPOLL_CONNECTOR);
      break;
    case NATIVE_KQUEUE:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_KQUEUE_CONNECTOR);
      break;
    case JAVA_NIO:
      bootstrap().channelFactory(SocketChannelProvider.JAVA_NIO_CONNECTOR);
      break;
    default:
      throw new IllegalStateException("Invalid socket type: " + socketType);
  }
}

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

protected void initChannelFactory() {
  SocketChannelProvider.SocketType socketType = socketType();
  switch (socketType) {
    case NATIVE_EPOLL:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_EPOLL_CONNECTOR);
      break;
    case NATIVE_KQUEUE:
      bootstrap().channelFactory(SocketChannelProvider.NATIVE_KQUEUE_CONNECTOR);
      break;
    case JAVA_NIO:
      bootstrap().channelFactory(SocketChannelProvider.JAVA_NIO_CONNECTOR);
      break;
    default:
      throw new IllegalStateException("Invalid socket type: " + socketType);
  }
}

代码示例来源:origin: atomix/atomix

private CompletableFuture<Void> bootstrapServer() {
 Bootstrap serverBootstrap = new Bootstrap()
   .group(group)
   .channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4))
   .handler(new SimpleChannelInboundHandler<Object>() {
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
     // Nothing will be sent.
    }
   })
   .option(ChannelOption.IP_MULTICAST_IF, iface)
   .option(ChannelOption.SO_REUSEADDR, true);
 CompletableFuture<Void> future = new CompletableFuture<>();
 serverBootstrap.bind(localAddress).addListener((ChannelFutureListener) f -> {
  if (f.isSuccess()) {
   serverChannel = f.channel();
   future.complete(null);
  } else {
   future.completeExceptionally(f.cause());
  }
 });
 return future;
}

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

@Override
public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerClientConfig) {
  EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
  InetAddress addr = InetAddress.getLoopbackAddress();
  final SocketAddress proxyAddress = new InetSocketAddress(addr, 8008);
  Security.addProvider(new BouncyCastleProvider());
  ChannelFactory<NioSocketChannel> factory = new ChannelFactory<NioSocketChannel>() {
    @Override
    public NioSocketChannel newChannel() {
      return configure(new NioSocketChannel());
    }
  };
  bootstrap.group(nioEventLoopGroup).channelFactory(factory)
      .handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(final SocketChannel channel) throws Exception {
          // channel.pipeline().addLast(new
          // HttpProxyHandler(proxyAddress));
          channel.pipeline().addLast(new HttpClientCodec());
          channel.pipeline().addLast(new HttpContentDecompressor());
        }
      });
  return nioEventLoopGroup;
}

代码示例来源:origin: atomix/atomix

private CompletableFuture<Void> bootstrapClient() {
 Bootstrap clientBootstrap = new Bootstrap()
   .group(group)
   .channelFactory(() -> new NioDatagramChannel(InternetProtocolFamily.IPv4))
   .handler(new SimpleChannelInboundHandler<DatagramPacket>() {
    @Override

代码示例来源:origin: Graylog2/graylog2-server

@VisibleForTesting
Bootstrap getBootstrap(MessageInput input) {
  LOG.debug("Setting UDP receive buffer size to {} bytes", getRecvBufferSize());
  final NettyTransportType transportType = nettyTransportConfiguration.getType();
  eventLoopGroup = eventLoopGroupFactory.create(workerThreads, localRegistry, "workers");
  return new Bootstrap()
      .group(eventLoopGroup)
      .channelFactory(new DatagramChannelFactory(transportType))
      .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(getRecvBufferSize()))
      .option(ChannelOption.SO_RCVBUF, getRecvBufferSize())
      .option(UnixChannelOption.SO_REUSEPORT, true)
      .handler(getChannelInitializer(getChannelHandlers(input)))
      .validate();
}

代码示例来源:origin: mpusher/mpush

private void createServer(Listener listener, EventLoopGroup eventLoopGroup, ChannelFactory<? extends DatagramChannel> channelFactory) {
  this.eventLoopGroup = eventLoopGroup;
  try {
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup)//默认是根据机器情况创建Channel,如果机器支持ipv6,则无法使用ipv4的地址加入组播
        .channelFactory(channelFactory)
        .option(ChannelOption.SO_BROADCAST, true)
        .handler(getChannelHandler());
    initOptions(b);
    //直接绑定端口,不要指定host,不然收不到组播消息
    b.bind(port).addListener(future -> {
      if (future.isSuccess()) {
        logger.info("udp server start success on:{}", port);
        if (listener != null) listener.onSuccess(port);
      } else {
        logger.error("udp server start failure on:{}", port, future.cause());
        if (listener != null) listener.onFailure(future.cause());
      }
    });
  } catch (Exception e) {
    logger.error("udp server start exception", e);
    if (listener != null) listener.onFailure(e);
    throw new ServiceException("udp server start exception, port=" + port, e);
  }
}

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

public EventLoopGroup epollGroup() {
  EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
  ChannelFactory<EpollDomainSocketChannel> factory = new ChannelFactory<EpollDomainSocketChannel>() {
    @Override
    public EpollDomainSocketChannel newChannel() {
      return configure(new EpollDomainSocketChannel());
    }
  };
  bootstrap.group(epollEventLoopGroup).channelFactory(factory).handler(new ChannelInitializer<UnixChannel>() {
    @Override
    protected void initChannel(final UnixChannel channel) throws Exception {
      channel.pipeline().addLast(new HttpClientCodec());
      channel.pipeline().addLast(new HttpContentDecompressor());
    }
  });
  return epollEventLoopGroup;
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private Bootstrap newBootstrap(ChannelFactory<? extends Channel> channelFactory, EventLoopGroup eventLoopGroup, AsyncHttpClientConfig config) {
 @SuppressWarnings("deprecation")
 Bootstrap bootstrap = new Bootstrap().channelFactory(channelFactory).group(eventLoopGroup)
     .option(ChannelOption.ALLOCATOR, config.getAllocator() != null ? config.getAllocator() : ByteBufAllocator.DEFAULT)
     .option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())
     .option(ChannelOption.SO_REUSEADDR, config.isSoReuseAddress())
     .option(ChannelOption.AUTO_CLOSE, false);
 if (config.getConnectTimeout() > 0) {
  bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout());
 }
 if (config.getSoLinger() >= 0) {
  bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger());
 }
 if (config.getSoSndBuf() >= 0) {
  bootstrap.option(ChannelOption.SO_SNDBUF, config.getSoSndBuf());
 }
 if (config.getSoRcvBuf() >= 0) {
  bootstrap.option(ChannelOption.SO_RCVBUF, config.getSoRcvBuf());
 }
 for (Entry<ChannelOption<Object>, Object> entry : config.getChannelOptions().entrySet()) {
  bootstrap.option(entry.getKey(), entry.getValue());
 }
 return bootstrap;
}

代码示例来源:origin: relayrides/pushy

@Override
  public void run() {
    final Bootstrap bootstrap = ApnsChannelFactory.this.bootstrapTemplate.clone()
        .channelFactory(new AugmentingReflectiveChannelFactory<>(
            ClientChannelClassUtil.getSocketChannelClass(ApnsChannelFactory.this.bootstrapTemplate.config().group()),
            CHANNEL_READY_PROMISE_ATTRIBUTE_KEY, channelReadyPromise));
    final ChannelFuture connectFuture = bootstrap.connect();
    connectFuture.addListener(new GenericFutureListener<ChannelFuture>() {
      @Override
      public void operationComplete(final ChannelFuture future) {
        if (!future.isSuccess()) {
          // This may seem spurious, but our goal here is to accurately report the cause of
          // connection failure; if we just wait for connection closure, we won't be able to
          // tell callers anything more specific about what went wrong.
          tryFailureAndLogRejectedCause(channelReadyPromise, future.cause());
        }
      }
    });
    connectFuture.channel().closeFuture().addListener(new GenericFutureListener<ChannelFuture> () {
      @Override
      public void operationComplete(final ChannelFuture future) {
        // We always want to try to fail the "channel ready" promise if the connection closes; if it has
        // already succeeded, this will have no effect.
        channelReadyPromise.tryFailure(
            new IllegalStateException("Channel closed before HTTP/2 preface completed."));
      }
    });
  }
}, delay, TimeUnit.SECONDS);

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

bootstrap.channelFactory(client.getVertx().transport().channelFactory(false));

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

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

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

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());

代码示例来源:origin: redisson/redisson

b.channelFactory(channelFactory);
b.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
final DnsResponseHandler responseHandler = new DnsResponseHandler(executor().<Channel>newPromise());

代码示例来源:origin: wildfly/wildfly

b.channelFactory(channelFactory);
b.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
final DnsResponseHandler responseHandler = new DnsResponseHandler(executor().<Channel>newPromise());

相关文章