아무거나

스케쥴러 구현 (@EnableScheduling) 본문

Java & Kotlin/Kotlin

스케쥴러 구현 (@EnableScheduling)

전봉근 2024. 11. 12. 14:36
반응형

참고소스: https://github.com/bkjeon1614/kotlin-example-code/tree/main/kotlin-jpa-codebase

 

  • Application.kt 에 설정
    ...
    import org.springframework.scheduling.annotation.EnableScheduling
    
    @EnableScheduling // 추가
    class TestApplication {
      ...
    
      fun main(...)
    }
    
  • AppHealthCheckScheduler.kt 스케쥴러 테스트용 코드 작성
      package com.bkjeon.base.v1.schedule
    
      import org.apache.http.client.methods.HttpGet
      import org.apache.http.impl.client.CloseableHttpClient
      import org.apache.http.impl.client.HttpClients
      import org.slf4j.LoggerFactory
      import org.springframework.beans.factory.annotation.Value
      import org.springframework.scheduling.annotation.Scheduled
      import org.springframework.stereotype.Component
    
      @Component
      class AppHealthCheckScheduler {
    
          @Value("\${spring.config.activate.on-profile:}")
          lateinit var env: String
    
          companion object {
              private val logger = LoggerFactory.getLogger(AppHealthCheckScheduler::class.java)
              private val httpClient: CloseableHttpClient = HttpClients.createDefault()
          }
    
          // 10 분에 1번
          @Scheduled(cron = "* 0/10 * * * *")
          fun isAppHealthCheck() {
              val httpGet = HttpGet("https://www.naver.com/")
    
              try {
                  logger.info(">>>>>>>>>>>>>>>>>>>> Health Check: $env")
                  httpClient.execute(httpGet)
              } catch (e: Exception) {
                  logger.error(">>>>>>>>>>>>>>>>>>>> Error: $e, Env: $env")
              }
          }
    
      }  
    
반응형
Comments