浏览代码

add OutOfMemoryErrorUncaughtExceptionHandler

tags/2.0.1
Jonathan Cobb 4 年前
父节点
当前提交
81f18e0d52
共有 1 个文件被更改,包括 29 次插入0 次删除
  1. +29
    -0
      src/main/java/org/cobbzilla/util/system/OutOfMemoryErrorUncaughtExceptionHandler.java

+ 29
- 0
src/main/java/org/cobbzilla/util/system/OutOfMemoryErrorUncaughtExceptionHandler.java 查看文件

@@ -0,0 +1,29 @@
package org.cobbzilla.util.system;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static org.cobbzilla.util.daemon.ZillaRuntime.shortError;
import static org.cobbzilla.util.daemon.ZillaRuntime.stacktrace;

@AllArgsConstructor @Slf4j
public class OutOfMemoryErrorUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

public static final OutOfMemoryErrorUncaughtExceptionHandler EXIT_ON_OOME = new OutOfMemoryErrorUncaughtExceptionHandler();

private final int status;

public OutOfMemoryErrorUncaughtExceptionHandler() { status = 2; }

@Override public void uncaughtException(Thread t, Throwable e) {
if (e instanceof OutOfMemoryError) {
try {
log.error("!!!!! OutOfMemoryError: calling System.exit("+status+") from: " + stacktrace(t));
} catch (Throwable ignored) {}
System.exit(status);
} else {
log.error("!!!!! Uncaught Exception: " + shortError(e));
}
}

}

正在加载...
取消
保存