如何在Service类中使用org.quartz.Scheduler对象

问题描述 投票:0回答:1

我创建了一个spring boot应用程序,其中主类我正在创建一个调度程序对象。

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

我想在我的服务类中使用相同的调度程序对象来触发作业。我在代码下面使用的那个不能显示不同的实例ID。

scheduler = StdSchedulerFactory.getDefaultScheduler();

请建议如何解决。提前致谢!

spring spring-boot quartz-scheduler
1个回答
1
投票

你可以创建一个单独的Scheduler,并在你的服务类中自动装配

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

然后你可以在你的服务类中使用

@Service
public class YourServiceClass {
    @Autowired
    private Scheduler scheduler;
}
© www.soinside.com 2019 - 2024. All rights reserved.