我是初次批处理的新手,我遇到了一个场景,我在可跳过的异常中声明了一个异常,跳过在项目处理器中检测到的无效记录。你能否帮助我一些可能的方法,通过这种方式我可以在整个作业运行期间跟踪所有跳过的记录,最后使用作业执行监听器或其他方式发送所有跳过的记录的邮件。 任何建议都会有所帮助。
弹簧context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<import resource="classpath:context-datasource.xml" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>springbatch.properties</value>
</property>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<!-- ItemReader which reads from database and returns the row mapped by
rowMapper -->
<bean id="databaseItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="SELECT * FROM employee" />
<property name="rowMapper">
<bean class="com.sample.springbatch.jdbc.EmployeeRowMapper" />
</property>
</bean>
<!-- ItemWriter writes a line into output flat file -->
<bean id="databaseItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
<value>
<![CDATA[
insert into actemployee(empId, firstName, lastName,additionalInfo)
values (?, ?, ?, ?)
]]>
</value>
</property>
<property name="itemPreparedStatementSetter">
<bean class="com.sample.springbatch.jdbc.EmployeePreparedStatementSetter" />
</property>
</bean>
<!-- Optional ItemProcessor to perform business logic/filtering on the input
records -->
<bean id="itemProcessor" class="com.sample.springbatch.EmployeeItemProcessor">
<property name="validator" ref="validator" />
</bean>
<!-- Step will need a transaction manager -->
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="recordSkipListener" class="com.sample.springbatch.RecordSkipListener" />
<!-- Actual Job -->
<batch:job id="employeeToActiveEmployee">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="databaseItemReader" writer="databaseItemWriter"
processor="itemProcessor" commit-interval="10" skip-limit="500">
<batch:skippable-exception-classes>
<batch:include
class="javax.validation.ValidationException" />
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="recordSkipListener" />
</batch:listeners>
</batch:step>
</batch:job>
<!-- Email API bean configuarion -->
</beans>
Processor.java
public class EmployeeItemProcessor implements ItemProcessor<Employee, ActiveEmployee> {
private Validator validator;
public Validator getValidator() {
return validator;
}
public void setValidator(Validator validator) {
this.validator = validator;
}
@Override
public ActiveEmployee process(Employee employee) throws Exception {
BindingResult results = BindAndValidate(employee);
if (results.hasErrors()) {
buildValidationException(results);
}
System.out.println("Processing employee object:" + employee);
ActiveEmployee activeEmployee = new ActiveEmployee(employee.getEmpId(), employee.getFirstName(),
employee.getLastName(), "This is additional info");
return activeEmployee;
}
private BindingResult BindAndValidate(Employee employee) {
DataBinder binder = new DataBinder(employee);
binder.setValidator(validator);
binder.validate();
return binder.getBindingResult();
}
private void buildValidationException(BindingResult results) {
StringBuilder msg = new StringBuilder();
for (ObjectError error : results.getAllErrors()) {
msg.append("-*-*-*- \n" + error.toString() + "-*-*-*- \n");
}
throw new ValidationException(msg.toString());
}
}
谢谢!
答案 0 :(得分:0)
我会使用SkipListener
将跳过的记录写入文件,然后使用StepExecutionListener
在步骤完成后发送包含该文件的电子邮件。