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

SpringBoot应用中Scheduler Job未触发问题求助

Troubleshooting Your SpringBoot Scheduled Task Not Running

Hey there! Let's dig into why your UptadeCurrencyJob1 task never executes. I've reviewed your code snippets, and here are the key areas to check, ordered by how common they are:

1. Verify Your Cron Expression First

Your current cron is 0 0/10 1 * * ? — this translates to every 10 minutes starting at 1 AM, every day. If you're testing outside of 1 AM, the task won't trigger at all!

For quick testing, switch to a more frequent expression like 0 */1 * * * ? (runs every minute). Once you confirm the task works, switch back to your desired schedule.

2. Check Component Scan Scope

SpringBoot automatically scans beans in the package of your BlogsApplication class and its sub-packages. If UptadeCurrencyJob1 lives in a completely separate package (e.g., com.example.jobs while your app is in com.example.blogs), Spring won't pick up the @Component annotation.

Fix this by explicitly defining scan packages in your startup class:

@SpringBootApplication(scanBasePackages = {"com.example.blogs", "com.example.jobs"})
@EnableScheduling
@Import({SecurityConfig.class})
public class BlogsApplication implements CommandLineRunner {
    // ... your code
}

3. Ensure @EnableScheduling is Properly Applied

You've added the annotation to your startup class, but double-check that:

  • Your BlogsApplication is indeed the entry point of your app (the class with the main method).
  • There's no conflicting configuration that disables scheduling (e.g., custom task scheduler beans that aren't initialized correctly).

4. Check for Context Initialization Issues

Your startup class implements CommandLineRunner. If the run method throws an unhandled exception, it could halt Spring's context initialization before your scheduled task bean is fully registered.

Test this temporarily by commenting out the CommandLineRunner implementation and restarting your app. If the task starts running, you'll know the issue is in your run method logic.

5. Replace System.out.println with Proper Logging

Sometimes console output can be suppressed or missed, especially in production environments. Use Spring's built-in logging to confirm execution:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class UptadeCurrencyJob1 {
    private static final Logger logger = LoggerFactory.getLogger(UptadeCurrencyJob1.class);

    @Scheduled(cron = "0 */1 * * * ?") // Test with every minute first
    public void reportCurrentTime() throws IOException {
        logger.info("UptadeCurrencyJob1 is executing!");
    }
}

Check your application logs for this message instead of relying on console prints.

6. Confirm Class and Method Access Modifiers

Make sure:

  • UptadeCurrencyJob1 is a public class (your code shows this, but it's worth double-checking).
  • The reportCurrentTime method is public (you already have this right, but non-public methods can cause issues with Spring's proxying).

Start with the first two checks — they're the most likely culprits. Once you get the task running with the test cron expression, you can adjust it back to your original schedule.

内容的提问来源于stack exchange,提问作者Nunyet Calçada

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:40:26