Первая проблема, когда я печатаю значение параметра «ErrorPathFile» задания, оно равно нулю.
@Component
@Scope(value = "step")
public class EmployeeFieldSetMapper implements FieldSetMapper<Employee> {
@Value("#{jobParameters['errorFilePath']}")
private String filePath; //="C:\\Users\\prajjwal\\Documents\\errorReports.csv";
@Override
public Employee mapFieldSet(FieldSet fieldSet) throws BindException {
final Employee employee = new Employee();
employee.setEmployeeId(fieldSet.readLong("employeeId"));
employee.setEmployeeName(fieldSet.readString("employeeName"));
employee.setAge(fieldSet.readInt("age"));
employee.setPincode(fieldSet.readInt("pincode"));
if(fieldSet.readInt("age")==4) {
try {
System.out.println("errorFilePath"+filePath);
FileWriter skipReport= new FileWriter(filePath, true);
skipReport.append("fromRead,age is 4");
skipReport.close();
}
catch(IOException e) {
throw new RuntimeException();
}
throw new MyException("age is incorrect"+fieldSet.readString("age"));
}
return employee;
}
}
Вот как я вводю значение:
@RestController
@RequestMapping("/api")
public class EmployeeResource {
@Autowired
EmployeeService employeeService;
@Value("${errorFilePath}")
private String errorFilePath;
@Autowired
Job job;
@Autowired
JobLauncher joblauncher;
@GetMapping("/batch")
public ResponseEntity<?> luanch(@RequestParam("file-path") String filePath)
throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
JobParametersInvalidException {
return ResponseEntity.ok(joblauncher.run(job, new JobParametersBuilder().addDate("date", new Date())
.addString("filePath", filePath).addString("errorFilePath", errorFilePath).toJobParameters()).getStatus());
}
}
Второе проблема в том, что SkipListener
не вызывается при пропуске:
@Component
@StepScope
public class MySkipListener implements SkipListener<Employee, Employee> {
@Value("#{jobParameters['errorFilePath']}")
private String filePath;//="abc";
@Override
//@OnSkipInRead
public void onSkipInRead(Throwable t) {
if(t instanceof MyException) {
MyException m= (MyException) t;
try {
System.out.println("called");
FileWriter skipReport= new FileWriter(filePath, true);
skipReport.append("fromRead,"+m.getMessage());
skipReport.close();
} catch (IOException e) {
throw new MyException("IOException occured");
}
}
}
@Override
//@OnSkipInWrite
public void onSkipInWrite(Employee item, Throwable t) {
if(t instanceof MyException) {
MyException m= (MyException) t;
try {
System.out.println("called");
FileWriter skipReport= new FileWriter(filePath, true);
skipReport.append("fromWrite,"+m.getMessage());
skipReport.close();
} catch (IOException e) {
throw new MyException("IOException occured");
}
}
}
@Override
//@OnSkipInProcess
public void onSkipInProcess(Employee item, Throwable t) {
System.out.println("innnn");
if(t instanceof MyException) {
MyException m= (MyException) t;
try {
System.out.println("called");
FileWriter skipReport= new FileWriter(filePath, true);
skipReport.append("fromProcess,"+m.getMessage());
skipReport.close();
} catch (IOException e) {
throw new MyException("IOException occured");
}
}
}
}
Вот файл конфигурации:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
@StepScope
public FlatFileItemReader<Employee> reader(@Value ("#{jobParameters[filePath]}") String filePath) {
return new FlatFileItemReaderBuilder<Employee>().name("EmployeeReader")
.resource(new FileSystemResource(new File(filePath))).delimited()
.names(new String[] { "employeeId", "employeeName", "age", "pincode"}).lineMapper(lineMapper())
.fieldSetMapper(new BeanWrapperFieldSetMapper<Employee>() {
{
setTargetType(Employee.class);
}
}).build();
}
@Bean
public LineMapper<Employee> lineMapper() {
final DefaultLineMapper<Employee> defaultLineMapper = new DefaultLineMapper<>();
final DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setDelimiter(",");
lineTokenizer.setStrict(false);
lineTokenizer.setNames(new String[] { "employeeId", "employeeName", "age", "pincode"});
final EmployeeFieldSetMapper fieldSetMapper = new EmployeeFieldSetMapper();
defaultLineMapper.setLineTokenizer(lineTokenizer);
defaultLineMapper.setFieldSetMapper(fieldSetMapper);
return defaultLineMapper;
}
@Bean
public EmployeeProcessor employeeProcessor() {
return new EmployeeProcessor();
}
@Bean
public JdbcBatchItemWriter<Employee> writer(final DataSource dataSource) {
return new JdbcBatchItemWriterBuilder<Employee>()
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.sql("INSERT INTO employee (employee_id, employee_name,age, pincode) VALUES (:employeeId, :employeeName ,:age, :pincode)")
.dataSource(dataSource).build();
}
@Bean
public Job importEmployeeJob(NotificationListener listener, Step step1) {
return jobBuilderFactory.get("importEmployeeJob").incrementer(new RunIdIncrementer()).listener(listener)
.flow(step1).end().build();
}
@Bean
public Step step1(JdbcBatchItemWriter<Employee> writer) {
return stepBuilderFactory.get("step1")
.<Employee, Employee>chunk(10)
.reader(reader(null)).faultTolerant().skip(MyException.class).skipLimit(Integer.MAX_VALUE).skipPolicy(new FileVerificationSkipper())
.processor(employeeProcessor()).writer(writer)
.listener( new MySkipListener()).build();
}
@Bean
public ErrorTracker getErrorTracker() {
return new ErrorTracker();
}
}
Пожалуйста, предложите, что я делаю неправильно.
Обновление: Первая проблема решена, поскольку я удалил @Component
аннотации, где бы я ни использовал @StepScop
. Но вторая проблема:
SkipListener не вызывается
не решена