10 Mayıs 2018 Perşembe

Netty ServerBootstrap Sınıfı

Giriş
Şu satırı dahil ederiz.
import io.netty.bootstrap.ServerBootstrap;
Açıklaması şöyle.
ServerBootstrap is a helper class that sets up a server. You can set up the server using a Channel directly. However, please note that this is a tedious process, and you do not need to do that in most cases.
Kullanım
Şöyle yaparız.
public static void main(String[] args) throws Exception {
  // Configure the server.
  EventLoopGroup bossGroup = new NioEventLoopGroup();
  EventLoopGroup workerGroup = new NioEventLoopGroup();
  try {
    ServerBootstrap b = new ServerBootstrap();
    ...

    // Start the server.
    ChannelFuture f = b.bind().sync();

    // Wait until the server socket is closed.
    f.channel().closeFuture().sync();
  } finally {
    // Shut down all event loops to terminate all threads.
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();

    // Wait until all threads are terminated.
    bossGroup.terminationFuture().sync();
    workerGroup.terminationFuture().sync();
  }
}
bind metodu
Örnek
Şöyle yaparız.
ChannelFuture future = b.bind(new InetSocketAddress(port));
Örnek
Şöyle yaparız.
static final int PORT = 4466;

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();

ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
  .channel(NioServerSocketChannel.class)
  .handler(...)
  .childHandler(...);

Channel ch = b.bind(PORT).sync().channel();
channel metodu


Genelliklei io.netty.channel.socket.nio.NioServerSocketChannel ile kullanılır.
Örnek
Şöyle yaparız.
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(threadCount);
try {
  ServerBootstrap b = new ServerBootstrap();
  b.group(bossGroup, workerGroup)
   .channel(NioServerSocketChannel.class)
   .childHandler(new ChannelInitializer<SocketChannel>() {
     @Override
     public void initChannel(SocketChannel ch) throws Exception {
       ch.pipeline().addLast(new EchoServerComputeHandler());
     }
   })
   .option(ChannelOption.SO_BACKLOG, 128)          
   .childOption(ChannelOption.SO_KEEPALIVE, true); 

   ChannelFuture f = b.bind(port).sync(); 

   f.channel().closeFuture().sync();
} finally {
  workerGroup.shutdownGracefully();
  bossGroup.shutdownGracefully();
}
childHandler metodu
Şöyle yaparız.
ServerBootstrap b = new ServerBootstrap();
...
b.childHandler(new ChannelInitializer<SocketChannel>() {
  @Override
  public void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast(new EchoServerComputeHandler());
  }
})
childOption metodu
Accept() ile yaratılan soketlerin ayarlarını belirtir.
Örnek
Şöyle yaparız.
b.childOption(ChannelOption.TCP_NODELAY, true);
Örnek
Şöyle yaparız.
b.childOption(ChannelOption.SO_KEEPALIVE, true);
group metodu
İki tane parametre alır. Açıklaması şöyle.
The first one, often called 'boss', accepts an incoming connection. The second one, often called 'worker', handles the traffic of the accepted connection once the boss accepts the connection and registers the accepted connection to the worker. How many Threads are used and how they are mapped to the created Channels depends on the EventLoopGroup implementation and may be even configurable via a constructor.
bossGroup için 1 thread kullanmak yeterli. workerGroup için thread sayısı belirtilebilir veya belirtilmeyebilir.

option metodu
Sunucu soketin ayarlarını belirtir.
Örnek
Şöyle yaparız.
b.group(bossGroup, workerGroup)
   .option(ChannelOption.SO_BACKLOG, 4096)
   .option(ChannelOption.SO_REUSEADDR, true)
   .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.MAX_VALUE)
   .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
   .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.MAX_VALUE)
Örnek
Şöyle yaparız.
b.option(ChannelOption.SO_BACKLOG, 100);
Örnek
Şöyle yaparız.
ServerBootstrap b = new ServerBootstrap();
...
b.option(ChannelOption.SO_BACKLOG, 128)          
 .childOption(ChannelOption.SO_KEEPALIVE, true); 
setFactory metodu
Şöyle yaparız.
serverBootstrap.setFactory(new NioServerSocketChannelFactory(
  Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
setPipelineFactory metodu
Örnek ver

Hiç yorum yok:

Yorum Gönder