View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.io.BufferedWriter;
19  import java.io.FileOutputStream;
20  import java.io.IOException;
21  import java.io.OutputStreamWriter;
22  import java.io.Writer;
23  import java.util.Map;
24  import java.util.function.Consumer;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.codelibs.core.exception.IORuntimeException;
29  import org.codelibs.fess.Constants;
30  
31  /**
32   * Utility class for generating and writing thread dumps.
33   * Provides methods to capture thread information for debugging purposes.
34   */
35  public class ThreadDumpUtil {
36      private static final Logger logger = LogManager.getLogger(ThreadDumpUtil.class);
37  
38      /**
39       * Private constructor to prevent instantiation of this utility class.
40       */
41      protected ThreadDumpUtil() {
42          // noop
43      }
44  
45      /**
46       * Prints thread dump information to the logger at INFO level.
47       */
48      public static void printThreadDump() {
49          processThreadDump(logger::info);
50      }
51  
52      /**
53       * Prints thread dump information to the logger at WARN level.
54       */
55      public static void printThreadDumpAsWarn() {
56          processThreadDump(logger::warn);
57      }
58  
59      /**
60       * Prints thread dump information to the logger at ERROR level.
61       */
62      public static void printThreadDumpAsError() {
63          processThreadDump(logger::error);
64      }
65  
66      /**
67       * Writes thread dump information to the specified file.
68       *
69       * @param file the file path to write the thread dump to
70       */
71      public static void writeThreadDump(final String file) {
72          try (final Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Constants.CHARSET_UTF_8))) {
73              processThreadDump(s -> {
74                  try {
75                      writer.write(s);
76                      writer.write('\n');
77                  } catch (final IOException e) {
78                      throw new IORuntimeException(e);
79                  }
80              });
81          } catch (final Exception e) {
82              logger.warn("Failed to write thread dump: file={}", file, e);
83          }
84      }
85  
86      /**
87       * Processes all thread information and sends it to the provided consumer.
88       *
89       * @param writer the consumer that will handle each line of thread dump output
90       */
91      public static void processThreadDump(final Consumer<String> writer) {
92          for (final Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
93              writer.accept("Thread: " + entry.getKey());
94              final StackTraceElement[] trace = entry.getValue();
95              for (final StackTraceElement element : trace) {
96                  writer.accept("\tat " + element);
97              }
98          }
99      }
100 }