자바에서 백그라운드 잡을 비동기로 처리하고 싶을때 사용

 

- Apache commons exec / ProcessBuilder 보다 많은 기능을 제공

- 자세한 내용은 아래 깃헙 참조 

- zt-exec 는 2020 년 apache commons exec 는 2014년 

- zt-exec 는 최신까지 패치는 안되어 최신 보안 이슈는 존재 (내부 의존성 때문, 2022.08 기준 )

 

java

- https://github.com/zeroturnaround/zt-exec

 

GitHub - zeroturnaround/zt-exec: ZeroTurnaround Process Executor

ZeroTurnaround Process Executor. Contribute to zeroturnaround/zt-exec development by creating an account on GitHub.

github.com

  public static int exec2(String... args) throws Exception {
    log.info("started at : {}", getCurrentTimestamp());

    String[] params = { "7", "5", "1" };
    for (String p : params) {
      String[] command = { "python3", "/Users/wonsama/Desktop/spring-dev/elk-log-test/test.py", p };
      Future<ProcessResult> future = new ProcessExecutor().command(command).readOutput(true).start().getFuture();
      // 아래 두 라인 주석 해제를 하면 동기 처리 됨
      // String output = future.get(60, TimeUnit.SECONDS).outputUTF8();
      // log.info("output : {}", output);
    }
    log.info("end at : {}", getCurrentTimestamp());
    return 0;
  }

python

import time
from datetime import datetime
import sys

loop = int(sys.argv[1])

for idx in range(1, loop+1):
    time.sleep(1)

f = open("result.txt", "a")
f.write("argv1 : {}, occured : {}\n".format(
    sys.argv[1], datetime.now(tz=None)))
f.close()

+ Recent posts