How to make a java program stop smoothly

foreword

       In agile development, the iteration cycle is fast, and versions are released frequently, requiring frequent updates to the production environment. When a new version is released, the old version of the program needs to be stopped. In the process of stopping the old version of the program, we hope that the old version of the program can be stopped smoothly. The so-called smooth stop means that there will be no dirty data, inconsistent transactions, data status has changed but not updated to the database, Kafka consumes data but not updated offset and other issues.

 

How to make a java program exit

1.kill -9 pid to force quit

2.kill -15 pid sends a signal to the java program. If the java program has Runtime.getRuntime().addShutdownHook(thread), it will exit after execution.

3.jmx, open the corresponding MBean in the program

4. The event is triggered by listening to the state change of a znode of zookeeper or subscribing to a topic of mq

5. Open http service in the program, such as through socket or start jetty service

 

The first one is of course not what we want. The second, third, fourth, and fifth methods can make the java application trigger the corresponding operation before exiting, and do some aftermath work to ensure that the program will not stop suddenly, causing various Anomalies and data inconsistencies.

 

Note :

1.kill -9 pid will not trigger Runtime.getRuntime().addShutdownHook(thread)

2. In the development phase, if the program is terminated directly through eclipse, it will not trigger Runtime.getRuntime().addShutdownHook(thread)

3. Try not to do too much work in the aftermath to prevent blocking and cause the program to die.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326452093&siteId=291194637