7
由於「只有void-returning方法可能註解爲@Scheduled
」,因此當我使用@Bean
配置而不是xml配置時,如何使用Spring Batch和Spring Scheduler Task?您可以在下面找到我的完整配置文件。當我從main()
觸發,但只有一次,它運行完美。我想添加@Scheduled(fixedrate=9999)
以便以特定頻率喚起相同的工作。據我所知,爲了做到這一點,我期望在step1方法的基礎上增加@Scheduled
,但是我不能這樣做,因爲它與void的返回值不同。如何將@Configuration和@EnableScheduling與Spring批處理一起使用
@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguration {
private static final Logger log = LoggerFactory
.getLogger(BatchConfiguration.class);
@Bean
@StepScope
public FlatFileItemReader<Person> reader() {
log.info(new Date().toString());
FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
reader.setResource(new ClassPathResource("test_person_json.js"));
reader.setLineMapper(new DefaultLineMapper<Person>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[] {"firstName", "lastName" });
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
{
setTargetType(Person.class);
}
});
}
});
return reader;
}
@Bean
public ItemProcessor<Person, Person> processor() {
return new PersonItemProcessor();
}
@Bean
public ItemWriter<Person> writer(DataSource dataSource) {
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
writer.setDataSource(dataSource);
return writer;
}
@Bean
public Job importUserJob(JobBuilderFactory jobs, Step s1,
JobExecutionListener listener) {
return jobs.get("importUserJob").incrementer(new RunIdIncrementer())
.listener(listener).flow(s1).end().build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory,
ItemReader<Person> reader, ItemWriter<Person> writer,
ItemProcessor<Person, Person> processor) {
return stepBuilderFactory.get("step1").<Person, Person> chunk(10)
.reader(reader).processor(processor).writer(writer).build();
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
//Question updated on Dec 3th 2015 with first suggestion
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class PersonScheduler {
private Job myImportJob;
private JobLauncher jobLauncher;
@Autowired
public PersonScheduler(JobLauncher jobLauncher, @Qualifier("myImportJob") Job myImportJob){
this.myImportJob = myImportJob;
this.jobLauncher = jobLauncher;
}
@Scheduled(fixedRate=9999)
public void runJob{
jobLauncher.run(myImportJob, new JobParameters());
}
}
我得到兩個警告: 1 - (右上@Scheduled)註釋@Scheduled不允許這個位置 2 - (右上法runJob)在該行 多個標記 - 語法錯誤,插入「;」完成 FieldDeclaration - void是變量的無效類型 runJob 我更新了我的問題並添加了您的建議。我展示了進口以及通常當我們沒有正確的進口時出現這種警告,但我想我是正確的。我應該添加@SuppressWarnings嗎? –
我爲runJob()方法提供了括號。現在更新了。 – luboskrnac
我可以看到scheduller正在工作,但我得到 SimpleStepHandler:步驟已經完成或不能重新啓動,因此沒有要執行的操作:StepExecution:id = 1,version = 3,name = step1,status = COMPLETED,exitStatus = COMPLETED, readCount = 1,filterCount = 0,writeCount = 1 readSkipCount = 0,writeSkipCount = 0,processSkipCount = 0,commitCount = 1,rollbackCount = 0,exitDescription =。在我看來,我的工作跑了兩次之後,就不能再跑了。我想,第一次是在我啓動應用程序時運行,然後是第二次作爲scheduller觸發。在此之後,狀態將永遠「完成」。 –