List of usage examples for org.springframework.batch.core JobExecution getCreateTime
public Date getCreateTime()
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 w w w . j ava 2s. co 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:admin.jmx.SimpleJobExecutionMetrics.java
private JobExecution getLatestJobExecution(String jobName) { try {/*from w ww . j ava 2 s. 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.trpr.platform.batch.impl.spring.admin.repository.MapJobExecutionDao.java
@Override public JobExecution getLastJobExecution(JobInstance jobInstance) { JobExecution lastExec = null; for (JobExecution exec : executionsById.values()) { if (!exec.getJobInstance().equals(jobInstance)) { continue; }//from w w w . ja v a2 s.co m if (lastExec == null) { lastExec = exec; } if (lastExec.getCreateTime().before(exec.getCreateTime())) { lastExec = exec; } } return copy(lastExec); }
From source file:gemlite.shell.service.batch.ImportService.java
public void executeJob(String name, String tpl) { checkContextInitialized();// w w w . jav a 2 s . c o m JobItem item = jobItems.get(name + tpl); LogUtil.getAppLog().info("Job:" + name + " executing..."); try { JobExecution exec = jobLauncher.run(item.job, new JobParametersBuilder() .addDate("StartTime", new Date()).addString("name", name).toJobParameters()); String s1 = DateUtil.format(exec.getCreateTime(), "HH:mm:ss.SSS"); LogUtil.getAppLog().info("Job:" + name + " start asynchronous,start time:" + s1); } catch (Exception e) { LogUtil.getCoreLog().info("Job:" + name + " failed"); if (LogUtil.getCoreLog().isErrorEnabled()) LogUtil.getCoreLog().error(name, e); } }
From source file:org.jasig.ssp.util.importer.job.report.ReportGenerator.java
@SuppressWarnings("unchecked") private String buildReport(JobExecution jobExecution) { StringBuffer emailMessage = new StringBuffer(); String EOL = System.getProperty("line.separator"); SimpleDateFormat dt = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss"); long diff = jobExecution.getEndTime().getTime() - jobExecution.getCreateTime().getTime();//as given emailMessage.append("Start Time: " + dt.format(jobExecution.getCreateTime()) + EOL); emailMessage.append("End Time: " + dt.format(jobExecution.getEndTime()) + EOL); emailMessage.append("Duration: " + DurationFormatUtils.formatDurationWords(diff, true, true) + " (" + DurationFormatUtils.formatDurationHMS(diff) + ")" + EOL); emailMessage.append("Job Id: " + jobExecution.getJobId() + EOL); emailMessage.append("Job Paramters: " + jobExecution.getJobParameters() + EOL); emailMessage.append("Job Status: " + jobExecution.getExitStatus().getExitCode() + EOL); emailMessage.append(EOL).append(EOL); emailMessage.append("Job Details: " + EOL); Map<String, ReportEntry> report = (Map<String, ReportEntry>) jobExecution.getExecutionContext() .get("report"); if (report != null) { Set<Entry<String, ReportEntry>> entrySet = report.entrySet(); for (Entry<String, ReportEntry> entry : entrySet) { emailMessage.append(entry.getValue().toString() + EOL); }//from w ww . j av a2 s. c om if (entrySet.size() > 0) emailReport = true; } else { emailMessage.append("NO FILES PROCESSED." + EOL); } emailMessage.append(EOL).append(EOL); emailMessage.append("Errors: " + EOL); List<ErrorEntry> errors = (List<ErrorEntry>) jobExecution.getExecutionContext().get("errors"); List<Throwable> failureExceptions = jobExecution.getAllFailureExceptions(); if (errors != null) { for (ErrorEntry errorEntry : errors) { emailMessage.append(errorEntry.toString() + EOL); emailMessage.append(EOL); } } else if (failureExceptions == null || failureExceptions.size() == 0) { emailMessage.append("No Errors Found." + EOL); } if (failureExceptions != null) { for (Throwable failureException : failureExceptions) { if (ExceptionUtils.indexOfThrowable(failureException, PartialUploadGuardException.class) >= 0 || ExceptionUtils.indexOfThrowable(failureException, BeanCreationException.class) >= 0) { emailReport = true; logger.info("emailReport:" + emailReport); } logger.info("failureException:" + failureException.getClass().getName()); emailMessage.append(failureException.getMessage() + EOL); } } String validations = (String) jobExecution.getExecutionContext().get("databaseValidations"); if (validations != null) { emailMessage.append("Database Validations:" + EOL + validations); } logger.info(emailMessage.toString()); return emailMessage.toString(); }
From source file:org.springframework.batch.admin.domain.JobExecutionInfoResource.java
public JobExecutionInfoResource(JobExecution jobExecution, TimeZone timeZone) { if (timeZone != null) { this.timeZone = timeZone; } else {//from w w w. ja v a2s .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: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. jav a 2 s .c om * * @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
/** * Validate JobExecution. At a minimum, JobId, StartTime, EndTime, and * Status cannot be null.//from w w w . j av a 2 s. c o m * * @param jobExecution * @throws IllegalArgumentException */ private void validateJobExecution(JobExecution jobExecution) { Assert.notNull(jobExecution); Assert.notNull(jobExecution.getJobId(), "JobExecution Job-Id cannot be null."); Assert.notNull(jobExecution.getStatus(), "JobExecution status cannot be null."); Assert.notNull(jobExecution.getCreateTime(), "JobExecution create time cannot be null"); }
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.//w ww .j a v a 2 s . c o 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(); } }