我通过xml配置了一个SkipListener,如下所示:
<batch:job id="importPersonsJob" job-repository="jobRepository">
<batch:step id="importPersonStep">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="personItemReader" writer="personItemWriter"
commit-interval="5" skip-limit="10">
<batch:skippable-exception-classes>
<batch:include class="java.lang.Throwable" />
</batch:skippable-exception-classes>
</batch:chunk>
<batch:listeners>
<batch:listener ref="skipListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="authenticationJobListener" />
<batch:listener ref="jobListener" />
</batch:listeners>
</batch:job>
我的SkipListener的实现如下所示:
public class SkipListener {
@OnSkipInRead
public void log(final Throwable throwable) throws IOException {
// make something
}
@OnSkipInWrite
public void log(final Object objectToWrite, final Throwable throwable) throws IOException {
// make something
}
// some dependencies
}
我已经问谷歌如何有效地测试这个。可悲的是,它并没有那么有效。也许你可以给我一个简短的提示,告诉我如何实现这个目标。
答案 0 :(得分:1)
我假设你没有在这里进行单元测试。
Spring批处理有一个很好的测试框架。
将以下内容添加到您的pom.xml
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>${spring.batch.version}</version>
<scope>test</scope>
</dependency>
您应该添加测试应用程序上下文以使用JobLauncherTestUtils,例如你的test-context.xml看起来像
<beans>
<bean class="org.springframework.batch.test.JobLauncherTestUtils">
<property name="job" ref="importPersonsJob"/>
</bean>
</beans>
然后使用spring test junit runner并调用JobLauncherTestUtils.launchStep()
@ContextConfiguration(
locations=
{"classpath:META-INF/application-conext","classpath: test-context.xml"}
)
@RunWith(SpringJUnit4ClassRunner.class)
public class StepWithSkipTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testStepWithSkip() throws JobInterruptedException{
JobExecution execution = jobLauncherTestUtils.launchStep("importPersonStep");
}
}
现在测试只会执行测试中的步骤 棘手的部分是将应用程序上下文xml分段,以便它可以测试