You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot中@Scheduled定时任务执行报错求助

Hey there, let's troubleshoot why your Spring Boot scheduled task is throwing errors step by step. First, let's spot some obvious red flags in your code, then cover other potential issues:

1. Critical Variable Name Typo in Your Code

Looking at your task logic, you’ve assigned the hotel list to currenciesInfo, but tried to loop over hotelsInfo—a variable that doesn’t exist here! This will definitely trigger a NullPointerException or a compilation error (if you’re lucky enough to catch it at build time):

List<Hotel> currenciesInfo = hotelService.getAllHotelsInfo(); 
for (Hotel hotelInfo : hotelsInfo) { // 🔴 Should be `currenciesInfo`, not `hotelsInfo`!
    hotelPriceService.updateHotelPrice (hotelInfo, usdRate); 
}

Fixing this typo is the first thing you should do—it’s likely the root cause of your immediate error.

2. Uncaught Exceptions Breaking Task Execution

Your method declares throws IOException, but Spring’s @Scheduled tasks will terminate abruptly if an uncaught exception is thrown. Worse, default logging might truncate the error message (which explains why your log cuts off mid-timestamp). Wrap the entire task logic in a try-catch block to capture and log the full exception stack trace:

@Scheduled(cron = "0 0/10 * * * ?") 
public void hotelTime() { 
    LOG.info("updating all hotels at " + new Date()); 
    try {
        List<Hotel> hotelsInfo = hotelService.getAllHotelsInfo(); 
        // Add null/empty check here too!
        if (hotelsInfo != null && !hotelsInfo.isEmpty()) {
            for (Hotel hotelInfo : hotelsInfo) { 
                hotelPriceService.updateHotelPrice(hotelInfo, usdRate); 
            }
            LOG.info("updated at " + new Date()); 
            currencyPriceService.deletePriceOlderThan1Month(); 
        } else {
            LOG.warn("No hotels found to update—skipping task execution");
        }
    } catch (Exception e) {
        LOG.error("Failed to run hotel update task", e); // 🟢 Prints full error stack
    }
}

This will give you the complete error details instead of a truncated log line.

3. Potential Issues with Dependent Services

Even after fixing the typo, your task might fail due to issues in downstream services:

  • hotelService.getAllHotelsInfo() could return null (instead of an empty list), which would crash the loop. The null check added above prevents this.
  • hotelPriceService.updateHotelPrice or currencyPriceService.deletePriceOlderThan1Month might throw database-related errors: connection timeouts, lock conflicts, invalid SQL, or missing permissions. The full exception log from the try-catch will reveal these.

4. Task Scheduler Configuration Gotchas

  • Timezone Misalignment: Your cron expression 0 0/10 * * * ? is set to run every 10 minutes, but if your app’s timezone doesn’t match your expected schedule, tasks might run at unexpected times or fail silently. Verify your app’s timezone configuration (check spring.timezone in application.properties).
  • Thread Pool Limitations: Spring’s default task scheduler uses a single thread. If another long-running task is using that thread, your hotel update task will be blocked. Increase the thread pool size with a custom bean:
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(5);
        scheduler.setThreadNamePrefix("hotel-task-pool-");
        return scheduler;
    }
    

5. Log Truncation or File Rolling Issues

Your truncated log line (2018-03-25 19:4...) suggests your logging framework (Logback/Log4j) might be limiting line lengths, or the log file rolled over mid-entry. Check your logging config to ensure:

  • Exception stack traces are not truncated.
  • You’re looking at the full set of log files for that timestamp (not just the active one).

内容的提问来源于stack exchange,提问作者Salvador Borés

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:12:33