简体   繁体   中英

How to test proper working of Quartz job in Spring Boot application

I want to test if my Quartz trigger is working as it supposes in practice. My Quartz configuration looks like:

@Configuration
public class QuartzConfiguration {

  @Bean
  public JobDetail verificationTokenRemoverJobDetails() {
    return
        JobBuilder
            .newJob(VerificationTokenQuartzRemoverJob.class)
            .withIdentity("Job for verification token remover")
            .storeDurably()
            .build();
  }

  @Bean
  public Trigger verificationTokenRemoverJobTrigger(JobDetail jobADetails) {
    return
        TriggerBuilder
            .newTrigger()
            .forJob(jobADetails)
            .withIdentity("Trigger for verification token remover")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0/2 1/1 * ? *"))
            .build();
  }
}

and my Job class looks like:

@AllArgsConstructor
public class VerificationTokenQuartzRemoverJob implements Job {

  private VerificationTokenRepository verificationTokenRepository;

  @Override
  public void execute(JobExecutionContext context) {
    verificationTokenRepository.deleteAllByCreatedLessThan(LocalDateTime.now().minusMinutes(30));
  }
}

When I am starting my Spring Boot application in logs I can realize that Job is working and triggered cyclical but it's not enough to confirm the proper working.

That's why I decided to create a JUnit test. I found a tutorial: click but an owner used a clause while(true) which according to this topic: click is not a preferable option. Here occurs a question, is there any other option to verify the Job class name, the identity of the trigger and check if CRON expression and the concrete job are called as often as possible?

If it possible I will be grateful for suggestions on how to reach a desirable effect.

Please not that the above answer on using the Spring Scheduling has one big drawback: This works nicely if you just run a single instance of your application, but as soon as you scale up to multiple instances it becomes more complex: You might want to run the job only once at a certain interval but if two nodes run simultaneously the job might run on both nodes (so basically twice). Quartz can handle these kind of situations because it can have a central database through which it can coordinate if a job is already started. With spring scheduling the job will run on both nodes.

With SpringBoot, You could do easier doing the following

--- Option 1 ---

@Configuration
// This is important!
@EnableScheduling
public class Configuration{
 // TODO Change to 0 0 0/2 1/1 * ? *
 @Scheduled(cron = "0 15 10 15 * ?")
 public void scheduleTaskUsingCronExpression() {

    long now = System.currentTimeMillis() / 1000;
     System.out.println(
        "schedule tasks using cron jobs - " + now);
  }      

}

Full Example: https://www.baeldung.com/spring-scheduled-tasks

--- Option 2-> Programatically ---

@Configuration
@EnableScheduling

public class Configuration implements SchedulingConfigurer {
   @Bean(destroyMethod = "shutdown")
   public Executor taskExecutor() {
      return Executors.newScheduledThreadPool(100);
   }

    @Override
            public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            CronTrigger cronTrigger
            = new CronTrigger("* * * * * *");

                    taskRegistrar.setScheduler(taskExecutor());
                    taskRegistrar.addTriggerTask(
                            new Runnable() {
                                @Override public void run() {
                                    System.out.println("RUN!!!");
                                }
                            },
                            cronTrigger
                    );
                }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM