6
A
回答
6
基本上,你可以做如下:
配置一個
JobExplorer
工廠bean在你的應用環境:配置一個
JobOperator
豆你applictaion方面查詢的jobExplorer針對不同的職位名稱:
jobExplorer.getJobNames()
對於來自步驟(3),查詢jobExplorer對未完成的作業每個作業:
jobExplorer.findRunningJobExecutions(String jobName)
對於每個
JobExecution
步驟(4)調用:jobOperator.restart(jobExecution.getJobId())
確保引導過程中調用這個過程中,任何其他工作啓動
之前在技術上可以通過覆蓋JobExecutionDao
合併步驟3 + 4這樣的事情findRunningJobExecutions()
,但目前API不支持它。
對於上述的Spring bean配置的幫助,請諮詢reference documentation
2
下面是完整的解決方案,重新啓動JVM崩潰後的工作。
- 通過使restarable = 「真」
作業id = 「工作名」 的xmlns = 「http://www.springframework.org/schema/batch」 重新啓動做工作重新啓動= 「true」
2。代碼重新開始工作
import java.util.Date;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
public class ResartJob {
@Autowired
private JobExplorer jobExplorer;
@Autowired
JobRepository jobRepository;
@Autowired
private JobLauncher jobLauncher;
@Autowired
JobOperator jobOperator;
public void restart(){
try {
List<JobInstance> jobInstances = jobExplorer.getJobInstances("jobName",0,1);// this will get one latest job from the database
if(CollectionUtils.isNotEmpty(jobInstances)){
JobInstance jobInstance = jobInstances.get(0);
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
if(CollectionUtils.isNotEmpty(jobExecutions)){
for(JobExecution execution: jobExecutions){
// If the job status is STARTED then update the status to FAILED and restart the job using JobOperator.java
if(execution.getStatus().equals(BatchStatus.STARTED)){
execution.setEndTime(new Date());
execution.setStatus(BatchStatus.FAILED);
execution.setExitStatus(ExitStatus.FAILED);
jobRepository.update(execution);
jobOperator.restart(execution.getId());
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
3.
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean" p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:lobHandler-ref="oracleLobHandler"/>
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean" p:dataSource-ref="dataSource" />
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor" ref="jobLauncherTaskExecutor" />
</bean>
<task:executor id="jobLauncherTaskExecutor" pool-size="6" rejection-policy="ABORT" />
<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator" p:jobLauncher-ref="jobLauncher" p:jobExplorer-re`enter code here`f="jobExplorer" p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry"/>
4
您需要標註 「跑」 的作業重新啓動之前爲失敗,就像這樣:
List<String> jobs = jobExplorer.getJobNames();
for (String job : jobs) {
Set<JobExecution> runningJobs = jobExplorer.findRunningJobExecutions(job);
for (JobExecution runningJob : runningJobs) {
try {
runningJob.setStatus(BatchStatus.FAILED);
runningJob.setEndTime(new Date());
jobRepository.update(runningJob);
jobOperator.restart(runningJob.getId());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
}
相關問題
- 1. 我有批處理文件崩潰
- 2. GCTaskThread JVM崩潰
- 3. JpCapWriter崩潰JVM
- 4. 處理JNI崩潰
- 5. JVM崩潰EXCEPTION_ACCESS_VIOLATION(0xc0000005)
- 6. JVM偶爾崩潰
- 7. JBoss/HotSpot JVM崩潰
- 8. JVM突然崩潰
- 9. JVM隨機崩潰
- 10. Spring批處理項目集成了Spring批處理管理器
- 11. Spring批處理+ Spring API REST
- 12. Spring批處理3,IBM JVM上的配置加載錯誤(BackToBackPatternClassifier)
- 13. spring批處理MapJobRepositoryFactoryBean
- 14. Spring批處理 - StepExecutionListener
- 15. Spring批處理JdbcBatchItemWriter
- 16. 事件處理後wxpython窗口崩潰
- 17. 處理信號後Perl進程崩潰
- 18. 遷移到64位後JVM崩潰
- 19. 系統升級後jvm崩潰
- 20. JVM外的JNI崩潰 - EXCEPTION_ACCESS_VIOLATION
- 21. 6.0_26-b03中的JVM崩潰
- 22. JVM的崩潰報告
- 23. JVM崩潰Hadoop的減速
- 24. Spring批處理 - 聚合處理器
- 25. Spring批處理並行處理
- 26. 如何處理硒崩潰
- 27. JavaScript事件處理崩潰
- 28. 批准後的應用程序崩潰
- 29. 爲什麼下面的批處理代碼崩潰?
- 30. JProfiler 7.2.2遠程JVM崩潰
請[見我的問題](http://stackoverflow.com/questions/40990935/resume-a-spring-batch-job-from-last-processed-point)。我試圖處理相同的情況,工作正在開始,但已經處理的塊不會被自動考慮。 – 2016-12-06 10:18:51
這是在春季啓動工程?我的意思是我們能夠處理Spring啓動應用程序是否有異常並崩潰並重新運行? – Kenji 2017-08-12 10:42:52