下面列出了怎么用org.springframework.boot.autoconfigure.batch.JobExecutionEvent的API类实例代码及写法,或者点击链接到github查看源代码。
protected BatchStatus execute(Job job, JobParameters jobParameters)
throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
JobParametersInvalidException, JobParametersNotFoundException {
BatchStatus status = BatchStatus.UNKNOWN;
val nextParameters = getNextJobParameters(job, jobParameters);
if (nextParameters != null) {
val execution = jobLauncher.run(job, nextParameters);
if (publisher != null) {
publisher.publishEvent(new JobExecutionEvent(execution));
}
status = execution.getStatus();
}
return status;
}
@EventListener(JobExecutionEvent.class)
public void job(JobExecutionEvent executionEvent) {
log.info("jobExecutionEvent: "
+ executionEvent.getJobExecution().toString());
jdbcTemplate.query("select * from CONTACT", (RowCallbackHandler) rs -> log
.info(String.format("id=%s, full_name=%s, email=%s, valid_email=%s",
rs.getLong("id"), rs.getString("full_name"), rs.getString("email"),
rs.getBoolean("valid_email"))));
}
@Override
public void onApplicationEvent(JobExecutionEvent event) {
System.out.println("finished " + event.getJobExecution().toString());
this.jdbcTemplate
.query("SELECT first_name, last_name, email FROM contact",
(rs, i) -> new Contact(rs.getString("first_name"),
rs.getString("last_name"), rs.getString("email")))
.forEach(System.out::println);
}
protected void execute(Job job, JobParameters jobParameters)
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
String jobName = job.getName();
JobParameters parameters = jobParameters;
boolean jobInstanceExists = this.taskJobRepository.isJobInstanceExists(jobName,
parameters);
if (jobInstanceExists) {
JobExecution lastJobExecution = this.taskJobRepository
.getLastJobExecution(jobName, jobParameters);
if (lastJobExecution != null && isStoppedOrFailed(lastJobExecution)
&& job.isRestartable()) {
// Retry a failed or stopped execution with previous parameters
JobParameters previousParameters = lastJobExecution.getJobParameters();
/*
* remove Non-identifying parameters from the previous execution's
* parameters since there is no way to remove them programmatically. If
* they are required (or need to be modified) on a restart, they need to
* be (re)specified.
*/
JobParameters previousIdentifyingParameters = removeNonIdentifying(
previousParameters);
// merge additional parameters with previous ones (overriding those with
// the same key)
parameters = merge(previousIdentifyingParameters, jobParameters);
}
}
else {
JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
if (incrementer != null) {
JobParameters nextParameters = new JobParametersBuilder(jobParameters,
this.taskJobExplorer).getNextJobParameters(job).toJobParameters();
parameters = merge(nextParameters, jobParameters);
}
}
JobExecution execution = this.taskJobLauncher.run(job, parameters);
if (this.taskApplicationEventPublisher != null) {
this.taskApplicationEventPublisher
.publishEvent(new JobExecutionEvent(execution));
}
this.jobExecutionList.add(execution);
if (execution.getStatus().equals(BatchStatus.FAILED)) {
throwJobFailedException(Collections.singletonList(execution));
}
}