spring bootで以下のサンプルを用いてスケジューリングを行いたいのですが、
調べても方法がわかりません。
詳しい方ご教示願えませんか?
追記
スケジューリングとは1分間に一回など複数回実行することです。
java
1package com.michaelcgood; 2 3import javax.sql.DataSource; 4 5import org.springframework.batch.core.Job; 6import org.springframework.batch.core.Step; 7import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 8import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 9import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 10import org.springframework.batch.core.launch.support.RunIdIncrementer; 11import org.springframework.batch.item.ItemProcessor; 12import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider; 13import org.springframework.batch.item.database.JdbcBatchItemWriter; 14import org.springframework.batch.item.file.FlatFileItemReader; 15import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; 16import org.springframework.batch.item.file.mapping.DefaultLineMapper; 17import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; 18import org.springframework.beans.factory.annotation.Autowired; 19import org.springframework.context.annotation.Bean; 20import org.springframework.context.annotation.Configuration; 21import org.springframework.core.io.ClassPathResource; 22 23@EnableBatchProcessing 24@Configuration 25public class CsvFileToDatabaseConfig { 26 27 @Autowired 28 public JobBuilderFactory jobBuilderFactory; 29 30 @Autowired 31 public StepBuilderFactory stepBuilderFactory; 32 33 @Autowired 34 public DataSource dataSource; 35 36 // begin reader, writer, and processor 37 38 39 @Bean 40 public FlatFileItemReader<AnimeDTO> csvAnimeReader(){ 41 FlatFileItemReader<AnimeDTO> reader = new FlatFileItemReader<AnimeDTO>(); 42 reader.setResource(new ClassPathResource("animescsv.csv")); 43 reader.setLineMapper(new DefaultLineMapper<AnimeDTO>() {{ 44 setLineTokenizer(new DelimitedLineTokenizer() {{ 45 setNames(new String[] { "id", "title", "description" }); 46 }}); 47 setFieldSetMapper(new BeanWrapperFieldSetMapper<AnimeDTO>() {{ 48 setTargetType(AnimeDTO.class); 49 }}); 50 }}); 51 return reader; 52 } 53 54 55 @Bean 56 ItemProcessor<AnimeDTO, AnimeDTO> csvAnimeProcessor() { 57 return new AnimeProcessor(); 58 } 59 60 @Bean 61 public JdbcBatchItemWriter<AnimeDTO> csvAnimeWriter() { 62 JdbcBatchItemWriter<AnimeDTO> csvAnimeWriter = new JdbcBatchItemWriter<AnimeDTO>(); 63 csvAnimeWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<AnimeDTO>()); 64 csvAnimeWriter.setSql("INSERT INTO animes (id, title, description) VALUES (:id, :title, :description)"); 65 csvAnimeWriter.setDataSource(dataSource); 66 return csvAnimeWriter; 67 } 68 69 // end reader, writer, and processor 70 71 // begin job info 72 @Bean 73 public Step csvFileToDatabaseStep() { 74 return stepBuilderFactory.get("csvFileToDatabaseStep") 75 .<AnimeDTO, AnimeDTO>chunk(1) 76 .reader(csvAnimeReader()) 77 .processor(csvAnimeProcessor()) 78 .writer(csvAnimeWriter()) 79 .build(); 80 } 81 82 @Bean 83 Job csvFileToDatabaseJob(JobCompletionNotificationListener listener) { 84 return jobBuilderFactory.get("csvFileToDatabaseJob") 85 .incrementer(new RunIdIncrementer()) 86 .listener(listener) 87 .flow(csvFileToDatabaseStep()) 88 .end() 89 .build(); 90 } 91 // end job info 92}
java
1package com.michaelcgood; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class SpringBatchCsvApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(SpringBatchCsvApplication.class, args); 11 } 12} 13
追記
以下を参考にバッチを作り直しました。
http://blogs.innovationm.com/job-scheduling-with-spring-batch/
java
1 2import org.springframework.batch.core.launch.support.SimpleJobLauncher; 3import org.springframework.batch.core.repository.JobRepository; 4import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; 5import org.springframework.batch.support.transaction.ResourcelessTransactionManager; 6import org.springframework.context.annotation.Bean; 7import org.springframework.context.annotation.Configuration; 8import org.springframework.scheduling.annotation.EnableScheduling; 9 10@Configuration 11@EnableScheduling 12public class BatchScheduler { 13@Bean 14public ResourcelessTransactionManager transactionManager() { 15return new ResourcelessTransactionManager(); 16} 17 18@Bean 19public MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean(ResourcelessTransactionManager resourcelessTransactionManager) throws Exception { 20MapJobRepositoryFactoryBean factoryBean = new MapJobRepositoryFactoryBean(resourcelessTransactionManager); 21factoryBean.afterPropertiesSet(); 22return factoryBean; 23} 24 25@Bean 26public JobRepository jobRepository(MapJobRepositoryFactoryBean factoryBean) throws Exception{ 27return (JobRepository) factoryBean.getObject(); 28} 29 30@Bean 31public SimpleJobLauncher jobLauncher(JobRepository jobRepository) { 32SimpleJobLauncher launcher = new SimpleJobLauncher(); 33launcher.setJobRepository(jobRepository); 34return launcher; 35} 36 37} 38
すると以下のエラーが発生します。何か設定が必要なのでしょうか?
Description:
Parameter 0 of method mapJobRepositoryFactoryBean in position.BatchScheduler required a bean of type 'org.springframework.batch.support.transaction.ResourcelessTransactionManager' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.batch.support.transaction.ResourcelessTransactionManager' in your configuration.
追記
BatchSchedulerクラスの@configurationを除去するとエラーはなくなりました。
しかし一度実行しただけで終了されます。方法が間違っているのでしょうか?
回答1件
あなたの回答
tips
プレビュー