spring批处理异常java.lang.IllegalStateException:必须提供事务管理器

问题描述 投票:0回答:1
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'runjob' defined in class path resource [com/example/inseespringbatch/BatchConfiguration.class]: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'runjob' threw exception with message: Error creating bean with name 'step1' defined in class path resource [com/example/inseespringbatch/BatchConfiguration.class]: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'step1' threw exception with message: java.lang.IllegalStateException: A transaction manager must be provided

我正在尝试通过一个小项目开始使用 Spring Batch,其想法是使用 itemReader 读取 txt 格式的文档,通过使用 itemProcessor 更改标头来修改它,然后将其重写到新的 json 文件中。我指定不需要存储在 bdd 中。当我启动我的应用程序时,出现以下错误


@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

  @Bean
  DataSource dataSource() {
    return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
    .addScript("/org/springframework/batch/core/schema-h2.sql").generateUniqueName(
        true).build();
  }

  @Bean
  TransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
  }

  @Bean
  public Job runjob(JobRepository jobRepository) {
    return new JobBuilder("runJob", jobRepository).start(step1(jobRepository)).build();
  }

  @Bean
  public Step step1(JobRepository jobRepository) {
      return new StepBuilder("step1")
          .repository(jobRepository)
          .<Country, Country>chunk(10)
          .reader(reader())
          .processor(processor())
          .writer(writer())
          .build();
  }

  @Bean
  public ItemReader<Country> reader() {
    FlatFileItemReader<Country> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("src/main/resources/documents/Countries.txt"));
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setDelimiter(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    tokenizer.setNames("cog", "actual", "capay", "crpay", "ani", "libcog", "libenr", "ancnom", "codeiso2", "codeiso3", "codenum3");
    reader.setLineMapper(new DefaultLineMapper<Country>() {{
      setLineTokenizer(tokenizer);
      setFieldSetMapper(new BeanWrapperFieldSetMapper<>() {{
        setTargetType(Country.class);
      }});
    }});
    return reader;
  }

  @Bean
  public ItemProcessor<Country, Country> processor() {
    return new CountriesProcessor();
  }

  @Bean
  public ItemWriter<Country> writer() {
    FlatFileItemWriter<Country> writer = new FlatFileItemWriter<>();
    writer.setResource(new FileSystemResource("Countries.json"));
    writer.setLineAggregator(item -> {
      ObjectMapper objectMapper = new ObjectMapper();
      try {
        return objectMapper.writeValueAsString(item);
      }
      catch(Exception e) {
        e.printStackTrace();
        return null;
      }
    });
    return writer;
  }
}

在这里您可以看到我的更改操作处理器

public class CountriesProcessor implements ItemProcessor<Country, Country> {

  @Override
  public Country process(Country item) throws Exception {
    // Modifier les noms d'en-tête
    item.setCodeiso2(item.getInsee()); // Remplacer "codeiso2" par "isee"
    item.setActual(item.getStatus()); // Remplacer "actual" par "status"
    item.setLibcog(item.getLabel()); // Remplacer "libcog" par "label"

    // Si le statut est égal à 1, définir "ok"
    if ("1".equals(item.getStatus())) {
      item.setStatus("ok");
    }

    return item;
  }
}```
spring-boot maven spring-batch transactionmanager spring-batch-tasklet
1个回答
0
投票

您必须将事务管理器传递到您的步骤。
尝试像这样重写你的步骤:

@Bean
    public Step step1(JobRepository jobRepository) {
        return new StepBuilder("step1", jobRepository)
                .<Country, Country>chunk(10, transactionManager())
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
© www.soinside.com 2019 - 2024. All rights reserved.