Home > Net >  Why @Scheduled cron not called at specific time?
Why @Scheduled cron not called at specific time?

Time:12-11

I'm trying to call a api every 23:30 So I decided to use @Scheduled annotation.

To use @Scheduled annotation, I placed @EnableScheduling annotation in the main class.

Application Class(Main Class)

@EnableScheduling
@SpringBootApplication(scanBasePackages= {"com.wook.model","com.wook.weather","com.wook.service"})
public class Application implements CommandLineRunner{

    private final ApiKey APK;
    
    private GeoService gs; //To get GeoService
    private ShortWeatherService sws;
    private List<Temperature> temperList;
    private List<ShortWeatherReq> swrList;
    private ShortWeatherReq swr;
    private TempService ts; //To save TempService
    
    
    private Logger logger = LoggerFactory.getLogger(Application.class); //로그를 찍기 위해서 사용하는 Class
    
    @Autowired
    public Application(ApiKey apk, GeoService gs, ShortWeatherService sws,ShortWeatherReq swr, TempService ts) {
        this.APK = apk;
        this.gs = gs;
        this.sws = sws;
        this.swr = swr;
        this.ts = ts;
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    public void run( String... args ) throws Exception {
        
        swrList = new ArrayList<>();
        
        //DB에 저장된 GeoInfo 정보를 ShortWeatherReq List에 추가한다.
        for(GeoInfo gi : gs.getGeoXY()) {
            swr = new ShortWeatherReq(APK.getApiKey(),gi.getX(),gi.getY());
            swr.setBaseDate(); //현재 날짜로 baseDate를 설정하는 메소드를 호출
            swr.setNx(gi.getX()); //x좌표값 저장
            swr.setNy(gi.getY()); //y좌표값 저장
            
            swrList.add(swr); //list에 swr 추가
        }
        
        //위 객체를 가지고 이제 API를 호출할수 있게 Service에게 전해줘야 함
        sws.setSwrList(swrList);
        
        temperList = sws.callSW(); // Call API
        
        logger.info("-------------------");
        
        logger.info("API ConnectionSuccess");
        
        logger.info("-------------------");
        
        //List에 담긴 온도 DB에 저장
        for(int i = 0;i<temperList.size();i  ) {
            Temperature temp = temperList.get(i);
            ts.saveTemp(temp);
        }
        
        logger.info("DB Store Success");
    }
    
}

When calling API in the main class call Service class, the service class used @Scheduled annotation to make it called at a specific time.

Service Class

@Scheduled(cron="0 30 23 * * *")
public List<Temperature> callSW() throws InterruptedException {}

However, when I run this code, the main class does not call the service at the time I specify.

How can I solve this problem??

CodePudding user response:

From your comment

It runs right away

This is because inside

 public void run( String... args ) throws Exception {

you have

 temperList = sws.callSW(); // Call API

Where you manually invoke the call.

This run method is used when the application starts up.

Also since you use implements CommandLineRunner this is just a command line application backed up by spring. So it executes the run method only once and then get's terminated when the run method finishes. So when the time arrives for scheduler to execute, the application is not running at all.

  • Related