找出谁调用jvm shutdown hook [英] Finding out who calls jvm shutdown hook

查看:129
本文介绍了找出谁调用jvm shutdown hook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于Java的Windows桌面应用程序。

I have Windows Desktop application based on Java.

我在应用程序中保留了defaultuncaughtexceptionahandler和shutdown hook。

I have kept defaultuncaughtexceptionahandler and shutdown hook in my application.

我有一些用户称为退出点,例如用户点击退出,某些错误条件等。所有用户出口点都有正确的日志,后面会有登录关闭钩子。

I have some user called exit points like , user clicks on exit, some error conditions etc. All the user exit points have proper logs which will be followed by the log in shutdown hook.

现在,对于我的一位用户,应用程序会不时退出。这里用户没有调用任何用户出口点。打印关闭挂钩日志。
defaultuncaughtexception处理程序没有异常。

Now for one of my user the application is getting exited from time to time . here user is not calling any user exit points. Shutdown hook logs are printed. There is no exception from defaultuncaughtexception handler.

我无法找到谁调用system.exit,因此无法调用shutdown hook。
我能以某种方式找到调用shutdown hook或system.exit()的内容吗?
关闭钩子的打印让我认为这是一个合适的jvm关闭而不是一个突然的。

I am not able to find who calls the system.exit and hence the shutdown hook . Can i somehow find what calls the shutdown hook or system.exit () ? Printing of shutdown hooks makes me think that is a proper jvm shutdown not an abrupt one.

最好的问候,
Saurav

Best Regards, Saurav

推荐答案

如果您怀疑有人明确调用 System.exit(...)或类似函数,你可以用 SecurityManager 拦截它:

If you suspect that someone invokes System.exit(…) or a similar function explicitly, you can intercept it with a SecurityManager:

System.setSecurityManager(new SecurityManager() {
    @Override
    public void checkExit(int status) {
        new Exception("exit attempt with return code "+status).printStackTrace();
    }
    // note that all dedicated check... methods delegate to the two below,
    // so overriding these is sufficient to enable all other actions
    @Override
    public void checkPermission(Permission perm, Object context) { }

    @Override
    public void checkPermission(Permission perm) { }
});

然而,这不会拦截由外部事件引起的关闭,如TERM信号等。

This, however, will not intercept shutdown caused by external events, like a TERM signal, etc.

这篇关于找出谁调用jvm shutdown hook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆