[JAVA] How to dynamically switch between FIN and RST in Netty

When building a server using Netty, you may want to use FIN and RST properly depending on the situation when closing Channel (socket). For example, there is a requirement that FIN (RST when delayed timeout) is set normally, but RST is set immediately when an abnormality is detected.

Specifying the default behavior when closing

Whether to use FIN / RST when Channel is closed can be specified when building ServerBootstrap.

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(0);
ServerBootstrap bootstrap = new ServerBootstrap()
    .option(ChannelOption.SO_BACKLOG, 1024)
    .group(bossGroup, workerGroup)
    .channel(NioServerSocketChannel.class)
    .childOption(ChannelOption.SO_LINGER, 10) //FIN → Delayed timeout(10 seconds)→ Specify to close with RST
    .childHandler(new MyChannelInitializer());
// ...

NOTE:

Release by RST that specifies> -1 is disabled.

Select FIN / RST when closing

If you want to close the ServerBootstrap in a way different from the default behavior specified when building it, you can do so by getting the ChannelConfig from the Channel and changing the SO_LINGER setting.

@ChannelHandler.Sharable
class MyHandler extends SimpleChannelInboundHandler<byte[]> {
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, byte[] msg) {
    // ...
  }
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    ctx.channel().config().setOption(ChannelOption.SO_LINGER, 0); //Specify to close with RST immediately
    ctx.close();
  }

}

Recommended Posts

How to dynamically switch between FIN and RST in Netty
How to dynamically switch JDK when building Java in Gradle
How to switch between multiple Java versions
Ruby How to convert between uppercase and lowercase
Switch between JDK 7 and JDK 8
How to switch Tomcat context.xml with WTP in Eclipse
How to convert A to a and a to A using AND and OR in Java
How to handle TSV files and CSV files in Ruby
How to launch Swagger UI and Swagger Editor in Docker
How to specify character code and line feed code in JAXB
How to prevent conflicts between JBoss modules and application libraries
Java conditional branching: How to create and study switch statements
Difference between Java and JavaScript (how to find the average)
[Forge] How to register your own Entity and Entity Render in 1.13.2
Implementation policy to dynamically save and display Timezone in Rails
How to set character code and line feed code in Eclipse
[Rails] Differences between redirect_to and render methods and how to output render methods
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
How to develop and register a Sota app in Java
How to switch Java in the OpenJDK era on Mac
How to install PHP 7.4 and SQL Server drivers in CentOS 7.7
How to include PKCE Code_Verifier and Code_Challenge in JMeter requests
How to POST JSON in Java-Method using OkHttp3 and method using HttpUrlConnection-
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to switch Java version with direnv in terminal on Mac
How to set and use profile in annotation-based Configuration in Spring framework
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to delete large amounts of data in Rails and concerns
How to install the language used in Ubuntu and how to build the environment
How to Synchronize Customer Data Between Salesforce and Kintone (Java-Simple JDBC)
How to get and add data from Firebase Firestore in Ruby
How to encrypt and decrypt with RSA public key in Java
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
Dynamically switch the database to connect to
How to run JUnit in Eclipse
How to use EventBus3 and ThreadMode
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to master programming in 3 months
How to learn JAVA in 7 days
How to get parameters in Spark
How to call classes and methods
How to use equality and equality (how to use equals)
How to install Bootstrap in Ruby
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
How to concatenate strings in java
How to install Swiper in Rails
How to call libraries such as JQuery and JQuery UI in Liferay 7 / DXP
What to do and how to install when an error occurs in DXRuby 1.4.7
How to change the maximum and maximum number of POST data in Spark
How to create your own annotation in Java and get the value
[swift5] How to specify color in hexadecimal
How to implement search functionality in Rails