当前位置:网站首页>JVM shutdown hook details
JVM shutdown hook details
2022-06-25 11:28:00 【pyiran】
What is? JVM Shutdown Hook
Shutdown Hook It is a special structure , Generally used in JVM Before closing , When you need to perform some operations . common , For example, when you need to do some cleaning work when your program exits , You can consider using it Shutdown Hook. But if yours JVM Abnormal exit , For example, received SIGKILL, There is no guarantee at this time shutdown hook Be able to perform normally .
How to use Shutdown Hook
Java In fact, the corresponding interfaces have been provided to implement , It's also very convenient to use . Here are a few simple examples :
public class ShutDownHookTest
{
public static void main(String[] args)
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
System.out.println("Shutdown Hook now!");
}
});
System.out.println("Going to exit");
}
}
Output :
Going to exit
Shutdown Hook now!
You can see the above example , We just need to give addShutdownHook Pass in one Thread object , stay run() Write the operations to be performed in .
class MyThread extends Thread {
public void run() {
System.out.println("Execute done");
System.out.println("Shutdown hook now!");
}
}
class ShutDownHookTest {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new MyThread());
for(int i = 1; i < 4; i++)
System.out.println("Execute:" + i);
}
}
Output :
Execute:1
Execute:2
Execute:3
Execute done
Shutdown hook now!
The above example clearly shows , When the main program is finished , Will start to execute Shutdown Hook, JVM You're going to quit .
Shutdown Hook Characteristics of
Shutdown Hook There is no guarantee that
If JVM crashe 了 , Shutdown Hook There is no guarantee that . For example, if you receive SIGKILL When , The program will immediately terminate execution ,JVM Quit now , Resulting in no opportunity to execute Shutdown Hook. If called Runtime.halt() Under the circumstances , Can also lead to JVM No execution at Shutdown Hook Exit directly when . Of course , If a program ends normally , Will be in JVM Exit to call Shutdown Hook. If JVM Because the user requests to interrupt or receives SIGTERM, Also called Shutdown Hook Of .
Shutdown Hook Can be forcibly interrupted
Even if the execution has started Shutdown Hook, It can also be interrupted , For example, when receiving SIGTERM, however shutdown hook Not completed within a certain time , Will also be forced to interrupt , Lead to shutdown hook Not fully implemented . So in general Shutdown hook All operations in should be completed quickly , It shouldn't be some long time The task of .
Shutdown Hook There can be multiple
You can register more than one in a program shutdown hook, however JVM perform shutdown hook Is an arbitrary order , also JVM perform hook Is concurrent .
Shutdown hook Can't regist/unregister shutdown hook
If so ,JVM Flip a IllegalStateException.
Shutdown hook Can be stopped
If one Shutdown It's been implemented , Except for example SIGKILL Such external intervention , It is necessary and only through Runtime.halt() interrupt .
Shutdown hook Security permissions are required
Shutdown hook stay Spark An example of application in
Here we use Spark Medium ApplicationMaster To do an example analysis .
ApplicationMaster.scala
// This shutdown hook should run *after* the SparkContext is shut down.
val priority = ShutdownHookManager.SPARK_CONTEXT_SHUTDOWN_PRIORITY - 1
ShutdownHookManager.addShutdownHook(priority) {
() =>
val maxAppAttempts = client.getMaxRegAttempts(sparkConf, yarnConf)
val isLastAttempt = appAttemptId.getAttemptId() >= maxAppAttempts
if (!finished) {
// The default state of ApplicationMaster is failed if it is invoked by shut down hook.
// This behavior is different compared to 1.x version.
// If user application is exited ahead of time by calling System.exit(N), here mark
// this application as failed with EXIT_EARLY. For a good shutdown, user shouldn't call
// System.exit(0) to terminate the application.
finish(finalStatus,
ApplicationMaster.EXIT_EARLY,
"Shutdown hook called before final status was reported.")
}
if (!unregistered) {
// we only want to unregister if we don't want the RM to retry
if (finalStatus == FinalApplicationStatus.SUCCEEDED || isLastAttempt) {
unregister(finalStatus, finalMsg)
cleanupStagingDir(new Path(System.getenv("SPARK_YARN_STAGING_DIR")))
}
}
In this program , When application At the end, you need to delete stagingDir The files under the . This is what we mentioned above shutdown hook Common use .
But in reality Spark application in , Sometimes application By kill Or abnormal exit , This time to observe stagingDir Will find ,application It was not deleted after the end . Because when application By kill Or exit abnormally ,Yarn Will send SIGTERM, And then send SIGKILL.shutdown hook It needs to be done in the middle of these two operations , If not completed in time , It will lead to shutdown hook Delete in stagingDir The operation of is not completed . This time can pass yarn.nodemanager.slepp-delay-before-sigkill.ms To set up .
Reference resources
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
边栏推荐
- Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million
- How to realize the rich text editor function of mobile terminal
- Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
- Gaussdb others scenarios with high memory
- [file containing vulnerability-03] six ways to exploit file containing vulnerabilities
- 金仓数据库 KingbaseES 插件DBMS_RANDOM
- Kingbasees plug-in DBMS of Jincang database_ UTILITY
- 金仓数据库 KingbaseES 插件identity_pwdexp
- GaussDB 集群维护案例集-sql执行慢
- 牛客网:分糖果问题
猜你喜欢
Jincang KFS data centralized scenario (many to one) deployment
Getting started with Apache Shenyu
Crawler scheduling framework of scratch+scratch+grammar
Golden sun education listed in the U.S.: a small cap medium cap stock with a market value of USD 360million
Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis
杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
Database Series: MySQL index optimization summary (comprehensive version)
Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)
Use of three-level linkage plug-ins selected by provinces and cities
随机推荐
贝叶斯
手机上股票开户安全吗?找谁可以开户啊?
西山科技冲刺科创板:拟募资6.6亿 郭毅军夫妇有60%表决权
Ladder side tuning: the "wall ladder" of the pre training model
Handler、Message、Looper、MessageQueue
c盘使用100%清理方法
Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
3 Questions par jour (3) - vérifier l'existence d'entiers et de leurs doubles
Course paper + code and executable EXE file of library information management system based on C language
时创能源冲刺科创板:拟募资11亿 年营收7亿净利反降36%
16 enterprise architecture strategies
SystemVerilog(十三)-枚举数据类型
Free access to the global human settlements layer (ghsl) dataset from Gee
relu与sigmod的比较
Getting started with Apache Shenyu
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
Application of global route guard
反应c语言程序结构特点的程序
2022年PMP项目管理考试敏捷知识点(2)
PHP如何提取字符串中的图片地址