Home > Enterprise >  Sring actuator system status missing scheduler data when schedulers are added programatically
Sring actuator system status missing scheduler data when schedulers are added programatically

Time:06-03

I am using Java 11, Spring Boot. I had my schedulers running with @Scheduled annotation and they appeared in Actuator page: #/administration/systemstatus automatically. But since now I have to provide possibility to schedule a task forcibly from UI I have to invoke schedule operation on TaskScheduler for such task for current time and after it ends, I have to again schedule such task according to some cron expression from application.properties (because schedule operations changes the scheduled time and I tasks generally have to be working on specified cron times and only sometimes will be additionally triggered by user form UI). That lead me to write my own scheduling component as I found it impossible to do all of this with @Schedule annotation. But as a side effect I don't have data about tasks and their cron expressions in actuator systemstatus. Below is the code, I include only a part that adds tasks to scheduler when application is deployed. I am not adding reschedule operations. Do u know how to repair the actuator data ?

@Component
@Slf4j
public class MyTaskService {

    private final TaskScheduler scheduler;
    private final List<CustomTask> taskList; //runnable tasks
    private final RunningTasksFutureHolder runningTasksFutureHolder;

    public MyTaskService(TaskScheduler scheduler,
                               List<CustomTask> taskList,
                               RunningTasksFutureHolder runningTasksFutureHolder) {
        this.scheduler = scheduler;
        this.taskList = taskList;
        this.runningTasksFutureHolder = runningTasksFutureHolder;
    }

    @PostConstruct
    public void afterLoad() {
        askList.forEach(this::addTaskToScheduler);
    }

    public void addTaskToScheduler(CustomTask task) {
    //here tasks are added to scheduler and they are not visible in actuator
        ScheduledFuture<?> scheduledTask = scheduler.schedule(task, new CronTrigger(task.getCronExpression()));
        runningTasksFutureHolder.add(task.getTaskType(), scheduledTask);
    }

}





@Configuration
@EnableScheduling
public class SchedulingConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //do nothing here
    }

    @Bean
    public TaskScheduler poolScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        scheduler.setPoolSize(5);
        scheduler.initialize();
        return scheduler;
    }
}

CodePudding user response:

You should use ScheduledTaskRegistrar rather than calling TaskScheduler directly. For the cron task the corresponding method is addCronTask().

References:

  • Related