我不知道为什么skiplistener不打印简单的日志。我想把跳过的记录记录到一个单独的日志文件中,我想我可以使用mdc.put()。但我甚至不能打印一个登录控制台不知道发生了什么。我想我遗漏了什么。如果你能帮忙,我将不胜感激。我甚至尝试使用泛型exception.class进行测试,但仍然没有做任何事情。这是我的密码;
BTache配置
@EnableBatchProcessing
public class BatchConfiguration {
static final Logger LOG = LogManager.getLogger(BatchConfiguration.class);
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
@Bean
public Job loadUserJob() {
return jobBuilderFactory.get("loadUserJob")
.incrementer(new RunIdIncrementer())
.start(loadUsersStep())
.listener(new JobLoggerListener())
.build();
}
@Bean
public Step loadUsersStep() {
return stepBuilderFactory.get("loadUsersStep")
.<UserInfo, UserInfoDTO> chunk(10)
.reader(reader())
.processor(UserInfoItemProcessor())
.writer(writer())
.faultTolerant()
.skipLimit(3)
.skip(Exception.class)
//.skip(UserNotFoundException.class)
.listener(new StepStartStopListener())
.listener(new LoadDataSkipListener())
.build();
}
@Bean
public FlatFileItemReader<UserInfo> reader() {
return new FlatFileItemReaderBuilder<UserInfo>()
.name("reader")
.delimited()
.names(new String[] {
"first_name",
"last_name",
"email"})
.targetType(UserInfo.class)
.resource(new ClassPathResource("userinfo_file.csv"))
.build();
}
@Bean
public UserInfoItemProcessor UserInfoItemProcessor() {
return new UserInfoItemProcessor();
}
@Bean
public JdbcBatchItemWriter<UserInfoDTO> writer() {
JdbcBatchItemWriter<UserInfoDTO> writer = new JdbcBatchItemWriter<UserInfoDTO>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
writer.setSql("INSERT INTO db1.userinfo "
+ "(first_name,last_name,email) " +
"VALUES (:firstName, :lastName,:email)");
writer.setDataSource(dataSource);
return writer;
}
}
skiplistener类
public class LoadDataSkipListener implements SkipListener<UserInfoDTO, UserInfoDTO>{
public static final Logger LOG = LogManager.getLogger(LoadDataSkipListener.class);
@Override
public void onSkipInRead(Throwable t) {
LOG.info("ItemWriter: ");
}
@Override
public void onSkipInWrite(UserInfoDTO item, Throwable t) {
LOG.info(">>> onSkipInWrite <<<");
// MDC.put("skipped_users", String.valueOf(item.getUserId())); // I want to log skipped user ids on write
}
@Override
public void onSkipInProcess(UserInfoDTO item, Throwable t) {
LOG.info(">>> onSkipInProcess<<< ");
}
}
应用程序属性
spring.datasource.url=jdbc:mysql://localhost:3306/db1?useSSL=false
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
spring.batch.initialize-schema=always
spring.datasource.initialize=true
# logging.file={log-name}.log --> WIll this write into a new file ??
只是想知道
为什么它甚至不在控制台中打印?
可以使用注解的mdc写入新文件吗?
提前谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!