Issue
I'm trying to change the cron expression of a specific method with the value defined in a Database. I'm normally using Spring annotations for this.
For Example:
@Scheduled(cron = "cronExpression from Database")
public void typeAJob(){
...
}
It would be ok if after the start of my program these cron expressions count not be changed anymore. But at the start, it should read the value from the Database and use it as cron expression for this method.
The final goal would be, that I have X numbers of Jobs in the Database with each having his own cron expressions. The jobs will have a specific type (typeAJob, typeBJob, etc.). Each type will have his own Scheduled method.
maybe someone has a Idee, thank you :)
Solution
I found a Solution:
Creating a ThreadPoolTaskScheduler
witch I can use to execute a runnable class:
public ExportJobService exportScheduler(ThreadPoolTaskScheduler threadPoolTaskScheduler) {
threadPoolTaskScheduler.setPoolSize(5);
threadPoolTaskScheduler.setThreadNamePrefix("ExportJobScheduler");
return new ExportJobService(threadPoolTaskScheduler);
}
I use PostConstruct
to load the specific cronExceptions from the database and execute all jobs at the start of my program.
@PostConstruct
public void runJobs() {
repository.findAll()
.stream()
.map(this::buildJob)
.forEach(job -> {
var cronTrigger = new CronTrigger(job.getCronExpression());
taskScheduler.schedule(job, cronTrigger);
});
}
You can find more informations here: https://www.baeldung.com/spring-task-scheduler
Answered By - Xxtreem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.