In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to realize the smooth stop of Java application". In the daily operation, I believe that many people have doubts about how to realize the smooth stop of Java application. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to realize the smooth stop of Java application". Next, please follow the editor to study!
The trigger mechanisms for Java application exit are:
Auto end: when the application does not have a live thread or only a background thread
System.exit (0)
Kill or ctrl+C
Kill-9 forced exit
How to make the application stop smoothly
The exit of the program is like shutting down, we want to shut down smoothly when shutting down to ensure that the data of all applications are saved. Just like writing blog now, I hope it can be saved in the draft box when the phone is turned off.
We often have a resident task or service in our Java program, such as message consumer, service provider, and we expect to stop smoothly and not produce dirty data in the middle of transaction execution.
Java's support for this block is achieved through hook threads. Every Java process can register a hook thread, and the hook thread is executed before the program exits (except for kill-9 forced exit). The code to register the hook thread is as follows:
Runtime.getRuntime () addShutdownHook (t)
We can do some clean-up data cleaning and other things in the hook thread to ensure that the program exits smoothly.
In general, the lifecycle of a service or framework should be considered:
Such as the context.stop () method of the spring container.
Another example is the shutdown method of thread pool ExecutorService, which guarantees that new tasks are not accepted and that outstanding tasks are done.
When we redesign the service, we also need to take into account the stop method at stop so that it can be called by the hook thread on exit.
After the hook thread is registered, when the program receives the exit signal, it keeps the program running until the hook thread finishes executing, and all threads of the program are stopped and exited, as illustrated by the following sample code:
Public class ShutDownTest {public static void main (String [] args) {/ / register * Runtime.getRuntime () .addShutdownHook (new Thread () {public void run () {try {Thread.currentThread () .sleep (5000)) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("clean task1 completed.")}}) / / register the second hook Runtime.getRuntime () .addShutdownHook (new Thread () {public void run () {try {Thread.currentThread () .sleep (10000);} catch (InterruptedException e) {e.printStackTrace ()) } System.out.println ("clean task2 completed");}) / / start child thread new Thread () {public void run () {while (true) {try {Thread.currentThread () .sleep (1000); System.out.println ("sub thread is running") } catch (InterruptedException e) {e.printStackTrace ();} .start (); / / Program exits System.exit (0);}}
Program output:
Sub thread is running clean task1 completed. Sub thread is running clean task2 completed
Note: the hook thread only deals with the aftermath, and the goal is to exit as soon as possible and no guarantee of dirty data. If too much is done in the hook thread, or blocking occurs, then the kill may fail and the program cannot exit, which requires a forced exit.
If the following program causes the kill to fail, you need to force an exit because the hook thread is blocked:
Public class ShutDownTest {public static void main (String [] args) {/ / register hook Runtime.getRuntime () .addShutdownHook (new Thread () {public void run () {synchronized (ShutdownFileTest.class) {try {ShutdownFileTest.class.wait ()) } catch (InterruptedException e) {e.printStackTrace ();}) / / start child thread new Thread () {public void run () {while (true) {try {Thread.currentThread () .sleep (1000); System.out.println ("sub thread is running") } catch (InterruptedException e) {e.printStackTrace ();} .start (); System.exit (0);}}
Selection of program exit mechanism
Triggering program exit has been mentioned earlier, but in order to stop convenience, security, and elegance, we generally recommend several more manipulative exit mechanisms. The common recommendation mechanisms are as follows:
1.kill
It is often used in linux. It sends an exit signal to the process, and after receiving it, the java process exits smoothly.
2.shutdownfile
The system creates a shutdown file. And listen for the existence of shutdown file. If you find that shutdown file no longer exists, call System.exit to exit the program.
If you expect only a specific person to terminate the program, you can set permissions for the file so that only a specific person can terminate the program.
The following code is a simple example:
Import java.io.File; import java.io.IOException; public class ShutdownFileTest {public static void main (String [] args) {/ / Promoter thread new Thread () {public void run () {while (true) {try {Thread.currentThread () .sleep (1000) System.out.println ("sub thread is running");} catch (InterruptedException e) {e.printStackTrace ();} .start () / / start the shutdownfile listening thread new Thread () {public void run () {File shutDownFile = new File ("a.shutdown"); / / create shutdownfile if (! shutDownFile.exists ()) {try {shutDownFile.createNewFile () } catch (IOException e) {e.printStackTrace () }} / / watch for file deleted then shutdown while (true) {try {if (shutDownFile.exists ()) {Thread.currentThread () .sleep (1000) } else {System.exit (0);}} catch (InterruptedException e) {e.printStackTrace ();} .start () }}
3. Open a port, listen for commands in the port, and call System.exit after receiving the command.
This seems unusual and troublesome.
4.JMX
It is realized by mbean remote control of JMX.
See an example in this link: how-to-stop-java-process-gracefully
At this point, the study on "how to achieve a smooth stop of Java applications" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un