23 Kasım 2017 Perşembe

Netty

Bootstrap Sınıfı
Açıklaması şöyle.
Bootstrap is similar to ServerBootstrap except that it's for non-server channels such as a client-side or connectionless channel.
ByteBuf Sınıfı
Örnek ver

ByteToMessageDecoder Sınıfı
Açıklaması şöyle.
ByteToMessageDecoder is an implementation of ChannelInboundHandler which makes it easy to deal with the fragmentation issue.
ChannelInboundHandlerAdapter Sınıfı
channelActive metodu
Şöyle yaparız.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
  channels.add(ctx.channel());
  // ...
}
channelInactive metodu
Şöyle yaparız.
@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
  channels.remove(ctx.channel());
}
channelRead metodu
Şöyle yaparız.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  // ...

  for (Channel ch : channels1) {
    // Does `ch` represent the channel of the current sending client?
    if (ch.equals(ctx.channel())) {
      // Skip.
       continue;
    }

    // Send the message to the `ch` channel.
    // ...
  }

  // ...
}
handlerAdded metodu
Açıklaması şöyle
A ChannelHandler has two life cycle listener methods: handlerAdded() and handlerRemoved(). You can perform an arbitrary (de)initialization task as long as it does not block for a long time.
Örnek ver

handlerRemoved metodu
Örnek ver

ChannelInitializer Sınıfı
Giriş
Açıklaması şöyle.
The ChannelInitializer is a special handler that is purposed to help a user configure a new Channel. It is most likely that you want to configure the ChannelPipeline of the new Channel by adding some handlers such as ... to implement your network application. 
initChannel metodu
Örnek
Şöyle yaparız.
public class MyInitializer extends ChannelInitializer<SocketChannel> 
{
  @Override
  public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    ...
  }
}
Örnek
Veriyi satırlara ayırarak benim sınıfımı çağıran kod için şöyle yaparız.
private static final StringDecoder DECODER = new StringDecoder();
@Override
protected void initChannel(SocketChannel ch) throws Exception {
  ChannelPipeline pipeline = ch.pipeline();
  pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
  pipeline.addLast(DECODER);
  pipeline.addLast(new MySentenceReaderHandler()); 
}
DefaultChannelGroup Sınıfı
constructor
Şöyle yaparız.
static final ChannelGroup channels =
  new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
DefaultEventExecutorGroup Sınıfı
constructor - int
Şöyle yaparız.
static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);
 ...

 ChannelPipeline pipeline = ch.pipeline();

 pipeline.addLast("decoder", new MyProtocolDecoder());
 pipeline.addLast("encoder", new MyProtocolEncoder());

 // Tell the pipeline to run MyBusinessLogicHandler's event handler methods
 // in a different thread than an I/O thread so that the I/O thread is not blocked by
 // a time-consuming task.
 // If your business logic is fully asynchronous or finished very quickly, you don't
 // need to specify a group.
 pipeline.addLast(group, "handler", new MyBusinessLogicHandler());
EventLoopGroup Sınıfı
shutdownGraceFully metodu
Açıklaması şöyle
Shutting down a Netty application is usually as simple as shutting down all EventLoopGroups you created via shutdownGracefully(). It returns a Future that notifies you when the EventLoopGroup has been terminated completely and all Channels that belong to the group have been closed.
ServerBootstrap Sınıfı
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.
bind metodu
Şö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();
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.
SimpleChannelInboundHandler Sınıfı
Giriş
ChannelInboundHandlerAdapter'dan kalıtır ve mesaj tipini tanımlamaye izin verir.

channelRead0 metodu
Şöyle yaparız.
public class MyBusinessLogicHandler extends SimpleChannelInboundHandler<MyModel>{
  public void channelRead0(ChannelHandlerContext ctx, Packet msg){
    Packet rslt = null;
    //Do some complicated business logic
    ctx.write(rslt);
  }
}