跳至主要內容

Job Execution


Job Execution

JobExecution介绍

JobExecution表示Job执行的句柄。 一次Job的执行可能成功也可能失败,只有Job Execution执行成功之后,对应的Job Instance才会被完成。

JobExecution源代码详解

package org.springframework.batch.core;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import org.springframework.batch.item.ExecutionContext;

public class JobExecution extends Entity {
    private final JobParameters jobParameters;
    private JobInstance jobInstance;
    private volatile Collection<StepExecution> stepExecutions;
    private volatile BatchStatus status;
    private volatile Date startTime;
    private volatile Date createTime;
    private volatile Date endTime;
    private volatile Date lastUpdated;
    private volatile ExitStatus exitStatus;
    private volatile ExecutionContext executionContext;
    private transient volatile List<Throwable> failureExceptions;

    public JobExecution(JobInstance job, Long id, JobParameters jobParameters) {
        super(id);
        this.stepExecutions = new CopyOnWriteArraySet();
        this.status = BatchStatus.STARTING;
        this.startTime = null;
        this.createTime = new Date(System.currentTimeMillis());
        this.endTime = null;
        this.lastUpdated = null;
        this.exitStatus = ExitStatus.UNKNOWN;
        this.executionContext = new ExecutionContext();
        this.failureExceptions = new CopyOnWriteArrayList();
        this.jobInstance = job;
        this.jobParameters = jobParameters == null ? new JobParameters() : jobParameters;
    }

    public JobExecution(JobInstance job, JobParameters jobParameters) {
        this(job, (Long)null, jobParameters);
    }

    public JobExecution(Long id, JobParameters jobParameters) {
        this((JobInstance)null, id, jobParameters);
    }

    public JobExecution(Long id) {
        this((JobInstance)null, id, (JobParameters)null);
    }

    public JobParameters getJobParameters() {
        return this.jobParameters;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public void setJobInstance(JobInstance jobInstance) {
        this.jobInstance = jobInstance;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getStartTime() {
        return this.startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public BatchStatus getStatus() {
        return this.status;
    }

    public void setStatus(BatchStatus status) {
        this.status = status;
    }

    public void upgradeStatus(BatchStatus status) {
        this.status = this.status.upgradeTo(status);
    }

    public Long getJobId() {
        return this.jobInstance != null ? this.jobInstance.getId() : null;
    }

    public void setExitStatus(ExitStatus exitStatus) {
        this.exitStatus = exitStatus;
    }

    public ExitStatus getExitStatus() {
        return this.exitStatus;
    }

    public JobInstance getJobInstance() {
        return this.jobInstance;
    }

    public Collection<StepExecution> getStepExecutions() {
        return Collections.unmodifiableList(new ArrayList(this.stepExecutions));
    }

    public StepExecution createStepExecution(String stepName) {
        StepExecution stepExecution = new StepExecution(stepName, this);
        this.stepExecutions.add(stepExecution);
        return stepExecution;
    }

    public boolean isRunning() {
        return this.endTime == null;
    }

    public boolean isStopping() {
        return this.status == BatchStatus.STOPPING;
    }

    public void stop() {
        Iterator i$ = this.stepExecutions.iterator();

        while(i$.hasNext()) {
            StepExecution stepExecution = (StepExecution)i$.next();
            stepExecution.setTerminateOnly();
        }

        this.status = BatchStatus.STOPPING;
    }

    public void setExecutionContext(ExecutionContext executionContext) {
        this.executionContext = executionContext;
    }

    public ExecutionContext getExecutionContext() {
        return this.executionContext;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    void addStepExecution(StepExecution stepExecution) {
        this.stepExecutions.add(stepExecution);
    }

    public Date getLastUpdated() {
        return this.lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public List<Throwable> getFailureExceptions() {
        return this.failureExceptions;
    }

    public synchronized void addFailureException(Throwable t) {
        this.failureExceptions.add(t);
    }

    public synchronized List<Throwable> getAllFailureExceptions() {
        Set<Throwable> allExceptions = new HashSet(this.failureExceptions);
        Iterator i$ = this.stepExecutions.iterator();

        while(i$.hasNext()) {
            StepExecution stepExecution = (StepExecution)i$.next();
            allExceptions.addAll(stepExecution.getFailureExceptions());
        }

        return new ArrayList(allExceptions);
    }

    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        this.failureExceptions = new ArrayList();
    }

    public String toString() {
        return super.toString() + String.format(", startTime=%s, endTime=%s, lastUpdated=%s, status=%s, exitStatus=%s, job=[%s], jobParameters=[%s]", this.startTime, this.endTime, this.lastUpdated, this.status, this.exitStatus, this.jobInstance, this.jobParameters);
    }

    public void addStepExecutions(List<StepExecution> stepExecutions) {
        if (stepExecutions != null) {
            this.stepExecutions.removeAll(stepExecutions);
            this.stepExecutions.addAll(stepExecutions);
        }

    }
}
上次编辑于:
贡献者: Neil