Home:ALL Converter>Generate thread dump from java code

Generate thread dump from java code

Ask Time:2018-06-08T22:11:08         Author:George Cimpoies

Json Formatter

I have the following method which overrides the default sorter in Jenkins:

@Override
public void sortBuildableItems(List<Queue.BuildableItem> items) {
    logBuildQueue(items, "BEFORE");
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future future =  executorService.submit(new Runnable() {
        @Override public void run() {
            items.sort(CustomSorter::compare);
        }
    });
    try {
        future.get(2, TimeUnit.SECONDS);
    }
    catch (InterruptedException e) {
        LOGGER.log(Level.SEVERE, "[INTERRUPTED EXCEPTION] Message:" + e.getMessage() + " StackTrace: " + Arrays.toString(e.getStackTrace()));
        setDefaultSorter();
        e.printStackTrace();
    }
    catch (ExecutionException e) {
        LOGGER.log(Level.SEVERE, "[EXECUTION EXCEPTION] Message:" + e.getMessage() + " StackTrace: " + Arrays.toString(e.getStackTrace()));
        setDefaultSorter();
        e.printStackTrace();
    }
    catch (TimeoutException e) {
        LOGGER.log(Level.SEVERE, "[TIMEOUT EXCEPTION] Sorting the items took more then 2 seconds, Message:" + e.getMessage() + " StackTrace: " + Arrays.toString(e.getStackTrace()));
        setDefaultSorter();
        e.printStackTrace();
    }

    logBuildQueue(items, "AFTER");
}

I'm looking to print all I can get my hands on in case an exception occurs, and I was wondering if there's any way to get a thread dump pushed to the logs, since the error message might be absent.

Author:George Cimpoies,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762552/generate-thread-dump-from-java-code
yy