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

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

Introduction

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

Prototype

public Long getId() 

Source Link

Usage

From source file:org.springframework.batch.core.launch.support.SimpleJobOperator.java

@Override
public Set<Long> getRunningExecutions(String jobName) throws NoSuchJobException {
    Set<Long> set = new LinkedHashSet<Long>();
    for (JobExecution jobExecution : jobExplorer.findRunningJobExecutions(jobName)) {
        set.add(jobExecution.getId());
    }/* ww  w  .j  a  va  2  s .  c o m*/
    if (set.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) {
        throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName);
    }
    return set;
}

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./*www. jav a  2s.  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   w w w.j ava  2  s .co  m
 *
 * @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());//from w  w w. j  a v  a2s  .  c o  m
        jobExecution.upgradeStatus(BatchStatus.valueOf(status));
        jobExecution.setVersion(currentVersion);
    }
}

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

@Override
public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) {
    List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
            new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId);

    Assert.state(executions.size() <= 1,
            "There can be at most one step execution with given name for single job execution");
    if (executions.isEmpty()) {
        return null;
    } else {//from  w  ww . j  av  a 2s .co m
        return executions.get(0);
    }
}

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

@Override
public void addStepExecutions(JobExecution jobExecution) {
    getJdbcTemplate().query(getQuery(GET_STEP_EXECUTIONS), new StepExecutionRowMapper(jobExecution),
            jobExecution.getId());
}

From source file:org.springframework.batch.core.repository.support.SimpleJobRepository.java

@Override
public void update(JobExecution jobExecution) {

    Assert.notNull(jobExecution, "JobExecution cannot be null.");
    Assert.notNull(jobExecution.getJobId(), "JobExecution must have a Job ID set.");
    Assert.notNull(jobExecution.getId(), "JobExecution must be already saved (have an id assigned).");

    jobExecution.setLastUpdated(new Date(System.currentTimeMillis()));

    jobExecutionDao.synchronizeStatus(jobExecution);
    jobExecutionDao.updateJobExecution(jobExecution);
}

From source file:org.springframework.batch.core.test.repository.JdbcJobRepositoryTests.java

@Test
public void testFindOrCreateJob() throws Exception {
    job.setName("foo");
    int before = 0;
    JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
    int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE", Integer.class);
    assertEquals(before + 1, after);//from w  w w.ja  v  a  2  s  . co  m
    assertNotNull(execution.getId());
}

From source file:org.springframework.batch.core.test.repository.JdbcJobRepositoryTests.java

@Test
public void testFindOrCreateJobWithExecutionContext() throws Exception {
    job.setName("foo");
    int before = 0;
    JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
    execution.getExecutionContext().put("foo", "bar");
    repository.updateExecutionContext(execution);
    int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_EXECUTION_CONTEXT", Integer.class);
    assertEquals(before + 1, after);// ww w .  ja v a2  s . co m
    assertNotNull(execution.getId());
    JobExecution last = repository.getLastJobExecution(job.getName(), new JobParameters());
    assertEquals(execution, last);
    assertEquals(execution.getExecutionContext(), last.getExecutionContext());
}

From source file:org.springframework.batch.core.test.repository.JdbcJobRepositoryTests.java

@Test
public void testFindOrCreateJobConcurrently() throws Exception {

    job.setName("bar");

    int before = 0;
    assertEquals(0, before);/*ww w.  java 2 s.  c o m*/

    long t0 = System.currentTimeMillis();
    try {
        doConcurrentStart();
        fail("Expected JobExecutionAlreadyRunningException");
    } catch (JobExecutionAlreadyRunningException e) {
        // expected
    }
    long t1 = System.currentTimeMillis();

    JobExecution execution = (JobExecution) list.get(0);

    assertNotNull(execution);

    int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE", Integer.class);
    assertNotNull(execution.getId());
    assertEquals(before + 1, after);

    logger.info("Duration: " + (t1 - t0)
            + " - the second transaction did not block if this number is less than about 1000.");
}