8 Mayıs 2018 Salı

Netty Bootstrap Sınıfı

Giriş
Şu satırı dahil ederiz.
import io.netty.bootstrap.Bootstrap;
Açıklaması şöyle.
Bootstrap is similar to ServerBootstrap except that it's for non-server channels such as a client-side or connectionless channel.
Bu sınıfın ismi eski kodlarda sanırım ClientBootstrap idi.

channel metodu
Şöyle yaparız.
bootstrap.channel(NioSocketChannel.class);
Şöyle yaparız.
bootstrap.channel(EpollDomainSocketChannel.class)
connect metodu
Örnek
Şöyle yaparız.
ChannelFuture future = bootstrap.connect();
Örnek
Şöyle yaparız.
new Bootstrap()
  .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
  .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
  .group(eventLoopGroup)
  .channel(NioSocketChannel.class)
  .handler(httpNettyClientChannelInitializer)
  .connect(remoteAddress, serverPort)
  .addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) {
      future.cancel(!future.isSuccess());
    }
  });
group metodu
channel ve group uyumlu olmalıdır.
Örnek
Epoll için şöyle yaparız.
EventLoopGroup clientEventLoop = new EpollEventLoopGroup();

bootstrap.group(clientEventLoop)
  .channel(EpollDomainSocketChannel.class)
Örnek
Nio için şöyle yaparız.
EventLoopGroup group = new NioEventLoopGroup();
bootstrap.group(group);
bootstrap.channel(NioSocketChannel.class);
Örnek
ChannelHandlerContext 'ten almak için şöyle yaparız.
@Override
public void channelActive(ChannelHandlerContext ctx) {
  final Channel inboundChannel = ctx.channel();

  // Start the connection attempt.
  Bootstrap b = new Bootstrap();
  b.group(inboundChannel.eventLoop())
    .channel(ctx.channel().getClass())
    .handler(new ServerBackendHandler(inboundChannel, response))
    .option(ChannelOption.AUTO_READ, false);
  ChannelFuture f = b.connect(remoteHost, remotePort);
  outboundChannel = f.channel();
  f.addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future) throws Exception {
      if (future.isSuccess()) {
        // connection complete start to read first data
        inboundChannel.read();
      } else {
        // Close the connection if the connection attempt has failed.
        inboundChannel.close();
      }
    }
   });
}
handler metodu
Örnek
Şöyle yaparız.
.handler(new ChannelInitializer<Channel>() {
  @Override
  protected void initChannel(final Channel channel) throws Exception {
    channel.pipeline().addLast(new ChannelInboundHandlerAdapter(){...});
  }
});
Örnek
Şöyle yaparız.
b.handler(new ChannelInitializer<SocketChannel>() {
  @Override
  public void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast("frameDecoder", new DelimiterBasedFrameDecoder(bufferSize,bt));
    ch.pipeline().addLast("ByteDecoder", new ByteArrayDecoder());                    
    ch.pipeline().addLast("frameEncoder", new ByteArrayEncoder());
    ch.pipeline().addLast (new TimeClientHandler (c));
  }
});
option metodu
Örnek - CONNECT_TIMEOUT_MILLIS
Şöyle yaparız.
bootsrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
Örnek - ALLOCATOR
Şöyle yaparız.
public void connect(final InetAddress remoteAddress){
  new Bootstrap()
    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
    .group(eventLoopGroup)
    .channel(NioSocketChannel.class)
    .handler(...)
    .connect(remoteAddress, serverPort)
    .addListener(new ChannelFutureListener() {
      @Override
      public void operationComplete(ChannelFuture future) {
        ...
      }
    });
}

Hiç yorum yok:

Yorum Gönder