List of usage examples for org.springframework.batch.core.scope.context ChunkContext getStepContext
public StepContext getStepContext()
From source file:com.github.jrrdev.mantisbtsync.core.jobs.projects.tasklets.ProjectsListTasklet.java
/** * {@inheritDoc}// ww w .j ava 2 s . com * @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext) */ @Override public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) throws Exception { Assert.notNull(clientStub); // Si on a renseign un authManager, on cherche rcuprer le cookie if (authManager != null && authManager.getAuthCookie() != null) { clientStub._setProperty(HTTPConstants.HEADER_COOKIE, authManager.getAuthCookie()); } final Set<BigInteger> projectsId = new HashSet<BigInteger>(); projectsId.add(projectId); insertIntoDb(projectId, null); searchAndInsertSubProject(projectId, projectsId); chunkContext.getStepContext().getStepExecution().getExecutionContext() .put("mantis.loop.projects_to_process", projectsId); return RepeatStatus.FINISHED; }
From source file:fr.acxio.tools.agia.tasks.FileCopyTasklet.java
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws ResourceCreationException, IOException, FileCopyException { File aOriginFile = origin.getFile(); if (aOriginFile.exists() && aOriginFile.isFile()) { if (!ignoreEmptyFile || (aOriginFile.length() > 0)) { if (sContribution != null) { sContribution.incrementReadCount(); }/*w w w . j a va 2 s. co m*/ if (destinationFactory != null) { Map<String, Object> aDestinationParams = new HashMap<String, Object>(); aDestinationParams.put(ResourceFactoryConstants.PARAM_SOURCE, origin); aDestinationParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC, ((sChunkContext != null) && (sChunkContext.getStepContext() != null)) ? sChunkContext.getStepContext().getStepExecution() : null); destination = destinationFactory.getResource(aDestinationParams); } File aDestinationFile = destination.getFile(); if (aDestinationFile.exists()) { if (forceReplace && aDestinationFile.isFile()) { if (!aDestinationFile.delete()) { throw new FileCopyException("Cannot remove: " + destination); } } else { throw new FileCopyException("Cannot replace: " + destination); } } if (LOGGER.isInfoEnabled()) { LOGGER.info("Copying : {} => {}", aOriginFile.getAbsolutePath(), aDestinationFile.getAbsolutePath()); } FileUtils.copyFile(aOriginFile, aDestinationFile); if ((emptyOrigin || deleteOrigin) && !aOriginFile.delete()) { throw new FileCopyException("Cannot delete: " + origin); } if (emptyOrigin && !aOriginFile.createNewFile()) { throw new FileCopyException("Cannot create: " + origin); } if (sContribution != null) { sContribution.incrementWriteCount(1); } } else if (LOGGER.isInfoEnabled()) { LOGGER.info("Ignoring empty file : {}", aOriginFile.getAbsolutePath()); } } else { throw new FileCopyException("File not found: " + origin); } return RepeatStatus.FINISHED; }
From source file:org.cbio.portal.pipelines.foundation.FoundationFileTasklet.java
@Override public RepeatStatus execute(StepContribution stepContext, ChunkContext chunkContext) throws Exception { Map<String, CaseType> fmiCaseTypeMap = new LinkedHashMap<>(); // get list of xml files sorted by date and put data into map File[] sourceXmlFiles = getDateSortedFiles(new File(sourceDir)); for (File xmlFile : sourceXmlFiles) { List<CaseType> newCases = new ArrayList(); try {/*from w ww . ja v a2 s.c om*/ LOG.info("Extracting case data from: " + xmlFile.getName()); newCases = extractFileCaseData(xmlFile); } catch (JAXBException | IOException ex) { LOG.error("Error processing file: " + xmlFile.getName()); ex.printStackTrace(); } // since files are sorted by filename/date, cases in more than one file // will automatically only have their most recent data converted for (CaseType ct : newCases) { if (fmiCaseTypeMap.containsKey(ct.getCase())) { LOG.warn("Sample found in more than one file. Using current file to update data for sample: " + ct.getCase()); } fmiCaseTypeMap.put(ct.getCase(), ct); } } // add list of cases to the execution context LOG.info("Adding " + fmiCaseTypeMap.size() + " cases to execution context."); chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext() .put("fmiCaseTypeMap", fmiCaseTypeMap); return RepeatStatus.FINISHED; }
From source file:org.emonocot.harvest.common.ParameterConvertingTasklet.java
public final RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) throws Exception { Map<String, String> defaultValuesMap = new HashMap<String, String>(); if (this.defaultValues != null) { String values = defaultValues.substring(1, this.defaultValues.length() - 1); for (String defaultValue : values.split(",")) { if (defaultValue.indexOf("=") > -1) { String field = defaultValue.substring(0, defaultValue.indexOf("=")); String value = defaultValue.substring(defaultValue.indexOf("=") + 1, defaultValue.length()); defaultValuesMap.put(field, value); }/*from www. j a v a2s .c om*/ } } chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext() .put(defaultValuesKey, defaultValuesMap); logger.debug("SETTING " + defaultValuesKey + " as " + defaultValuesMap); String names = fieldNames.substring(1, this.fieldNames.length() - 1); String[] fieldNamesArray = names.split(","); chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put(fieldNamesKey, fieldNamesArray); logger.debug("SETTING " + fieldNamesKey + " as " + Arrays.toString(fieldNamesArray)); return RepeatStatus.FINISHED; }
From source file:org.ohdsi.webapi.feasibility.PerformFeasibilityTasklet.java
@Override public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) throws Exception { Date startTime = Calendar.getInstance().getTime(); Map<String, Object> jobParams = chunkContext.getStepContext().getJobParameters(); Integer studyId = Integer.valueOf(jobParams.get("study_id").toString()); Integer sourceId = Integer.valueOf(jobParams.get("source_id").toString()); boolean isValid = false; DefaultTransactionDefinition requresNewTx = new DefaultTransactionDefinition(); requresNewTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus initStatus = this.transactionTemplate.getTransactionManager() .getTransaction(requresNewTx); FeasibilityStudy study = this.feasibilityStudyRepository.findOne(studyId); CohortDefinition resultDef = study.getResultRule(); if (resultDef != null) { CohortGenerationInfo resultInfo = findCohortGenerationInfoBySourceId(resultDef.getGenerationInfoList(), sourceId);//from w w w . j a va 2 s .com resultInfo.setIsValid(false).setStatus(GenerationStatus.RUNNING).setStartTime(startTime) .setExecutionDuration(null); } StudyGenerationInfo studyInfo = findStudyGenerationInfoBySourceId(study.getStudyGenerationInfoList(), sourceId); studyInfo.setIsValid(false); studyInfo.setStartTime(startTime); studyInfo.setStatus(GenerationStatus.RUNNING); this.feasibilityStudyRepository.save(study); this.transactionTemplate.getTransactionManager().commit(initStatus); try { final int[] ret = this.transactionTemplate.execute(new TransactionCallback<int[]>() { @Override public int[] doInTransaction(final TransactionStatus status) { return doTask(chunkContext); } }); log.debug("Update count: " + ret.length); isValid = true; } catch (final TransactionException e) { isValid = false; log.error(e.getMessage(), e); throw e;//FAIL job status } finally { TransactionStatus completeStatus = this.transactionTemplate.getTransactionManager() .getTransaction(requresNewTx); Date endTime = Calendar.getInstance().getTime(); study = this.feasibilityStudyRepository.findOne(studyId); resultDef = study.getResultRule(); if (resultDef != null) { CohortGenerationInfo resultInfo = findCohortGenerationInfoBySourceId( resultDef.getGenerationInfoList(), sourceId); resultInfo = findCohortGenerationInfoBySourceId(resultDef.getGenerationInfoList(), sourceId); resultInfo.setIsValid(isValid); resultInfo.setExecutionDuration(new Integer((int) (endTime.getTime() - startTime.getTime()))); resultInfo.setStatus(GenerationStatus.COMPLETE); } studyInfo = findStudyGenerationInfoBySourceId(study.getStudyGenerationInfoList(), sourceId); studyInfo.setIsValid(isValid); studyInfo.setExecutionDuration(new Integer((int) (endTime.getTime() - startTime.getTime()))); studyInfo.setStatus(GenerationStatus.COMPLETE); this.feasibilityStudyRepository.save(study); this.transactionTemplate.getTransactionManager().commit(completeStatus); } return RepeatStatus.FINISHED; }
From source file:org.ohdsi.webapi.ircalc.PerformAnalysisTasklet.java
private int[] doTask(ChunkContext chunkContext) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> jobParams = chunkContext.getStepContext().getJobParameters(); Integer analysisId = Integer.valueOf(jobParams.get("analysis_id").toString()); int[] result = null; try {/* www .j av a 2 s . c o m*/ String sessionId = SessionUtils.sessionId(); IncidenceRateAnalysis analysis = this.incidenceRateAnalysisRepository.findOne(analysisId); IncidenceRateAnalysisExpression expression = mapper.readValue(analysis.getDetails().getExpression(), IncidenceRateAnalysisExpression.class); IRAnalysisQueryBuilder.BuildExpressionQueryOptions options = new IRAnalysisQueryBuilder.BuildExpressionQueryOptions(); options.cdmSchema = jobParams.get("cdm_database_schema").toString(); options.resultsSchema = jobParams.get("results_database_schema").toString(); String deleteSql = String.format("DELETE FROM %s.ir_strata WHERE analysis_id = %d", options.resultsSchema, analysisId); this.jdbcTemplate.update(deleteSql); String insertSql = StringUtils.replace( "INSERT INTO @results_schema.ir_strata (analysis_id, strata_sequence, name, description) VALUES (?,?,?,?)", "@results_schema", options.resultsSchema); insertSql = SqlTranslate.translateSql(insertSql, "sql server", jobParams.get("target_dialect").toString(), sessionId, null); List<StratifyRule> strataRules = expression.strata; for (int i = 0; i < strataRules.size(); i++) { StratifyRule r = strataRules.get(i); this.jdbcTemplate.update(insertSql, new Object[] { analysisId, i, r.name, r.description }); } String expressionSql = analysisQueryBuilder.buildAnalysisQuery(analysis, options); String translatedSql = SqlTranslate.translateSql(expressionSql, "sql server", jobParams.get("target_dialect").toString(), sessionId, null); String[] sqlStatements = SqlSplit.splitSql(translatedSql); result = PerformAnalysisTasklet.this.jdbcTemplate.batchUpdate(sqlStatements); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.ohdsi.webapi.ircalc.PerformAnalysisTasklet.java
@Override public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) throws Exception { Date startTime = Calendar.getInstance().getTime(); Map<String, Object> jobParams = chunkContext.getStepContext().getJobParameters(); Integer analysisId = Integer.valueOf(jobParams.get("analysis_id").toString()); Integer sourceId = Integer.valueOf(jobParams.get("source_id").toString()); boolean isValid = false; String statusMessage = "OK"; DefaultTransactionDefinition requresNewTx = new DefaultTransactionDefinition(); requresNewTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus initStatus = this.transactionTemplate.getTransactionManager() .getTransaction(requresNewTx); IncidenceRateAnalysis analysis = this.incidenceRateAnalysisRepository.findOne(analysisId); ExecutionInfo analysisInfo = findExecutionInfoBySourceId(analysis.getExecutionInfoList(), sourceId); analysisInfo.setIsValid(false);/* w ww. j av a2s. c o m*/ analysisInfo.setStartTime(startTime); analysisInfo.setStatus(GenerationStatus.RUNNING); this.incidenceRateAnalysisRepository.save(analysis); this.transactionTemplate.getTransactionManager().commit(initStatus); try { final int[] ret = this.transactionTemplate.execute(new TransactionCallback<int[]>() { @Override public int[] doInTransaction(final TransactionStatus status) { return doTask(chunkContext); } }); log.debug("Update count: " + ret.length); isValid = true; } catch (final Exception e) { isValid = false; statusMessage = e.getMessage(); log.error(e.getMessage(), e); throw e;//FAIL job status } finally { TransactionStatus completeStatus = this.transactionTemplate.getTransactionManager() .getTransaction(requresNewTx); Date endTime = Calendar.getInstance().getTime(); analysis = this.incidenceRateAnalysisRepository.findOne(analysisId); analysisInfo = findExecutionInfoBySourceId(analysis.getExecutionInfoList(), sourceId); analysisInfo.setIsValid(isValid); analysisInfo.setExecutionDuration((int) (endTime.getTime() - startTime.getTime())); analysisInfo.setStatus(GenerationStatus.COMPLETE); analysisInfo .setMessage(statusMessage.substring(0, Math.min(MAX_MESSAGE_LENGTH, statusMessage.length()))); this.incidenceRateAnalysisRepository.save(analysis); this.transactionTemplate.getTransactionManager().commit(completeStatus); } return RepeatStatus.FINISHED; }
From source file:org.pepstock.jem.springbatch.tasks.JemTasklet.java
/** * Is called by SpringBatch framework to execute business logic.<br> * Prepares datasets (and the files and resources) which could be used from * implementation of this class.<br> * Loads JNDI context so all resources could be used by their name, defined * in JCL.// w w w .ja v a 2s . c o m * * @param stepContribution step contribution, passed by SpringBatch core * @param chunkContext chunk context, passed by SpringBatch core * @return always the status returned by abstract method * <code>executeByJem</code> * @throws SpringBatchException if a error occurs */ @Override public final RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws SpringBatchException { LogAppl.getInstance(); // this boolean is necessary to understand if I have an exception // before calling the main class boolean isExecutionStarted = false; boolean isAbended = false; SpringBatchSecurityManager batchSM = (SpringBatchSecurityManager) System.getSecurityManager(); batchSM.setInternalAction(true); RepeatStatus status = null; // extract stepContext because the step name is necessary StepContext stepContext = chunkContext.getStepContext(); List<DataDescriptionImpl> dataDescriptionImplList = ImplementationsContainer.getInstance() .getDataDescriptionsByItem(stepContext.getStepName()); // new initial context for JNDI InitialContext ic = null; try { ic = ContextUtils.getContext(); // scans all datasource passed for (DataSource source : dataSourceList) { // checks if datasource is well defined if (source.getResource() == null) { throw new SpringBatchException(SpringBatchMessage.JEMS016E); } else if (source.getName() == null) { // if name is missing, it uses the same string // used to define the resource source.setName(source.getResource()); } // gets the RMi object to get resources CommonResourcer resourcer = InitiatorManager.getCommonResourcer(); // lookups by RMI for the database Resource res = resourcer.lookup(JobId.VALUE, source.getResource()); if (!batchSM.checkResource(res)) { throw new SpringBatchException(SpringBatchMessage.JEMS017E, res.toString()); } // all properties create all StringRefAddrs necessary Map<String, ResourceProperty> properties = res.getProperties(); // scans all properteis set by JCL for (Property property : source.getProperties()) { if (property.isCustom()) { if (res.getCustomProperties() == null) { res.setCustomProperties(new HashMap<String, String>()); } if (!res.getCustomProperties().containsKey(property.getName())) { res.getCustomProperties().put(property.getName(), property.getValue()); } else { throw new SpringBatchException(SpringBatchMessage.JEMS018E, property.getName(), res); } } else { // if a key is defined FINAL, throw an exception for (ResourceProperty resProperty : properties.values()) { if (resProperty.getName().equalsIgnoreCase(property.getName()) && !resProperty.isOverride()) { throw new SpringBatchException(SpringBatchMessage.JEMS018E, property.getName(), res); } } ResourcePropertiesUtil.addProperty(res, property.getName(), property.getValue()); } } // creates a JNDI reference Reference ref = getReference(resourcer, res, source, dataDescriptionImplList); // loads all properties into RefAddr for (ResourceProperty property : properties.values()) { ref.add(new StringRefAddr(property.getName(), replaceProperties(property.getValue()))); } // loads custom properties in a string format if (res.getCustomProperties() != null && !res.getCustomProperties().isEmpty()) { // loads all entries and substitute variables for (Entry<String, String> entry : res.getCustomProperties().entrySet()) { String value = replaceProperties(entry.getValue()); entry.setValue(value); } // adds to reference ref.add(new StringRefAddr(CommonKeys.RESOURCE_CUSTOM_PROPERTIES, res.getCustomPropertiesString())); } // binds the object with format {type]/[name] LogAppl.getInstance().emit(SpringBatchMessage.JEMS024I, res); ic.rebind(source.getName(), ref); } // check if I have resources which must be locked if (!dataDescriptionImplList.isEmpty()) { // binds all data description impl to JNDI context for (DataDescriptionImpl ddImpl : dataDescriptionImplList) { // create reference for JNDI access Reference reference = new DataStreamReference(); // load GDG information, solving the real name of relative // position GDGManager.load(ddImpl); // serialize data description object in XML format XStream xstream = new XStream(); String xml = xstream.toXML(ddImpl); // add string xml reference reference.add(new StringRefAddr(StringRefAddrKeys.DATASTREAMS_KEY, xml)); LogAppl.getInstance().emit(SpringBatchMessage.JEMS023I, ddImpl); // bind resource using data description name ic.rebind(ddImpl.getName(), reference); } } // execute business logic // executes the java class defined in JCL // setting the boolean to TRUE SetFields.applyByAnnotation(this); isExecutionStarted = true; batchSM.setInternalAction(false); status = this.run(stepContribution, chunkContext); } catch (NamingException e) { isAbended = true; throw new SpringBatchException(SpringBatchMessage.JEMS043E, e); } catch (RemoteException e) { isAbended = true; throw new SpringBatchException(SpringBatchMessage.JEMS045E, e, this.getClass().getName(), e.getMessage()); } catch (IOException e) { isAbended = true; throw new SpringBatchException(SpringBatchMessage.JEMS044E, e, e.getMessage()); } catch (Exception e) { isAbended = true; throw new SpringBatchException(SpringBatchMessage.JEMS045E, e, this.getClass().getName(), e.getMessage()); } finally { batchSM.setInternalAction(true); if (!dataDescriptionImplList.isEmpty()) { StringBuilder exceptions = new StringBuilder(); // scans data descriptions for (DataDescriptionImpl ddImpl : dataDescriptionImplList) { try { // commit the GDG index in the root // if an exception, write on standard output of job // only if execution started if (isExecutionStarted) { GDGManager.store(ddImpl); } } catch (IOException e) { // ignore LogAppl.getInstance().ignore(e.getMessage(), e); LogAppl.getInstance().emit(SpringBatchMessage.JEMS025E, e.getMessage()); if (exceptions.length() == 0) { exceptions.append(e.getMessage()); } else { exceptions.append(e.getMessage()).append("\n"); } } // unbinds all data sources try { ic.unbind(ddImpl.getName()); } catch (NamingException e) { // ignore LogAppl.getInstance().ignore(e.getMessage(), e); LogAppl.getInstance().emit(SpringBatchMessage.JEMS047E, e.getMessage()); } if (exceptions.length() > 0 && !isAbended) { LogAppl.getInstance().emit(SpringBatchMessage.JEMS025E, StringUtils.center("ATTENTION", 40, "-")); LogAppl.getInstance().emit(SpringBatchMessage.JEMS025E, exceptions.toString()); } } } for (DataSource source : dataSourceList) { if (source.getName() != null) { // unbinds all resources try { ic.unbind(source.getName()); } catch (NamingException e) { // ignore LogAppl.getInstance().ignore(e.getMessage(), e); LogAppl.getInstance().emit(SpringBatchMessage.JEMS047E, e.getMessage()); } } } batchSM.setInternalAction(false); } return status; }
From source file:org.springframework.batch.core.step.tasklet.SystemCommandTasklet.java
/** * Execute system command and map its exit code to {@link ExitStatus} using * {@link SystemProcessExitCodeMapper}.// ww w . j a v a 2 s.c o m */ @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { FutureTask<Integer> systemCommandTask = new FutureTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { Process process = Runtime.getRuntime().exec(command, environmentParams, workingDirectory); return process.waitFor(); } }); long t0 = System.currentTimeMillis(); taskExecutor.execute(systemCommandTask); while (true) { Thread.sleep(checkInterval);//moved to the end of the logic if (stoppable) { JobExecution jobExecution = jobExplorer .getJobExecution(chunkContext.getStepContext().getStepExecution().getJobExecutionId()); if (jobExecution.isStopping()) { stopped = true; } } if (systemCommandTask.isDone()) { contribution.setExitStatus(systemProcessExitCodeMapper.getExitStatus(systemCommandTask.get())); return RepeatStatus.FINISHED; } else if (System.currentTimeMillis() - t0 > timeout) { systemCommandTask.cancel(interruptOnCancel); throw new SystemCommandException("Execution of system command did not finish within the timeout"); } else if (execution.isTerminateOnly()) { systemCommandTask.cancel(interruptOnCancel); throw new JobInterruptedException( "Job interrupted while executing system command '" + command + "'"); } else if (stopped) { systemCommandTask.cancel(interruptOnCancel); contribution.setExitStatus(ExitStatus.STOPPED); return RepeatStatus.FINISHED; } } }
From source file:org.springframework.batch.core.step.tasklet.TaskletStep.java
/** * Process the step and update its context so that progress can be monitored * by the caller. The step is broken down into chunks, each one executing in * a transaction. The step and its execution and execution context are all * given an up to date {@link BatchStatus}, and the {@link JobRepository} is * used to store the result. Various reporting information are also added to * the current context governing the step execution, which would normally be * available to the caller through the step's {@link ExecutionContext}.<br> * * @throws JobInterruptedException if the step or a chunk is interrupted * @throws RuntimeException if there is an exception during a chunk * execution/*from w ww .j a v a2 s . c o m*/ * */ @Override protected void doExecute(StepExecution stepExecution) throws Exception { stepExecution.getExecutionContext().put(TASKLET_TYPE_KEY, tasklet.getClass().getName()); stepExecution.getExecutionContext().put(STEP_TYPE_KEY, this.getClass().getName()); stream.update(stepExecution.getExecutionContext()); getJobRepository().updateExecutionContext(stepExecution); // Shared semaphore per step execution, so other step executions can run // in parallel without needing the lock final Semaphore semaphore = createSemaphore(); stepOperations.iterate(new StepContextRepeatCallback(stepExecution) { @Override public RepeatStatus doInChunkContext(RepeatContext repeatContext, ChunkContext chunkContext) throws Exception { StepExecution stepExecution = chunkContext.getStepContext().getStepExecution(); // Before starting a new transaction, check for // interruption. interruptionPolicy.checkInterrupted(stepExecution); RepeatStatus result; try { result = new TransactionTemplate(transactionManager, transactionAttribute) .execute(new ChunkTransactionCallback(chunkContext, semaphore)); } catch (UncheckedTransactionException e) { // Allow checked exceptions to be thrown inside callback throw (Exception) e.getCause(); } chunkListener.afterChunk(chunkContext); // Check for interruption after transaction as well, so that // the interrupted exception is correctly propagated up to // caller interruptionPolicy.checkInterrupted(stepExecution); return result; } }); }