首页 > 解决方案 > Spring Boot 计划任务未运行

问题描述

我无法弄清楚为什么我的计划任务不会运行。我在那里读到的大多数帮助都说他们在入口类上缺少@EnableScheduling,或者在调度程序所在的组件上缺少@Component。我的代码中都有这两个,所以我无法弄清楚我在这里缺少什么。

更新: 我看到如果我注释掉 taskExecutor bean,它就会开始工作。如何维护我的任务执行器并使用调度器?

主要的

@SpringBootApplication
@EnableAutoConfiguration
@EnableAsync
@EnableScheduling
public class MonitoringApplication {

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

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(1000);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("async-thread-");
        executor.initialize();
        return executor;
    }

    @Configuration
    public class WebConfiguration implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedMethods("*");
        }
    }

}

@Component
public class HealthMonitor {

    ArrayList<Application> apps = new ArrayList<>();

    HashMap<String, String> groupQuery = new HashMap<>();

    @Autowired
    MyMonitoringAdapter monitoringAdapter;

    @Autowired
    ServiceNowAdapter snowAdapter;

    @Autowired
    SitePinger sitePinger;

    @PostConstruct
    public void getAllAppHealth() {
        System.out.println("Monitor starting up...");
        buildAllApplications();
        populateProductLine();
        populateAppMonitors();
        getIncidents();
        System.out.println("Monitor started! - async processes may still be running");
    }

    @Scheduled(fixedRate = 1000)
    public void scheduledRefresh() {
        System.out.println("Fixed rate task async - ");
        refreshPing();
    }
}

标签: javaspring-bootscheduled-tasksjavabeansquartz-scheduler

解决方案


推荐阅读