我如何使春季调度定期调用具有单个任务包的工作



使用注释@Scheduled(fixedRate = 600000),我希望每 10 分钟触发一次作业,因此也触发任务(600000 毫秒 = 600 秒 = 10 分钟)。首先,我尝试使用 return RepeatStatus.FINISHED,因为我知道 spring 调度程序会每 10 分钟触发一个独立的线程。实际上,如果我使用return RepeatStatus.FINISHED,它完全完成了程序,换句话说,spring调度器不会再次调用该作业。我不确定我是否在 Spring 调度程序中设置了错误,或者我对 tasklet 有一些错误的概念。根据经验,根据我最近所做的事情,当我不需要阅读器和作家方法时,tasklet 是一个可能的替代方案。我想创建一个批处理过程,该进程每十分钟将文件从一个文件夹移动到另一个文件夹。不会有文件进程。从控制台日志中,我可以看到TestScheduller.runJob在我运行时被唤起CommandLineJobRunner一次。然后,作为我的第一个调查测试,我改为return RepeatStatus.CONTINUABLE,之后,我注意到任务确实运行了无限时间,但不是 10 分钟,而是每 1 秒。当然,这是不正确的。此外,这项工作根本没有完成。所以,我的问题是:我怎样才能让 spring.sedulling 每十分钟唤起以下工作?

为了每 10 分钟触发一次 tasklet,创建的调度程序:

@Component
public class TestScheduller {
       private Job job;
       private JobLauncher jobLauncher;
       @Autowired
       public TestScheduller(JobLauncher jobLauncher,
                     @Qualifier("helloWorldJob") Job job) {
              this.job = job;
              this.jobLauncher = jobLauncher;
       }
       @Scheduled(fixedRate = 600000) 
       public void runJob() {
              try {
                     System.out.println("runJob");
                     JobParameters jobParameters = new JobParametersBuilder().addLong(
                                  "time", System.currentTimeMillis()).toJobParameters();
                     jobLauncher.run(job, jobParameters);
              } catch (Exception ex) {
                     System.out.println("runJob exception ***********");
              }
       }

Java 配置类

@Configuration
@ComponentScan("com.test.config")
@EnableScheduling
@Import(StandaloneInfrastructureConfiguration.class)
public class HelloWorldJobConfig {
       @Autowired
       private JobBuilderFactory jobBuilders;
       @Autowired
       private StepBuilderFactory stepBuilders;
       @Autowired
       private InfrastructureConfiguration infrastructureConfiguration;
       @Autowired
       private DataSource dataSource; // just for show...
       @Bean
       public Job helloWorldJob(){
              return jobBuilders.get("helloWorldJob")
                           .start(step())
                           .build();

       }
       @Bean
       public Step step(){
              return stepBuilders.get("step")
                           .tasklet(tasklet())
                           .build();
       }
       @Bean
       public Tasklet tasklet() {
              return new HelloWorldTasklet();
       }
}

任务: public class HelloWorldTasklet 实现 Tasklet {

    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
            throws Exception {
        System.out.println("HelloWorldTasklet.execute called");
        return RepeatStatus.CONTINUABLE;
    }
}

控制台日志:

2016-01-18 14:16:16,376 INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@dcf3e99: startup date [Mon Jan 18 14:16:16 CST 2016]; root of context hierarchy
2016-01-18 14:16:16,985 WARN  org.springframework.context.annotation.ConfigurationClassEnhancer - @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
2016-01-18 14:16:17,024 WARN  org.springframework.context.annotation.ConfigurationClassEnhancer - @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
2016-01-18 14:16:17,091 INFO  org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.scheduling.annotation.SchedulingConfiguration' of type [class org.springframework.scheduling.annotation.SchedulingConfiguration$$EnhancerBySpringCGLIB$$e07fa052] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-01-18 14:16:17,257 INFO  org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory - Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa'
2016-01-18 14:16:17,425 INFO  org.springframework.jdbc.datasource.init.ScriptUtils - Executing SQL script from class path resource [org/springframework/batch/core/schema-drop-hsqldb.sql]
2016-01-18 14:16:17,430 INFO  org.springframework.jdbc.datasource.init.ScriptUtils - Executed SQL script from class path resource [org/springframework/batch/core/schema-drop-hsqldb.sql] in 5 ms.
2016-01-18 14:16:17,430 INFO  org.springframework.jdbc.datasource.init.ScriptUtils - Executing SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql]
2016-01-18 14:16:17,456 INFO  org.springframework.jdbc.datasource.init.ScriptUtils - Executed SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql] in 25 ms.
runJob
2016-01-18 14:16:18,083 INFO  org.springframework.batch.core.repository.support.JobRepositoryFactoryBean - No database type set, using meta data indicating: HSQL
2016-01-18 14:16:18,103 INFO  org.springframework.batch.core.repository.support.JobRepositoryFactoryBean - No database type set, using meta data indicating: HSQL
2016-01-18 14:16:18,448 INFO  org.springframework.batch.core.launch.support.SimpleJobLauncher - No TaskExecutor has been set, defaulting to synchronous executor.
2016-01-18 14:16:18,454 INFO  org.springframework.batch.core.launch.support.SimpleJobLauncher - No TaskExecutor has been set, defaulting to synchronous executor.
2016-01-18 14:16:18,558 INFO  org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=helloWorldJob]] launched with the following parameters: [{time=1453148177985}]
2016-01-18 14:16:18,591 INFO  org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=helloWorldJob]] launched with the following parameters: [{}]
2016-01-18 14:16:18,613 INFO  org.springframework.batch.core.job.SimpleStepHandler - Executing step: [step]
HelloWorldTasklet.execute called
2016-01-18 14:16:18,661 INFO  org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
2016-01-18 14:16:18,661 INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@dcf3e99: startup date [Mon Jan 18 14:16:16 CST 2016]; root of context hierarchy
2016-01-18 14:16:18,665 INFO  org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory - Shutting down embedded database: url='jdbc:hsqldb:mem:testdb'
2016-01-18 14:16:18,844 INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:C:PROGRA~2HPQUICKT~1binJAVA_S~1classes;C:PROGRA~2HPQUICKT~1binJAVA_S~1classesjasmine.jar

你需要调用 TaskletStep 的 setAllowStartIfComplete(true) 方法。所以而不是有这样的方法

@Bean
public Step step(){
    return stepBuilders.get("step")
                 .tasklet(tasklet())
                 .build();
}

它应该看起来像:

@Bean
public Step step(){
    TaskletStep step = stepBuilders.get("step")
                 .tasklet(tasklet())
                 .build();
    step.setAllowSta
}

最新更新