1

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.

Martin
  • 1,139
  • 4
  • 23
  • 49
  • 3
    Looking for actual solutions of unit-testing Quartz-based implementations in Spring, I found this thread very disappointing. Please, consider updating your question so it reflects that you were - in fact - looking for an easier solution for scheduling in Spring, not for testing practices for Spring + Quartz combination of frameworks. – ryfterek Jun 16 '20 at 18:42
  • I downvoted for this question. The answer and the question aren't related as @ryfterek mentioned. Kindly update your question or remove it. – Niamatullah Bakhshi Sep 06 '22 at 03:55

2 Answers2

1

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.

-2

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
                    );
                }

}

jrey
  • 2,163
  • 1
  • 32
  • 48
  • 1
    Thanks for your answer. Nonetheless, You suggest spring boot approach but in my scenario, I should use Quartz mechanism. If I will not find out the Quartz JUnit example I will be forced to use above Spring Boot mechanism but till this moment I will try to find out the most valuable solution. Kudos – Martin Nov 05 '19 at 15:10
  • 1
    after research I decided to use your solution. There is no need to use an additional tool which is available from spring boot ;) – Martin Nov 07 '19 at 10:22
  • 3
    how is this even an answer? The question is how to test the Quartz scheduler not configuring a scheduler using spring boot. – Niamatullah Bakhshi Sep 05 '22 at 06:32