한 번 생성 된 스레드가 24x7 실행해야하는 스레드가있는 스프링 부트 스레드 풀 프로젝트를 만들었지 만 유지 관리를 위해 서버에서 응용 프로그램을 중지해야 할 때 현재 작업을 완료 한 후 종료하고 새 작업을 다시 시작하지 않습니다.24x7 실행중인 스프링 부트 스레드 풀 프로젝트를 우회적으로 종료 할 수 있습니다
동일한에 대한 나의 코드는 다음과 같습니다 구성 클래스
@Configuration
public class ThreadConfig {
@Bean
public ThreadPoolTaskExecutor taskExecutor(){
ThreadPoolTaskExecutor executorPool = new ThreadPoolTaskExecutor();
executorPool.setCorePoolSize(10);
executorPool.setMaxPoolSize(20);
executorPool.setQueueCapacity(10);
executorPool.setWaitForTasksToCompleteOnShutdown(true);
executorPool.setAwaitTerminationSeconds(60);
executorPool.initialize();
return executorPool;
}
}
의 Runnable 클래스
@Component
@Scope("prototype")
public class DataMigration implements Runnable {
String name;
private boolean run=true;
public DataMigration(String name) {
this.name = name;
}
@Override
public void run() {
while(run){
System.out.println(Thread.currentThread().getName()+" Start Thread = "+name);
processCommand();
System.out.println(Thread.currentThread().getName()+" End Thread = "+name);
if(Thread.currentThread().isInterrupted()){
System.out.println("Thread Is Interrupted");
break;
}
}
}
private void processCommand() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void shutdown(){
this.run = false;
}
}
Main 클래스 : 나는 중지해야 할 경우 이해하는 데 도움이 필요
@SpringBootApplication
public class DataMigrationPocApplication implements CommandLineRunner{
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
public static void main(String[] args) {
SpringApplication.run(DataMigrationPocApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
for(int i = 1; i<=20 ; i++){
taskExecutor.execute(new DataMigration("Task " + i));
}
for (;;) {
int count = taskExecutor.getActiveCount();
System.out.println("Active Threads : " + count);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count == 0) {
taskExecutor.shutdown();
break;
}
}
System.out.println("Finished all threads");
}
}
내 스프링 부팅 응용 프로그램은 실행중인 모든 20 개의 스레드를 중지해야합니다 (24x7) while 루프 및 종료에서 현재 루프를 완료 한 후 erwise.
제안 해 주셔서 감사합니다. 하지만 한 가지 더 이해해야합니다. 저는이 봄 부팅의 항아리를 만들고 리눅스 환경에서 실행합니다. 이 과정을 어떻게 중단 할 것입니까? 24x7로 실행되도록 설계하고 작업을 수행해야하기 때문에 스프링 부트를 중지하려고하면 현재 실행중인 작업을 완료하고 새로운 while 루프를 시작하기 전에 중지해야합니다. 코드 디자인을 도와주세요. 감사합니다 –
대답을 명확히했습니다. 도움이 되었으면 좋겠다 –
고마워. 답장! 그리고 응용 프로그램을 종료하기 위해 linux env에서 JMX 종료 종점을 호출하는 방법. –