Search in sources :

Example 81 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project tomee by apache.

the class ThreadStackRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    if (System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows")) {
        return base;
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final ScheduledExecutorService ses = Executors.newScheduledThreadPool(1, new DaemonThreadFactory(ThreadStackRule.class.getSimpleName() + "-"));
            final ScheduledFuture<?> task = ses.scheduleAtFixedRate(new Runnable() {

                @Override
                public void run() {
                    final RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
                    String pid = bean.getName();
                    if (pid.contains("@")) {
                        pid = pid.substring(0, pid.indexOf("@"));
                    }
                    try {
                        Pipe.pipe(Runtime.getRuntime().exec("kill -3 " + pid));
                    } catch (final Exception exception) {
                        exception.printStackTrace();
                    }
                }
            }, 2, 2, TimeUnit.MINUTES);
            try {
                base.evaluate();
            } finally {
                task.cancel(true);
                ses.shutdownNow();
            }
        }
    };
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Statement(org.junit.runners.model.Statement) DaemonThreadFactory(org.apache.openejb.util.DaemonThreadFactory) RuntimeMXBean(java.lang.management.RuntimeMXBean)

Aggregations

RuntimeMXBean (java.lang.management.RuntimeMXBean)81 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)13 MemoryMXBean (java.lang.management.MemoryMXBean)10 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)9 IOException (java.io.IOException)8 ThreadMXBean (java.lang.management.ThreadMXBean)7 MemoryUsage (java.lang.management.MemoryUsage)6 ArrayList (java.util.ArrayList)6 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 Properties (java.util.Properties)5 InputStream (java.io.InputStream)4 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)4 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 InputStreamReader (java.io.InputStreamReader)3