0

I made my job class to consume data from DB. In addition to this I made the schedule class with interval 1 minutes for test. However the job is not triggering

@Component
@RequiredArgsConstructor
public class OfJob implements Job {

    private final OfRepository ofRepository;

    private final OfService ofService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        List<OfDomain> ofacList = ofRepository.findByPending(true);

        ofList.forEach(ofDomain -> {

            ofService.evaluate(ofDomain);

        });
    }
}

Config:

@Component
public class QuartsScheduleConfiguration {
    @Bean
    public Scheduler scheduler() throws SchedulerException {

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail jobDetail = JobBuilder.newJob(OfJob.class).build();
        SimpleTrigger simpleTrigger = newTrigger()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatForever().withIntervalInMinutes(1))
                .build();


        scheduler.scheduleJob(jobDetail, simpleTrigger);
        scheduler.start();
        return scheduler;
    }
}

I have not error on my logs

Help me please

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
  • Is there anything during application startup that is referencing the scheduler bean through autowiring? If you only declare it, but not use it then Spring doesn't have a reason to initialize it. I think you need to create an application start or context refresh listener that gets the scheduler as autowired argument. And from that listener method call the start method of the scheduler. Conversely, also set up an application stop listener and make sure to shut down the scheduler there – tiguchi Mar 06 '20 at 20:24
  • @tiguchi I use Spring Boot, should I configure ``QuartsScheduleConfiguration`` with it? do you have some example to help me? – TuGordoBello Mar 06 '20 at 20:34
  • I'm not familiar with `QuartzScheduleConfiguration`....Just pointing out a conceptual problem with your code. By the look of it nothing is ever calling your scheduler setup method. You need to start the scheduler (once) when your application starts. Look into application context events. Here's an example on SO that might be helpful. You set up a component with an event listener method that registers with the context refreshed event: https://stackoverflow.com/a/33370231/1473663 – tiguchi Mar 06 '20 at 20:42
  • Create a context refresh event listener method like in in the linked example, but add your scheduler as autowired argument: ``` @EventListener({ContextRefreshedEvent.class}) public void onContextRefresh(@Autowired Scheduler scheduler) { scheduler.start(); } ``` – tiguchi Mar 06 '20 at 20:46
  • @tiguchi Not working – TuGordoBello Mar 06 '20 at 20:59
  • Well, then my best guess is that Spring is not set up properly to auto-discover your components and configurations. You should update your question with your application configuration. Are you using something like a `beans.xml` file? Or do you set up Spring in code? Add that to your question. – tiguchi Mar 06 '20 at 21:04
  • Also: what version of Spring Boot are you using? – tiguchi Mar 06 '20 at 21:04
  • @tiguchi Spring Boot v2, I am not using beans.xml – TuGordoBello Mar 06 '20 at 21:14
  • 1
    `@Component` is no(t) "Config"! (the other way around, the statement holds:) – xerx593 Mar 06 '20 at 21:15
  • 1
    ..maybe that's the only issue: replace `@Component` by `@Configuration` !? – xerx593 Mar 06 '20 at 21:20
  • @xerx593 I replaced it Component to Configuration and the job is still not trigger, however the my quartzConfig is does – TuGordoBello Mar 09 '20 at 13:25

0 Answers0