设为首页 收藏本站
查看: 569|回复: 0

[经验分享] Tomcat 中的shutdowhook

[复制链接]

尚未签到

发表于 2015-8-9 12:28:09 | 显示全部楼层 |阅读模式
今天去Tomcat代码里面查东西,无意中发现:

1     /**
2      * Start a new server instance.
3      */
4     public void start() {
5
6         if (server == null) {
7             load();
8         }
9
10         long t1 = System.currentTimeMillis();
11
12         // Start the new server
13         if (server instanceof Lifecycle) {
14             try {
15                 ((Lifecycle) server).start();
16             } catch (LifecycleException e) {
17                 log.error("Catalina.start: ", e);
18             }
19         }
20
21         long t2 = System.currentTimeMillis();
22         log.info("Server startup in " + (t2 - t1) + " ms");
23
24         try {
25             // Register shutdown hook
26             if (useShutdownHook) {
27                 if (shutdownHook == null) {
28                     shutdownHook = new CatalinaShutdownHook();
29                 }
30                 Runtime.getRuntime().addShutdownHook(shutdownHook);
31             }
32         } catch (Throwable t) {
33             // This will fail on JDK 1.2. Ignoring, as Tomcat can run
34             // fine without the shutdown hook.
35         }
36
37         if (await) {
38             await();
39             stop();
40         }
41
42     }
其中:


1     /**
2      * Shutdown hook which will perform a clean shutdown of Catalina if needed.
3      */
4     protected class CatalinaShutdownHook extends Thread {
5
6         public void run() {
7
8             if (server != null) {
9                 Catalina.this.stop();
10             }
11            
12         }
13
14     }

而:

1   /**
2      * Registers a new virtual-machine shutdown hook.
3      *
4      *  The Java virtual machine shuts down in response to two kinds
5      * of events:
6      *
7      *   
8      *
9      *     The program exits normally, when the last non-daemon
10      *   thread exits or when the {@link #exit exit} (equivalently,
11      *   {@link System#exit(int) System.exit}) method is invoked, or
12      *
13      *     The virtual machine is terminated in response to a
14      *   user interrupt, such as typing ^C, or a system-wide event,
15      *   such as user logoff or system shutdown.
16      *
17      *   
18      *
19      *  A shutdown hook is simply an initialized but unstarted
20      * thread.  When the virtual machine begins its shutdown sequence it will
21      * start all registered shutdown hooks in some unspecified order and let
22      * them run concurrently.  When all the hooks have finished it will then
23      * run all uninvoked finalizers if finalization-on-exit has been enabled.
24      * Finally, the virtual machine will halt.  Note that daemon threads will
25      * continue to run during the shutdown sequence, as will non-daemon threads
26      * if shutdown was initiated by invoking the {@link #exit exit}
27      * method.
28      *
29      *  Once the shutdown sequence has begun it can be stopped only by
30      * invoking the {@link #halt halt} method, which forcibly
31      * terminates the virtual machine.
32      *
33      *  Once the shutdown sequence has begun it is impossible to register a
34      * new shutdown hook or de-register a previously-registered hook.
35      * Attempting either of these operations will cause an
36      * {@link IllegalStateException} to be thrown.
37      *
38      *  Shutdown hooks run at a delicate time in the life cycle of a virtual
39      * machine and should therefore be coded defensively.  They should, in
40      * particular, be written to be thread-safe and to avoid deadlocks insofar
41      * as possible.  They should also not rely blindly upon services that may
42      * have registered their own shutdown hooks and therefore may themselves in
43      * the process of shutting down.
44      *
45      *  Shutdown hooks should also finish their work quickly.  When a
46      * program invokes {@link #exit exit} the expectation is
47      * that the virtual machine will promptly shut down and exit.  When the
48      * virtual machine is terminated due to user logoff or system shutdown the
49      * underlying operating system may only allow a fixed amount of time in
50      * which to shut down and exit.  It is therefore inadvisable to attempt any
51      * user interaction or to perform a long-running computation in a shutdown
52      * hook.
53      *
54      *  Uncaught exceptions are handled in shutdown hooks just as in any
55      * other thread, by invoking the {@link ThreadGroup#uncaughtException
56      * uncaughtException} method of the thread's {@link
57      * ThreadGroup} object.  The default implementation of this method
58      * prints the exception's stack trace to {@link System#err} and
59      * terminates the thread; it does not cause the virtual machine to exit or
60      * halt.
61      *
62      *  In rare circumstances the virtual machine may abort, that is,
63      * stop running without shutting down cleanly.  This occurs when the
64      * virtual machine is terminated externally, for example with the
65      * SIGKILL signal on Unix or the TerminateProcess call on
66      * Microsoft Windows.  The virtual machine may also abort if a native method goes awry
67      * by, for example, corrupting internal data structures or attempting to
68      * access nonexistent memory.  If the virtual machine aborts then no
69      * guarantee can be made about whether or not any shutdown hooks will be
70      * run.
71      *
72      * @param   hook
73      *          An initialized but unstarted {@link Thread} object
74      *
75      * @throws  IllegalArgumentException
76      *          If the specified hook has already been registered,
77      *          or if it can be determined that the hook is already running or
78      *          has already been run
79      *
80      * @throws  IllegalStateException
81      *          If the virtual machine is already in the process
82      *          of shutting down
83      *
84      * @throws  SecurityException
85      *          If a security manager is present and it denies
86      *          {@link RuntimePermission}("shutdownHooks")
87      *
88      * @see #removeShutdownHook
89      * @see #halt(int)
90      * @see #exit(int)
91      * @since 1.3
92      */
93     public void addShutdownHook(Thread hook) {
94     SecurityManager sm = System.getSecurityManager();
95     if (sm != null) {
96         sm.checkPermission(new RuntimePermission("shutdownHooks"));
97     }
98     Shutdown.add(hook);
99     }
再来看看Shutdow:

1     /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
2      * but does not do any security checks.
3      */
4     static void add(Thread hook) {
5     synchronized (lock) {
6         if (state > RUNNING)
7         throw new IllegalStateException("Shutdown in progress");
8         if (hook.isAlive())
9         throw new IllegalArgumentException("Hook already running");
10         if (hooks == null) {
11         hooks = new HashSet(11);
12         hooks.add(new WrappedHook(hook));
13         Terminator.setup();
14         } else {
15         WrappedHook wh = new WrappedHook(hook);
16         if (hooks.contains(wh))
17             throw new IllegalArgumentException("Hook previously registered");
18         hooks.add(wh);
19         }
20     }
21     }

结论:
还是好多东西以前不知道,想起那个怪圈来了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-96375-1-1.html 上篇帖子: 关于TOMCAT的安装和配置 下篇帖子: How Tomcat Works(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表