Tuesday, June 25, 2013

How to catch JVM crash

Problem:
 A scheduled task will invoke 3rd party library and sometimes JVM crashes and it cannot be recovered.
Solution:
 Use ProcessBuilder to invoke the possible-crash application. (It works on JRE7, not on JRE6)
public
class CrashExample {
@SuppressWarnings("restriction")
public static void main(String[] args) {
System.
out.println("Hello, I will crash!");
new sun.dc.pr.PathDasher(null);
}
}

import
java.io.IOException;
import
java.util.concurrent.Executors;
import
java.util.concurrent.TimeUnit;
 
public
class FixedTest {
public static void main(String[] args) {
Executors.newScheduledThreadPool(10).scheduleAtFixedRate(
new Runnable() {
volatile int cnt = 0;
volatile boolean is_normal;
@Override
public void run() {
cnt++;
if(is_normal||cnt<5){
System.
out.println("Running..."+cnt);
}
else{
String separator = System.getProperty(
"file.separator");
String classpath = System.getProperty(
"java.class.path");
String path = System.getProperty(
"java.home")
+ separator +
"bin" + separator + "java";
System.
out.println("path="+path);
ProcessBuilder processBuilder =
new ProcessBuilder(path, "-cp",
classpath,
CrashExample.
class.getName());
// processBuilder.inheritIO();
try {
Process process = processBuilder.start();
process.waitFor();
}
catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.
out.println("Running "+cnt);
is_normal = true;
}
}
}, 100, 1000, TimeUnit.
MILLISECONDS);
}
}

1 comment:

  1. Good information. World-class thread dump analyzer to fire fight any sort of JVM related availability, scalability and performance problems. Applies several intelligence patterns discovers the root cause of the problem instantly.Java Thread Dump Analyzer

    ReplyDelete