简体   繁体   English

如何调用在另一个线程中运行的程序的关闭挂钩

[英]How to invoke shutdown hooks of a program running in another thread

I am writing automated tests for a Swing application using Jemmy framework. 我正在使用Jemmy框架为Swing应用程序编写自动化测试。

My test suite run this application by invoking the main method of its Main class in a new thread. 我的测试套件通过在新线程中调用其Main类的main方法来运行此应用程序。

I have already written a lot of GUI-related tests but now I have got a more complex task. 我已经写了很多与GUI相关的测试,但是现在我有一个更复杂的任务。

I need to check if the tested application does some clean up of folders when it is being closed. 我需要检查被关闭的应用程序在关闭时是否对文件夹进行了一些清理。 This action is probably executed as a shutdown hook. 此操作可能作为关闭挂钩执行。 Is it somehow possible to invoke shutdown hooks of that application without calling System.exit(0) ? 是否有可能在不调用System.exit(0)的情况下调用该应用程序的关闭挂钩?

When this command is called both threads will be terminated. 调用此命令时,两个线程都将终止。 But I want the thread with my tests to continue running after the tested application is closed so I can check if those folders still exist or not. 但是我希望带有测试的线程在被测试的应用程序关闭后继续运行,以便我可以检查那些文件夹是否仍然存在。 Is it somehow possible to invoke the shutdown hooks without changing the architecture of my test suite? 是否可以在不更改测试套件体系结构的情况下调用关闭挂钩?

I found this, but I didn't test it: 我找到了,但是没有测试:

ApplicationShutdownHooks.hook().run(); // JRE 6
ApplicationShutdownHooks.runHooks();   // JRE 7

As you can see, the names of the methods changed over time, which means you really shouldn't do this. 如您所见,方法的名称随时间而改变,这意味着您实际上不应该这样做。 However, I think this should do the job, but the class is private. 但是,我认为这应该可以完成工作,但是班级是私人的。 Using some reflection, you could force the execution of it. 使用一些反射,您可以强制执行它。

// JRE 7
try
{
    Class cl = Class.forName("java.lang.ApplicationShutdownHooks");
    Method m = cl.getDeclaredMethod("runHooks");
    m.setAccessible(true);
    m.invoke(null);
} catch (Exception e)
{
    e.printStackTrace(System.out);
}

Put the important parts of your shutdown hooks in separate methods/classes, and then you will be able to unit-test this code without having to run the whole app. 将关闭挂钩的重要部分放在单独的方法/类中,然后您就可以对该代码进行单元测试,而不必运行整个应用程序。 Then, assuming you do register the hooks at app startup, you should pretty much be able to rely on the JVM to call your hooks, so not testing the fact that the hooks actually get executed is not so terrible IMO. 然后,假设您确实在应用程序启动时注册了钩子,那么您应该几乎可以依靠JVM来调用钩子,因此不要测试钩子实际执行的事实并不是那么糟糕的IMO。 If you really need to test the full end-to-end behavior, you should probably be running your testing code in a process separate from the application uder test, anyway, in which case you can test the actual expected cleanup behavior without needing to "plug into" the hooks (eg check that some files do exist after application startup but disappear after the app shuts down). 如果您确实需要测试完整的端到端行为,则可能应该在与应用程序uder测试分开的进程中运行测试代码,在这种情况下,您可以测试实际的预期清除行为,而无需“插入”挂钩(例如,在应用程序启动后检查某些文件是否确实存在,但在应用程序关闭后消失)。

If you're going to write tests, it's better to write good ones. 如果要编写测试,最好编写良好的测试。

You don't need to mess with the JVM to end a thread but not the other: you only need to be sure that when the window is closed the shutdown code is executed, otherwise a non-graceful shutdown is assumed. 您不需要弄乱JVM来结束线程,而不必弄乱另一个线程:您只需要确保在关闭窗口时执行关闭代码,否则就假定了非正常关闭。 Put that code in a method that you can test separately, and you're done. 将代码放在可以单独测试的方法中,就可以了。 Likely you don't really need to add a shutdown hook to the JVM. 可能您实际上并不需要向JVM添加关闭挂钩。

You may want to check (and possibly repair) the system status in your bootstrap code. 您可能要检查(并可能修复)引导程序代码中的系统状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何停止在 IntelliJ 中运行的进程,以便它调用关闭挂钩? - How do I stop a process running in IntelliJ such that it calls the shutdown hooks? 如何停止一个线程并调用另一个 - How to stop a thread and invoke another 运行多个线程的JVM如何处理ctrl-c,w /和w / o关闭挂钩? - How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks? Java - 如何在外部ExecutorService上调用shutdown时,使ExecutorService在另一个ExecutorService中运行而不关闭? - Java - How to make an ExecutorService running inside another ExecutorService not to shutdown when shutdown is invoked on the outer ExecutorService? runOnUiThread如何关闭/关闭线程 - runOnUiThread how to close/shutdown thread 关机挂钩可以依赖另一个线程吗? - Can a shutdown hook rely on another thread? 如何管理LocalConverter并在调用ShutDown()方法时? - How to manage LocalConverter and when invoke ShutDown() method? 生产者是单个主线程,有 n 个消费者在无限循环中运行,所有消费者都在阻塞队列上工作。 如何关机 - Producer is a single main thread and there are n consumers running in an infinite loop all working on blocking queue. how to shutdown 如何锁定/暂停在另一个类中运行的线程? - How to lock/pause the thread running in another Class? 如何在另一个线程运行时禁用文本框 - How to disable textbox while another thread is running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM