提问者:小点点

Spring批处理未运行ItemProcter和ItemWriter


运行作业时,我在Spring批次中遇到问题。我已经在3个不同的文件中配置了我的ItemReader,ItemProcessor,ItemWriter,然后尝试在步骤中运行它们,但是在ItemReader之后什么也没有执行。下面是我所有的文件。

package com.example.demo.config;

import com.example.demo.listners.FirstListners;
import com.example.demo.processors.FirstItemProcessor;
import com.example.demo.readers.FirstItemReader;
import com.example.demo.service.SecondTasklet;
import com.example.demo.writers.FirstItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SampleJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private FirstItemReader firstItemReader;

    @Autowired
    private FirstItemProcessor firstItemProcessor;

    @Autowired
    private FirstItemWriter firstItemWriter;

    @Bean
    public Job secondJob() throws Exception {
        return jobBuilderFactory.get("second Job")
                .incrementer(new RunIdIncrementer())
                .start(firstChunkStep())
                .build();
    }


    @Bean
    public Step firstChunkStep() throws Exception {

               return stepBuilderFactory.get("first Chunk Step")
                .<Integer,Long>chunk(3)
                .reader(firstItemReader)
                .processor(firstItemProcessor)
                .writer(firstItemWriter)
                .build();
    }


}

package com.example.demo.readers;

import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@StepScope
public class FirstItemReader implements ItemReader<Integer> {

    List<Integer> listOfItems = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

    int i = 0;

    @Override
    public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        System.out.println("Inside Item reader");
        Integer item;

        if(i < listOfItems.size()){
            item = listOfItems.get(i);
            i++;
        }
       // i = 0;
        return null;
    }
}

import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

@Component
@StepScope
public class FirstItemProcessor implements ItemProcessor<Integer, Long> {
    @Override
    public Long process(Integer item) throws Exception {
        System.out.println("Inside Item Processor");
        return Long.valueOf(item + 20);
    }
}

import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@StepScope
public class FirstItemWriter implements ItemWriter<Long> {
    @Override
    public void write(List<? extends Long> list) throws Exception {
        System.out.println("Inside writer");
        list.stream().forEach(System.out::println);
    }
};

由于区块大小为3,它应该从读取器运行并处理列表中的3个项目,但在运行后,只运行一次ItemReader,然后什么都不会发生

控制台日志:

2022-09-03 16:47:45.972  INFO 9836 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-09-03 16:47:46.625  INFO 9836 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-09-03 16:47:46.727  INFO 9836 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: MYSQL
2022-09-03 16:47:47.072  INFO 9836 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2022-09-03 16:47:47.204  INFO 9836 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.908 seconds (JVM running for 3.501)
2022-09-03 16:47:47.206  INFO 9836 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: [run=a]
2022-09-03 16:47:47.408  INFO 9836 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=second Job]] launched with the following parameters: [{run.id=18, run=a}]
2022-09-03 16:47:47.476  INFO 9836 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [first Chunk Step]
Inside Item reader
2022-09-03 16:47:47.527  INFO 9836 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [first Chunk Step] executed in 49ms
2022-09-03 16:47:47.550  INFO 9836 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=second Job]] completed with the following parameters: [{run.id=18, run=a}] and the following status: [COMPLETED] in 96ms
2022-09-03 16:47:47.556  INFO 9836 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-09-03 16:47:47.570  INFO 9836 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

共1个答案

匿名用户

谢谢,已经解决了。我在ItemReader的if case中添加了return语句,它如我预期的那样工作得很好。