0

I have created a Spring Boot application wherein main class I am creating a scheduler object.

prop.put("quartz.scheduler.instanceName", "ServerScheduler");
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
        prop.put("org.quartz.scheduler.instanceId", "CLUSTERED");
        prop.put("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
        prop.put("org.quartz.jobStore.tablePrefix", "H1585.QRTZ_");
        prop.put("org.quartz.jobStore.isClustered", "false");
        prop.put("org.quartz.scheduler.misfirePolicy", "doNothing");

        prop.put("org.quartz.dataSource.quartzDataSource.driver", "com.ibm.db2.jcc.DB2Driver");
        prop.put("org.quartz.dataSource.quartzDataSource.URL", url);
        prop.put("org.quartz.dataSource.quartzDataSource.user", user);
        prop.put("org.quartz.dataSource.quartzDataSource.password", passwrd);
        prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "2");

        SpringApplication.run(SchedulerApplication.class, args);

        try {

            SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(prop);
            Scheduler scheduler = stdSchedulerFactory.getScheduler();
            scheduler.start();

I want to use the same scheduler object in my service class to trigger the job. The one which I am getting using below code is not working showing different instance id.

scheduler = StdSchedulerFactory.getDefaultScheduler();

How can I solve this?

1 Answer 1

1

you can create a singleton Scheduler, and autowired in your service class

@SpringBootApplication
public class SchedulerApplication {

    public static void main(final String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Bean
    public Scheduler scheduler() {
        //create props as you above code
        SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(prop);
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        scheduler.start();
        return scheduler;
    }
}

then you can use in your service class

@Service
public class YourServiceClass {
    @Autowired
    private Scheduler scheduler;
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.