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.admin.domain.JobExecutionInfoResource.java

public JobExecutionInfoResource(JobExecution jobExecution, TimeZone timeZone) {

    if (timeZone != null) {
        this.timeZone = timeZone;
    } else {//  w  ww . j av  a 2 s  .c o 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:admin.jmx.SimpleJobExecutionMetrics.java

private JobExecution getLatestJobExecution(String jobName) {
    try {//from w  ww.j av  a  2s.  c o  m
        // On the cautious side: grab the last 4 executions by ID and look for
        // the one that was last created...
        Collection<JobExecution> jobExecutions = jobService.listJobExecutionsForJob(jobName, 0, 4);
        if (jobExecutions.isEmpty()) {
            return null;
        }
        long lastUpdated = 0L;
        JobExecution result = null;
        for (JobExecution jobExecution : jobExecutions) {
            long updated = jobExecution.getCreateTime().getTime();
            if (updated > lastUpdated) {
                result = jobExecution;
                lastUpdated = updated;
            } else if (result != null && updated == lastUpdated && jobExecution.getId() > result.getId()) {
                // Tie breaker using ID
                result = jobExecution;
            }
        }
        return result;
    } catch (NoSuchJobException e) {
        throw new IllegalStateException("Cannot locate job=" + jobName, e);
    }
}

From source file:org.springframework.batch.admin.web.JobExecutionController.java

@RequestMapping(value = "/jobs/{jobName}/{jobInstanceId}/executions", method = RequestMethod.POST)
public String restart(Model model, @PathVariable String jobName, @PathVariable long jobInstanceId,
        @ModelAttribute("date") Date date, Errors errors) {

    try {//from  www  .  j a  v a  2  s .  co m

        Collection<JobExecution> jobExecutions = jobService.getJobExecutionsForJobInstance(jobName,
                jobInstanceId);
        model.addAttribute(new JobInfo(jobName, jobExecutions.size() + 1));
        JobExecution jobExecution = jobExecutions.iterator().next();
        model.addAttribute(new JobExecutionInfo(jobExecution, timeZone));

        Long jobExecutionId = jobExecution.getId();

        try {

            jobExecution = jobService.restart(jobExecutionId);
            model.addAttribute(new JobExecutionInfo(jobExecution, timeZone));

        } catch (NoSuchJobExecutionException e) {
            errors.reject("no.such.job.execution", new Object[] { jobExecutionId },
                    "There is no such job execution (" + jobExecutionId + ")");
        } catch (JobExecutionAlreadyRunningException e) {
            errors.reject("job.execution.already.running", new Object[] { jobExecutionId },
                    "This job execution is already running (" + jobExecutionId + ")");
        } catch (JobRestartException e) {
            errors.reject("job.restart.exception", new Object[] { jobName },
                    "There was a problem restarting the job (" + jobName + ")");
        } catch (JobInstanceAlreadyCompleteException e) {
            errors.reject("job.instance.already.complete", new Object[] { jobName },
                    "The job instance is already complete for (" + jobName
                            + "). Use different job parameters to launch it again.");
        } catch (JobParametersInvalidException e) {
            errors.reject("job.parameters.invalid", new Object[] { jobName },
                    "The job parameters are invalid according to the job (" + jobName + ")");
        }

    } catch (NoSuchJobException e) {
        errors.reject("no.such.job", new Object[] { jobName }, "There is no such job (" + jobName + ")");
    }

    return "jobs/execution";

}

From source file:admin.service.SimpleJobService.java

/**
 * Check all the active executions and see if they are still actually
 * running. Remove the ones that have completed.
 */// w ww  . java 2 s . c om
@Scheduled(fixedDelay = 60000)
public void removeInactiveExecutions() {

    for (Iterator<JobExecution> iterator = activeExecutions.iterator(); iterator.hasNext();) {
        JobExecution jobExecution = iterator.next();
        try {
            jobExecution = getJobExecution(jobExecution.getId());
        } catch (NoSuchJobExecutionException e) {
            logger.error("Unexpected exception loading JobExecution", e);
        }
        if (!jobExecution.isRunning()) {
            iterator.remove();
        }
    }

}

From source file:com.xchanging.support.batch.admin.service.SimpleJobService.java

/**
 * Stop all the active jobs and wait for them (up to a time out) to finish
 * processing./*  w ww. j av  a 2s  .co m*/
 */
public void destroy() throws Exception {

    Exception firstException = null;

    for (JobExecution jobExecution : activeExecutions) {
        try {
            if (jobExecution.isRunning()) {
                stop(jobExecution.getId());
            }
        } catch (JobExecutionNotRunningException e) {
            logger.info("JobExecution is not running so it cannot be stopped");
        } catch (Exception e) {
            logger.error("Unexpected exception stopping JobExecution", e);
            if (firstException == null) {
                firstException = e;
            }
        }
    }

    int count = 0;
    int maxCount = (shutdownTimeout + 1000) / 1000;
    while (!activeExecutions.isEmpty() && ++count < maxCount) {
        logger.error("Waiting for " + activeExecutions.size() + " active executions to complete");
        removeInactiveExecutions();
        Thread.sleep(1000L);
    }

    if (firstException != null) {
        throw firstException;
    }

}

From source file:admin.service.SimpleJobService.java

/**
 * Stop all the active jobs and wait for them (up to a time out) to finish
 * processing./*from  w w  w .  jav  a  2 s.c om*/
 */
@Override
public void destroy() throws Exception {

    Exception firstException = null;

    for (JobExecution jobExecution : activeExecutions) {
        try {
            if (jobExecution.isRunning()) {
                stop(jobExecution.getId());
            }
        } catch (JobExecutionNotRunningException e) {
            logger.info("JobExecution is not running so it cannot be stopped");
        } catch (Exception e) {
            logger.error("Unexpected exception stopping JobExecution", e);
            if (firstException == null) {
                firstException = e;
            }
        }
    }

    int count = 0;
    int maxCount = (shutdownTimeout + 1000) / 1000;
    while (!activeExecutions.isEmpty() && ++count < maxCount) {
        logger.error("Waiting for " + activeExecutions.size() + " active executions to complete");
        removeInactiveExecutions();
        Thread.sleep(1000L);
    }

    if (firstException != null) {
        throw firstException;
    }

}

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

/**
 * Interface method implementation//from  w  w  w . j  a va 2 s. c o m
 * @see org.springframework.batch.admin.service.JobService#getJobExecution(java.lang.Long)
 */
public JobExecution getJobExecution(Long jobExecutionId) throws NoSuchJobExecutionException {
    for (String jobName : this.jobRegistry.getJobNames()) {
        for (JobInstance jobInstance : this.jobExplorer.getJobInstances(jobName, 0, Integer.MAX_VALUE)) {
            for (JobExecution jobExecution : this.jobExplorer.getJobExecutions(jobInstance)) {
                if (jobExecution.getId() == jobExecutionId) {
                    return jobExecution;
                }
            }
        }
    }
    return null;
}

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

/**
 * Interface method implementation/*w w w .j  a v a2  s.  com*/
 * @see org.springframework.batch.admin.service.JobService#getStepExecutions(java.lang.Long)
 */
public Collection<StepExecution> getStepExecutions(Long jobExecutionId) throws NoSuchJobExecutionException {
    for (String jobName : this.jobRegistry.getJobNames()) {
        for (JobInstance jobInstance : this.jobExplorer.getJobInstances(jobName, 0, Integer.MAX_VALUE)) {
            for (JobExecution jobExecution : this.jobExplorer.getJobExecutions(jobInstance)) {
                if (jobExecution.getId() == jobExecutionId) {
                    return jobExecution.getStepExecutions();
                }
            }
        }
    }
    return null;
}

From source file:com.iisigroup.cap.batch.handler.BatchHandler.java

/**
 * ?/*from   ww w .  j  a  va  2s .  c  o m*/
 * 
 * @param request
 *            IRequest
 * @return IResult
 */
public Result executionRestart(Request request) {
    AjaxFormResult result = new AjaxFormResult();
    try {
        String jobName = request.get("jobId");
        long jobInstanceId = Long.parseLong(request.get("jobInsId"));
        Collection<JobExecution> jobExecutions = jobService.getJobExecutionsForJobInstance(jobName,
                jobInstanceId);
        new JobInfo(jobName, jobExecutions.size() + 1);
        JobExecution jobExecution = jobExecutions.iterator().next();
        new JobExecutionInfo(jobExecution, TimeZone.getDefault());

        Long jobExecutionId = jobExecution.getId();

        try {

            jobExecution = jobService.restart(jobExecutionId);
            new JobExecutionInfo(jobExecution, TimeZone.getDefault());

        } catch (NoSuchJobExecutionException e) {
            throw new CapMessageException("msg.job.noSuchJob", getClass());
        } catch (JobExecutionAlreadyRunningException e) {
            throw new CapMessageException("msg.job.alreadyRunning", getClass());
        } catch (JobRestartException e) {
            throw new CapMessageException("msg.job.restartExecption", getClass())
                    .setExtraInformation(new Object[] { jobName });
        } catch (JobInstanceAlreadyCompleteException e) {
            throw new CapMessageException("msg.job.alreadyComplete", getClass());
        } catch (JobParametersInvalidException e) {
            throw new CapMessageException("msg.job.parametersInvalid", getClass())
                    .setExtraInformation(new Object[] { jobName });
        }
    } catch (NoSuchJobException e) {
        throw new CapMessageException("msg.job.noSuchJob", getClass());
    }
    return result;
}

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

/**
 * Interface method implementation//from   www  .ja v  a2s . c  om
 * @see org.springframework.batch.admin.service.JobService#getStepExecution(java.lang.Long, java.lang.Long)
 */
public StepExecution getStepExecution(Long jobExecutionId, Long stepExecutionId)
        throws NoSuchStepExecutionException, NoSuchJobExecutionException {
    for (String jobName : this.jobRegistry.getJobNames()) {
        for (JobInstance jobInstance : this.jobExplorer.getJobInstances(jobName, 0, Integer.MAX_VALUE)) {
            for (JobExecution jobExecution : this.jobExplorer.getJobExecutions(jobInstance)) {
                if (jobExecution.getId() == jobExecutionId) {
                    for (StepExecution step : jobExecution.getStepExecutions()) {
                        if (step.getId() == stepExecutionId) {
                            return step;
                        }
                    }
                }
            }
        }
    }
    return null;
}