BrodcastExecuteResultHandler.java 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cn.exlive.monitor.utils;
  2. import org.apache.commons.exec.DefaultExecuteResultHandler;
  3. import java.util.function.Consumer;
  4. import java.util.function.Function;
  5. /**
  6. * <pre>
  7. *
  8. * Created by zhaopx.
  9. * Date: 2025/6/30
  10. * Time: 20:32
  11. * Vendor: exlive.cn
  12. *
  13. * </pre>
  14. *
  15. * @author zhaopx
  16. */
  17. public class BrodcastExecuteResultHandler extends DefaultExecuteResultHandler {
  18. /**
  19. * 执行回调
  20. */
  21. final Consumer consumer;
  22. public BrodcastExecuteResultHandler(Consumer<Process> consumer) {
  23. this.consumer = consumer;
  24. }
  25. /**
  26. * 执行开始
  27. */
  28. public void onProcessStart(final Process process) {
  29. try {
  30. Thread.sleep(3000);
  31. } catch (Exception ignore) {}
  32. try {
  33. consumer.accept(process);
  34. } catch (Exception ignore) {}
  35. }
  36. /**
  37. * 获取进程PID
  38. * @return
  39. */
  40. public static int getProcessPid(Process process) {
  41. // 获取进程PID
  42. return process == null ? -1 : PidExecuteWatchdog.getPid(process).orElseGet(() -> -1);
  43. }
  44. }