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.test.repository.JdbcJobRepositoryTests.java

@Test
public void testFindOrCreateJobConcurrentlyWhenJobAlreadyExists() throws Exception {

    job = new JobSupport("test-job");
    job.setRestartable(true);//from  w  w  w  .j a  v a  2s  .c o  m
    job.setName("spam");

    JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
    cacheJobIds(execution);
    execution.setEndTime(new Timestamp(System.currentTimeMillis()));
    repository.update(execution);
    execution.setStatus(BatchStatus.FAILED);

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

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

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

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

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

private void cacheJobIds(JobExecution execution) {
    if (execution == null) {
        return;//from   w ww . j a  v  a  2  s . c om
    }
    jobExecutionIds.add(execution.getId());
    jobIds.add(execution.getJobId());
}

From source file:org.springframework.batch.integration.partition.JmsIntegrationTests.java

@Test
public void testLaunchJob() throws Exception {
    int before = jobExplorer.getJobInstances(job.getName(), 0, 100).size();
    assertNotNull(jobLauncher.run(job, new JobParameters()));
    List<JobInstance> jobInstances = jobExplorer.getJobInstances(job.getName(), 0, 100);
    int after = jobInstances.size();
    assertEquals(1, after - before);/*from  w  ww  .  j a v  a 2s.c o m*/
    JobExecution jobExecution = jobExplorer.getJobExecutions(jobInstances.get(jobInstances.size() - 1)).get(0);
    assertEquals(jobExecution.getExitStatus().getExitDescription(), BatchStatus.COMPLETED,
            jobExecution.getStatus());
    assertEquals(3, jobExecution.getStepExecutions().size());
    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
        // BATCH-1703: we are using a map dao so the step executions in the job execution are old and we need to
        // pull them back out of the repository...
        stepExecution = jobExplorer.getStepExecution(jobExecution.getId(), stepExecution.getId());
        logger.debug("" + stepExecution);
        assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    }
}

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

@Test
public void testLaunchJob() throws Exception {

    JobExecution jobExecution = jobLauncherTestUtils.launchJob();

    Thread.sleep(1000);//from w  ww.  j  a v  a2  s . com

    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.batch.sample.support.JdbcJobRepositoryTests.java

@Transactional
@Test//  w  ww  .  j  ava  2  s .c  o m
public void testFindOrCreateJob() throws Exception {
    job.setName("foo");
    int before = 0;
    JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
    int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
    assertEquals(before + 1, after);
    assertNotNull(execution.getId());
}

From source file:org.springframework.batch.sample.support.JdbcJobRepositoryTests.java

@Transactional
@Test//from   w  ww .  j ava 2  s  . c o m
public void testFindOrCreateJobConcurrently() throws Exception {

    job.setName("bar");

    int before = 0;
    assertEquals(0, before);

    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 = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
    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.");
}

From source file:org.springframework.batch.sample.support.JdbcJobRepositoryTests.java

@Test
public void testFindOrCreateJobConcurrentlyWhenJobAlreadyExists() throws Exception {

    job = new JobSupport("test-job");
    job.setRestartable(true);// w  ww .  java  2s  . co m
    job.setName("spam");

    JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
    cacheJobIds(execution);
    execution.setEndTime(new Timestamp(System.currentTimeMillis()));
    repository.update(execution);
    execution.setStatus(BatchStatus.FAILED);

    int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
    assertEquals(1, before);

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

    int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) FROM BATCH_JOB_INSTANCE");
    assertNotNull(execution.getId());
    assertEquals(before, after);

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

From source file:org.springframework.batch.sample.support.JdbcJobRepositoryTests.java

private void cacheJobIds(JobExecution execution) {
    if (execution == null)
        return;//  www.  ja  v  a2 s.  co  m
    jobExecutionIds.add(execution.getId());
    jobIds.add(execution.getJobId());
}

From source file:org.springframework.cloud.dataflow.server.batch.SimpleJobService.java

@Override
public int stopAll() {
    Collection<JobExecution> result = jobExecutionDao.getRunningJobExecutions();
    Collection<String> jsrJobNames = getJsrJobNames();

    for (JobExecution jobExecution : result) {
        if (jsrJobOperator != null && jsrJobNames.contains(jobExecution.getJobInstance().getJobName())) {
            jsrJobOperator.stop(jobExecution.getId());
        } else {/*from   w  w  w . j  av a 2 s. c  om*/
            jobExecution.stop();
            jobRepository.update(jobExecution);
        }
    }

    return result.size();
}

From source file:org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner.java

protected void execute(Job job, JobParameters jobParameters)
        throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
        JobParametersInvalidException, JobParametersNotFoundException {
    JobParameters nextParameters = new JobParametersBuilder(jobParameters, this.jobExplorer)
            .getNextJobParameters(job).toJobParameters();
    JobExecution execution = this.jobLauncher.run(job, nextParameters);
    if (this.publisher != null) {
        this.publisher.publishEvent(new JobExecutionEvent(execution));
    }//from w  w w. j a va 2 s  .c  om
    if (execution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())) {
        String message = String.format(
                "Job %s failed during " + "execution for jobId %s with jobExecutionId of %s",
                execution.getJobInstance().getJobName(), execution.getJobId(), execution.getId());
        logger.error(message);
        throw new TaskException(message);
    }
}