Example usage for org.springframework.batch.core JobExecution getVersion

List of usage examples for org.springframework.batch.core JobExecution getVersion

Introduction

In this page you can find the example usage for org.springframework.batch.core JobExecution getVersion.

Prototype

public Integer getVersion() 

Source Link

Usage

From source file:org.trpr.platform.batch.impl.spring.admin.repository.MapJobExecutionDao.java

@Override
public void synchronizeStatus(JobExecution jobExecution) {
    JobExecution saved = getJobExecution(jobExecution.getId());
    if (saved.getVersion().intValue() != jobExecution.getVersion().intValue()) {
        jobExecution.upgradeStatus(saved.getStatus());
        jobExecution.setVersion(saved.getVersion());
    }//  w  ww. j a  v  a2 s  .  c om
}

From source file:org.trpr.platform.batch.impl.spring.admin.repository.MapJobExecutionDao.java

/**
 * Returns a copy of {@link JobExecution}, by adding new Objects for every field(no references are passed)
 * /*from ww w  .ja va2s. c  o  m*/
 * @param original JobExecution to be copied
 * @return JobExecution copy
 */
private static JobExecution copy(JobExecution original) {
    JobInstance jobInstance = original.getJobInstance();
    JobExecution copy;
    if (jobInstance == null) {
        copy = new JobExecution(original.getId());
    }
    copy = new JobExecution(jobInstance, original.getId());
    if (original.getStartTime() != null) {
        copy.setStartTime((Date) original.getStartTime().clone());
    }
    if (original.getEndTime() != null) {
        copy.setEndTime((Date) original.getEndTime().clone());
    }
    if (original.getStatus() != null) {
        copy.setStatus(BatchStatus.valueOf(original.getStatus().name()));
    }
    if (original.getExitStatus() != null) {
        copy.setExitStatus(new ExitStatus(original.getExitStatus().getExitCode(),
                original.getExitStatus().getExitDescription()));
    }
    if (original.getCreateTime() != null) {
        copy.setCreateTime((Date) original.getCreateTime().clone());
    }
    if (original.getLastUpdated() != null) {
        copy.setLastUpdated((Date) original.getLastUpdated().clone());
    }
    copy.setVersion(original.getVersion());
    return copy;
}

From source file:org.trpr.platform.batch.impl.spring.admin.repository.MapJobExecutionDao.java

@Override
public void updateJobExecution(JobExecution jobExecution) {
    Long id = jobExecution.getId();
    Assert.notNull(id, "JobExecution is expected to have an id (should be saved already)");
    JobExecution persistedExecution = executionsById.get(id);
    Assert.notNull(persistedExecution, "JobExecution must already be saved");

    synchronized (jobExecution) {
        if (!persistedExecution.getVersion().equals(jobExecution.getVersion())) {
            throw new OptimisticLockingFailureException("Attempt to update step execution id=" + id
                    + " with wrong version (" + jobExecution.getVersion() + "), where current version is "
                    + persistedExecution.getVersion());
        }//from   w w  w.  ja v  a2 s  .  co  m
        jobExecution.incrementVersion();
        this.addNewExecution(id, copy(jobExecution));
    }
}

From source file:org.springframework.batch.admin.domain.JobExecutionInfoResource.java

public JobExecutionInfoResource(JobExecution jobExecution, TimeZone timeZone) {

    if (timeZone != null) {
        this.timeZone = timeZone;
    } else {//from  ww  w  .  ja va2s  .  co m
        this.timeZone = TimeZone.getTimeZone("UTC");
    }

    this.executionId = jobExecution.getId();
    this.jobId = jobExecution.getJobId();
    this.stepExecutionCount = jobExecution.getStepExecutions().size();
    this.jobParameters = jobExecution.getJobParameters();
    this.status = jobExecution.getStatus();
    this.exitStatus = jobExecution.getExitStatus();
    this.jobConfigurationName = jobExecution.getJobConfigurationName();
    this.failureExceptions = jobExecution.getFailureExceptions();
    Map<String, Object> executionContextEntires = new HashMap<String, Object>(
            jobExecution.getExecutionContext().size());

    for (Map.Entry<String, Object> stringObjectEntry : jobExecution.getExecutionContext().entrySet()) {
        executionContextEntires.put(stringObjectEntry.getKey(), stringObjectEntry.getValue());
    }

    this.executionContext = executionContextEntires;

    this.version = jobExecution.getVersion();

    JobInstance jobInstance = jobExecution.getJobInstance();
    if (jobInstance != null) {
        this.jobName = jobInstance.getJobName();
        BatchStatus status = jobExecution.getStatus();
        this.restartable = status.isGreaterThan(BatchStatus.STOPPING)
                && status.isLessThan(BatchStatus.ABANDONED);
        this.abandonable = status.isGreaterThan(BatchStatus.STARTED) && status != BatchStatus.ABANDONED;
        this.stoppable = status.isLessThan(BatchStatus.STOPPING) && status != BatchStatus.COMPLETED;
    } else {
        this.jobName = "?";
    }

    this.dateFormat = this.dateFormat.withZone(DateTimeZone.forTimeZone(timeZone));

    this.createDate = dateFormat.print(jobExecution.getCreateTime().getTime());
    this.lastUpdated = dateFormat.print(jobExecution.getLastUpdated().getTime());

    if (jobExecution.getStartTime() != null) {
        this.startTime = dateFormat.print(jobExecution.getStartTime().getTime());
        this.endTime = dateFormat.print(jobExecution.getEndTime().getTime());
    }
}

From source file:org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.java

/**
 *
 * SQL implementation using Sequences via the Spring incrementer
 * abstraction. Once a new id has been obtained, the JobExecution is saved
 * via a SQL INSERT statement./*w  w w  .j  a  v  a2  s  .c  o m*/
 *
 * @see JobExecutionDao#saveJobExecution(JobExecution)
 * @throws IllegalArgumentException if jobExecution is null, as well as any
 * of it's fields to be persisted.
 */
@Override
public void saveJobExecution(JobExecution jobExecution) {

    validateJobExecution(jobExecution);

    jobExecution.incrementVersion();

    jobExecution.setId(jobExecutionIncrementer.nextLongValue());
    Object[] parameters = new Object[] { jobExecution.getId(), jobExecution.getJobId(),
            jobExecution.getStartTime(), jobExecution.getEndTime(), jobExecution.getStatus().toString(),
            jobExecution.getExitStatus().getExitCode(), jobExecution.getExitStatus().getExitDescription(),
            jobExecution.getVersion(), jobExecution.getCreateTime(), jobExecution.getLastUpdated(),
            jobExecution.getJobConfigurationName() };
    getJdbcTemplate().update(getQuery(SAVE_JOB_EXECUTION), parameters,
            new int[] { Types.BIGINT, Types.BIGINT, Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR,
                    Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP,
                    Types.VARCHAR });

    insertJobParameters(jobExecution.getId(), jobExecution.getJobParameters());
}

From source file:org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.java

/**
 * Update given JobExecution using a SQL UPDATE statement. The JobExecution
 * is first checked to ensure all fields are not null, and that it has an
 * ID. The database is then queried to ensure that the ID exists, which
 * ensures that it is valid./*from ww  w  .j ava  2 s . c om*/
 *
 * @see JobExecutionDao#updateJobExecution(JobExecution)
 */
@Override
public void updateJobExecution(JobExecution jobExecution) {

    validateJobExecution(jobExecution);

    Assert.notNull(jobExecution.getId(),
            "JobExecution ID cannot be null. JobExecution must be saved before it can be updated");

    Assert.notNull(jobExecution.getVersion(),
            "JobExecution version cannot be null. JobExecution must be saved before it can be updated");

    synchronized (jobExecution) {
        Integer version = jobExecution.getVersion() + 1;

        String exitDescription = jobExecution.getExitStatus().getExitDescription();
        if (exitDescription != null && exitDescription.length() > exitMessageLength) {
            exitDescription = exitDescription.substring(0, exitMessageLength);
            if (logger.isDebugEnabled()) {
                logger.debug("Truncating long message before update of JobExecution: " + jobExecution);
            }
        }
        Object[] parameters = new Object[] { jobExecution.getStartTime(), jobExecution.getEndTime(),
                jobExecution.getStatus().toString(), jobExecution.getExitStatus().getExitCode(),
                exitDescription, version, jobExecution.getCreateTime(), jobExecution.getLastUpdated(),
                jobExecution.getId(), jobExecution.getVersion() };

        // Check if given JobExecution's Id already exists, if none is found
        // it
        // is invalid and
        // an exception should be thrown.
        if (getJdbcTemplate().queryForObject(getQuery(CHECK_JOB_EXECUTION_EXISTS), Integer.class,
                new Object[] { jobExecution.getId() }) != 1) {
            throw new NoSuchObjectException("Invalid JobExecution, ID " + jobExecution.getId() + " not found.");
        }

        int count = getJdbcTemplate().update(getQuery(UPDATE_JOB_EXECUTION), parameters,
                new int[] { Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                        Types.INTEGER, Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT, Types.INTEGER });

        // Avoid concurrent modifications...
        if (count == 0) {
            int curentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_JOB_EXECUTION),
                    Integer.class, new Object[] { jobExecution.getId() });
            throw new OptimisticLockingFailureException(
                    "Attempt to update job execution id=" + jobExecution.getId() + " with wrong version ("
                            + jobExecution.getVersion() + "), where current version is " + curentVersion);
        }

        jobExecution.incrementVersion();
    }
}

From source file:org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.java

@Override
public void synchronizeStatus(JobExecution jobExecution) {
    int currentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_JOB_EXECUTION),
            Integer.class, jobExecution.getId());

    if (currentVersion != jobExecution.getVersion().intValue()) {
        String status = getJdbcTemplate().queryForObject(getQuery(GET_STATUS), String.class,
                jobExecution.getId());//ww w . j  a va2  s .  co m
        jobExecution.upgradeStatus(BatchStatus.valueOf(status));
        jobExecution.setVersion(currentVersion);
    }
}

From source file:org.springframework.batch.sample.DatabaseShutdownFunctionalTests.java

@Test
public void testLaunchJob() throws Exception {

    JobExecution jobExecution = jobLauncherTestUtils.launchJob();

    Thread.sleep(1000);/*from w  w  w. j  a va  2  s.c o  m*/

    assertEquals(BatchStatus.STARTED, jobExecution.getStatus());
    assertTrue(jobExecution.isRunning());
    assertNotNull(jobExecution.getVersion());

    jobOperator.stop(jobExecution.getId());

    int count = 0;
    while (jobExecution.isRunning() && count <= 10) {
        logger.info("Checking for end time in JobExecution: count=" + count);
        Thread.sleep(100);
        count++;
    }

    assertFalse("Timed out waiting for job to end.", jobExecution.isRunning());
    assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());

}

From source file:org.springframework.yarn.batch.repository.JobRepositoryRemoteService.java

/**
 * Handles saving a job execution./*  www . j a v  a2 s  .c o m*/
 *
 * @param request the {@link SaveJobExecutionReq}
 * @return the {@link SaveJobExecutionRes}
 */
private SaveJobExecutionRes handleSaveJobExecution(SaveJobExecutionReq request) {
    SaveJobExecutionRes response = null;
    try {
        JobExecution jobExecution = JobRepositoryRpcFactory.convertJobExecutionType(request.jobExecution);
        jobExecutionDao.saveJobExecution(jobExecution);
        response = new SaveJobExecutionRes();
        response.id = jobExecution.getId();
        response.version = jobExecution.getVersion();
    } catch (Exception e) {
        log.error("error handling command", e);
    }
    return response;
}

From source file:org.springframework.yarn.batch.repository.JobRepositoryRemoteService.java

/**
 * Handles updating a job execution.//  ww w. j ava 2  s  . c  o m
 *
 * @param request the {@link UpdateJobExecutionReq}
 * @return the {@link UpdateJobExecutionRes}
 */
private UpdateJobExecutionRes handleUpdateJobExecution(UpdateJobExecutionReq request) {
    UpdateJobExecutionRes response = null;
    try {
        JobExecution jobExecution = JobRepositoryRpcFactory.convertJobExecutionType(request.jobExecution);
        jobExecutionDao.updateJobExecution(jobExecution);
        response = new UpdateJobExecutionRes(jobExecution.getVersion());
    } catch (Exception e) {
        log.error("error handling command", e);
    }
    return response;
}