Spring batch (误差范围){ 抛出(错误)t; } 抛出新的非法状态异常(t); } }); }捕获(TaskRejectedException e){ jobExecution.upgradeStatus(BatchStatus.FAILED); if(jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)){ jobExecution.setExitStatus(ExitStatus.FAILED .补充说明(e)); } jobRepository.update(作业执行); } 返回作业执行; } 静态接口ExecutionContextContributor{ 布尔值=真; boolean\u NOTHING=false; /** * *@param executionContext *@return true(如果已将ExecutionContext贡献给 */ 公共布尔贡献(ExecutionContext ExecutionContext); } @凌驾 public void afterPropertieSet()引发异常{ state(jobRepository!=null,“尚未设置jobRepository”); if(taskExecutor==null){ info(“未设置TaskExecutor,默认为synchronous executor”); taskExecutor=新的SyncTaskExecutor(); } this.simpleLauncher=新的simplejoblancher(); this.simpleLancher.setJobRepository(jobRepository); this.simpleLancher.setTaskExecutor(taskExecutor); this.simpleLauncher.afterPropertiesSet(); } @凌驾 公共作业执行运行(作业、作业参数、作业参数) 抛出JobExecutionAlreadyRunningException、JobRestartException、, JobInstanceAlreadyCompleteException,JobParametersInvalidException{ 返回simpleLauncher.run(作业、作业参数); } }
从spring batch 2.2.x开始,支持非识别参数。如果您使用的是CommandLineJobRunner,则可以使用“-”前缀指定非标识参数。 例如: java org.springframework.batch.core.launch.support.CommandLineJobRunner文件=/my/file/path-session=5678 如果您使用的是旧版本的SpringBatch,则需要迁移数据库架构。请参阅上的“迁移到2.x.x”部分Spring batch (误差范围){ 抛出(错误)t; } 抛出新的非法状态异常(t); } }); }捕获(TaskRejectedException e){ jobExecution.upgradeStatus(BatchStatus.FAILED); if(jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)){ jobExecution.setExitStatus(ExitStatus.FAILED .补充说明(e)); } jobRepository.update(作业执行); } 返回作业执行; } 静态接口ExecutionContextContributor{ 布尔值=真; boolean\u NOTHING=false; /** * *@param executionContext *@return true(如果已将ExecutionContext贡献给 */ 公共布尔贡献(ExecutionContext ExecutionContext); } @凌驾 public void afterPropertieSet()引发异常{ state(jobRepository!=null,“尚未设置jobRepository”); if(taskExecutor==null){ info(“未设置TaskExecutor,默认为synchronous executor”); taskExecutor=新的SyncTaskExecutor(); } this.simpleLauncher=新的simplejoblancher(); this.simpleLancher.setJobRepository(jobRepository); this.simpleLancher.setTaskExecutor(taskExecutor); this.simpleLauncher.afterPropertiesSet(); } @凌驾 公共作业执行运行(作业、作业参数、作业参数) 抛出JobExecutionAlreadyRunningException、JobRestartException、, JobInstanceAlreadyCompleteException,JobParametersInvalidException{ 返回simpleLauncher.run(作业、作业参数); } },spring-batch,Spring Batch,从spring batch 2.2.x开始,支持非识别参数。如果您使用的是CommandLineJobRunner,则可以使用“-”前缀指定非标识参数。 例如: java org.springframework.batch.core.launch.support.CommandLineJobRunner文件=/my/file/path-session=5678 如果您使用的是旧版本的SpringBatch,则需要迁移数据库架构。请参阅上的“迁移到2.x.x”部分 这是该功能的Jira页面,下面是
这是该功能的Jira页面,下面是实现该功能的更改快速问题,您的JobFilter来自何处?该job filter界面是一个自定义实现,我们将其作为删除可能实现的另一点。我认为唯一的实现是检查是否基于与参数不同的标准集运行作业,如果找到了,则抛出作业请求。如果你愿意的话,可以省去这个。如果你需要的话,我可以挖掘接口,如果你愿意的话,但是你可以从上面的代码中收集接口,因为它只有一个方法。是的,这很有意义。我想这是一个自定义类,但我想确定一下。非常感谢。即使在spring批处理管理控制台中,使用减号对我来说也很有效
file=/my/file/path,session=1234
file=/my/file/path,session=5678
if (contributor != null) {
if (contributor.contributeTo(jobExecution.getExecutionContext())) {
jobRepository.updateExecutionContext(jobExecution);
}
}
public class ControlMJobLauncher implements JobLauncher, InitializingBean {
private JobRepository jobRepository;
private TaskExecutor taskExecutor;
private SimpleJobLauncher simpleLauncher;
private JobFilter jobFilter;
public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
/**
* Optional filter to prevent job launching based on some specific criteria.
* Jobs that are filtered out will return success to ControlM, but will not run
*/
public void setJobFilter(JobFilter jobFilter) {
this.jobFilter = jobFilter;
}
public JobExecution run(final Job job, final JobParameters jobParameters, ExecutionContextContributor contributor)
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException, JobFilteredException {
Assert.notNull(job, "The Job must not be null.");
Assert.notNull(jobParameters, "The JobParameters must not be null.");
//See if job is filtered
if(this.jobFilter != null && !jobFilter.launchJob(job, jobParameters)) {
throw new JobFilteredException(String.format("Job has been filtered by the filter: %s", jobFilter.getFilterName()));
}
final JobExecution jobExecution;
JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
if (lastExecution != null) {
if (!job.isRestartable()) {
throw new JobRestartException("JobInstance already exists and is not restartable");
}
logger.info(String.format("Restarting job %s instance %d", job.getName(), lastExecution.getId()));
}
// Check the validity of the parameters before doing creating anything
// in the repository...
job.getJobParametersValidator().validate(jobParameters);
/*
* There is a very small probability that a non-restartable job can be
* restarted, but only if another process or thread manages to launch
* <i>and</i> fail a job execution for this instance between the last
* assertion and the next method returning successfully.
*/
jobExecution = jobRepository.createJobExecution(job.getName(),
jobParameters);
if (contributor != null) {
if (contributor.contributeTo(jobExecution.getExecutionContext())) {
jobRepository.updateExecutionContext(jobExecution);
}
}
try {
taskExecutor.execute(new Runnable() {
public void run() {
try {
logger.info("Job: [" + job
+ "] launched with the following parameters: ["
+ jobParameters + "]");
job.execute(jobExecution);
logger.info("Job: ["
+ job
+ "] completed with the following parameters: ["
+ jobParameters
+ "] and the following status: ["
+ jobExecution.getStatus() + "]");
} catch (Throwable t) {
logger.warn(
"Job: ["
+ job
+ "] failed unexpectedly and fatally with the following parameters: ["
+ jobParameters + "]", t);
rethrow(t);
}
}
private void rethrow(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof Error) {
throw (Error) t;
}
throw new IllegalStateException(t);
}
});
} catch (TaskRejectedException e) {
jobExecution.upgradeStatus(BatchStatus.FAILED);
if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
jobExecution.setExitStatus(ExitStatus.FAILED
.addExitDescription(e));
}
jobRepository.update(jobExecution);
}
return jobExecution;
}
static interface ExecutionContextContributor {
boolean CONTRIBUTED_SOMETHING = true;
boolean CONTRIBUTED_NOTHING = false;
/**
*
* @param executionContext
* @return true if the exeuctioncontext was contributed to
*/
public boolean contributeTo(ExecutionContext executionContext);
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.state(jobRepository != null, "A JobRepository has not been set.");
if (taskExecutor == null) {
logger.info("No TaskExecutor has been set, defaulting to synchronous executor.");
taskExecutor = new SyncTaskExecutor();
}
this.simpleLauncher = new SimpleJobLauncher();
this.simpleLauncher.setJobRepository(jobRepository);
this.simpleLauncher.setTaskExecutor(taskExecutor);
this.simpleLauncher.afterPropertiesSet();
}
@Override
public JobExecution run(Job job, JobParameters jobParameters)
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
return simpleLauncher.run(job, jobParameters);
}
}