java - JVM控制台应用程序中如何处理^C?

标签 java scala console jvm reliability

当我的 JVM 运行(实际上是用 Scala 编写的,但我倾向于认为解决方案对于 Groovy、Clojure 或纯 Java 来说几乎是相同的)控制台程序被用户终止时 Ctrl+C(或者按系统关机顺序,不知道程序有没有区别),如何确定应用程序的外部资源修改(数据库、文件、Web 服务抽象资源)处于可预测的、非逻辑损坏的状态?

最佳答案

您可以像其他人指出的那样尝试实现关闭 Hook 但是:

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

我想,我相信您必须将事务上下文引入到您的应用程序中。对于非常简单的数据库,对于文件系统,您可以查看 Apache Commons Transaction

关于java - JVM控制台应用程序中如何处理^C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128368/

相关文章:

scala - Spark DataFrame 并行性

windows - 以编程方式在 R Console 中设置字体和字体大小?

java - hibernate 一对一 : getId() without fetching entire object

java - Maven 无法从存储库中的另一个依赖项加载依赖项

java - 如何在 Play Framework 2.x [Java] 中模拟 Controller /休息端点以进行单元测试

scala - 展平 Scala 中的嵌套对象

c# - 二维数组棋盘

linux - 允许在 rbash 中完成制表符

java - Android:在 onPause 和 onResume 之间保存用户定义对象的 ArrayList

java - 当用户未使用 Servlet 登录时如何重定向到索引页面?