Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何在服务类中使用org.quartz.Scheduler对象_Spring_Spring Boot_Quartz Scheduler - Fatal编程技术网

Spring 如何在服务类中使用org.quartz.Scheduler对象

Spring 如何在服务类中使用org.quartz.Scheduler对象,spring,spring-boot,quartz-scheduler,Spring,Spring Boot,Quartz Scheduler,我已经创建了一个Spring引导应用程序,我正在其中创建一个调度程序对象 prop.put("quartz.scheduler.instanceName", "ServerScheduler"); prop.put("org.quartz.scheduler.instanceId", "AUTO"); prop.put("org.quartz.scheduler.skipUpdateCheck", "true"); prop.put("org.qu

我已经创建了一个Spring引导应用程序,我正在其中创建一个调度程序对象

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

如何解决此问题?

您可以创建singleton
调度程序,并在服务类中自动连接

@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;
}
然后您可以在服务类中使用

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