[ 개발 ] Spring 비동기 딜레이 주기

장태규·2024년 11월 18일

[ WebSocket ]

목록 보기
7/8

@EnableScheduling

@EnableScheduling
@SpringBootApplication
public class QuizSever21Application {

	public static void main(String[] args) {
		SpringApplication.run(QuizSever21Application.class, args);
	}

}

비동기 방식 딜레이 처리를 위한 어노테이션

ScheduledExecutorService 객체 활용

// 3초 후에 다음 질문 호출
            ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.schedule(() ->
            {
                try
                {
                    callNextQuestion();
                } catch (IOException e)
                {
                    System.err.println("Error during delay: " + e.getMessage());
                }
            }, 3, TimeUnit.SECONDS);

-ScheduledExecutorService : 비동기 스레드 작업 처리를 위한 객체
-Executors.newSingleThreadScheduledExecutor() 단일 스레드 작업처리를 위해 객체 생성
-scheduler.schedule() : 실행할 작업, 시간, 시간 유형 지정

profile
무럭무럭 자라나는 중

0개의 댓글