重试整个批处理作业 n 次
Retry Whole Batch Job for n times
是否可以重试某项工作 n 次?
public void run() {
String[] springConfig = { "spring/batch/jobs/job-read-files.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("partitionJob");
JobParameters jobParameters = new JobParameters();
for (int i = 0; i < 2; i++) {
try {
JobExecution execution = jobLauncher.run(job,jobParameters);
System.out.println("Exit Status : " + execution.getAllFailureExceptions());
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Done");
}
我试过了,但是由于 spring 批处理存储了作业完成的一些状态,它在第二次和第三次时不起作用。
更新:当我尝试这个时它起作用了
public void run() {
for (int i = 0; i <= 2; i++) {
String[] springConfig = { "spring/batch/jobs/job-read-files.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("partitionJob");
JobParameters jobParameters = new JobParameters();
try {
JobExecution execution = jobLauncher.run(job,jobParameters);
System.out.println("Exit Status : " + execution.getAllFailureExceptions());
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Done");
}
还有比这更好的解决方案吗?
这是我的工作配置
<!-- partitioner job -->
<job id="partitionJob" restartable="true" xmlns="http://www.springframework.org/schema/batch">
<!-- master step, 2 threads (grid-size) -->
<step id="masterStep" next="finalstep">
<partition step="slave" partitioner="rangePartitioner">
<handler grid-size="2" task-executor="taskExecutor" />
</partition>
</step>
<step id="finalstep">
<tasklet>
<chunk reader="dummyReader" writer="spWriter" commit-interval="1" />
</tasklet>
</step>
</job>
<batch:step id="slave">
<tasklet>
<chunk reader="pagingItemReader" writer="dummyWriter"
commit-interval="2" retry-limit="3">
<batch:retryable-exception-classes>
<batch:include class="java.lang.Exception" />
</batch:retryable-exception-classes>
</chunk>
</tasklet>
</batch:step>
Spring 有很好的重试机制,你可以定义 RetryTemplate
,你调用某些部分代码 N
次,你可以定义 RetryCallback
和 RecoveryCallback
这很好。
Spring 批处理实际上在内部使用它作为步骤的重试机制。您可以查看 spring retry documentation and regarding retry on step level this is nice blog post,它解释了 spring 批处理中的跳过和重试机制。
是否可以重试某项工作 n 次?
public void run() {
String[] springConfig = { "spring/batch/jobs/job-read-files.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("partitionJob");
JobParameters jobParameters = new JobParameters();
for (int i = 0; i < 2; i++) {
try {
JobExecution execution = jobLauncher.run(job,jobParameters);
System.out.println("Exit Status : " + execution.getAllFailureExceptions());
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Done");
}
我试过了,但是由于 spring 批处理存储了作业完成的一些状态,它在第二次和第三次时不起作用。
更新:当我尝试这个时它起作用了
public void run() {
for (int i = 0; i <= 2; i++) {
String[] springConfig = { "spring/batch/jobs/job-read-files.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("partitionJob");
JobParameters jobParameters = new JobParameters();
try {
JobExecution execution = jobLauncher.run(job,jobParameters);
System.out.println("Exit Status : " + execution.getAllFailureExceptions());
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("Done");
}
还有比这更好的解决方案吗? 这是我的工作配置
<!-- partitioner job -->
<job id="partitionJob" restartable="true" xmlns="http://www.springframework.org/schema/batch">
<!-- master step, 2 threads (grid-size) -->
<step id="masterStep" next="finalstep">
<partition step="slave" partitioner="rangePartitioner">
<handler grid-size="2" task-executor="taskExecutor" />
</partition>
</step>
<step id="finalstep">
<tasklet>
<chunk reader="dummyReader" writer="spWriter" commit-interval="1" />
</tasklet>
</step>
</job>
<batch:step id="slave">
<tasklet>
<chunk reader="pagingItemReader" writer="dummyWriter"
commit-interval="2" retry-limit="3">
<batch:retryable-exception-classes>
<batch:include class="java.lang.Exception" />
</batch:retryable-exception-classes>
</chunk>
</tasklet>
</batch:step>
Spring 有很好的重试机制,你可以定义 RetryTemplate
,你调用某些部分代码 N
次,你可以定义 RetryCallback
和 RecoveryCallback
这很好。
Spring 批处理实际上在内部使用它作为步骤的重试机制。您可以查看 spring retry documentation and regarding retry on step level this is nice blog post,它解释了 spring 批处理中的跳过和重试机制。