Example usage for org.springframework.batch.core StepExecution getExecutionContext

List of usage examples for org.springframework.batch.core StepExecution getExecutionContext

Introduction

In this page you can find the example usage for org.springframework.batch.core StepExecution getExecutionContext.

Prototype

public ExecutionContext getExecutionContext() 

Source Link

Document

Returns the ExecutionContext for this execution

Usage

From source file:org.springframework.batch.sample.common.SkipCheckingListener.java

@BeforeStep
public void saveStepName(StepExecution stepExecution) {
    stepExecution.getExecutionContext().put("stepName", stepExecution.getStepName());
}

From source file:org.springframework.cloud.task.app.composedtaskrunner.ComposedTaskStepExecutionListener.java

/**
 * If endTime for task is null then the ExitStatus will be set to  UNKNOWN.
 * If an exitMessage is returned by the TaskExecution then the exit status
 * returned will be the ExitMessage.  If no exitMessage is set for the task execution and the
 * task returns an exitCode ! = to zero an exit status of FAILED is
 * returned.  If no exit message is set and the exit code of the task is
 * zero then the ExitStatus of COMPLETED is returned.
 * @param stepExecution The stepExecution that kicked of the Task.
 * @return ExitStatus of COMPLETED else FAILED.
 *//*from   w  ww. j  a v a  2 s.c o m*/
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    ExitStatus result = ExitStatus.COMPLETED;
    logger.info(String.format("AfterStep processing for stepExecution %s", stepExecution.getStepName()));

    Long executionId = (Long) stepExecution.getExecutionContext().get("task-execution-id");
    Assert.notNull(executionId,
            "TaskLauncherTasklet did not " + "return a task-execution-id.  Check to see if task " + "exists.");

    TaskExecution resultExecution = this.taskExplorer.getTaskExecution(executionId);

    if (!StringUtils.isEmpty(resultExecution.getExitMessage())) {
        result = new ExitStatus(resultExecution.getExitMessage());
    } else if (resultExecution.getExitCode() != 0) {
        result = ExitStatus.FAILED;
    }

    logger.info(String.format("AfterStep processing complete for " + "stepExecution %s with taskExecution %s",
            stepExecution.getStepName(), executionId));
    return result;
}

From source file:org.springframework.yarn.batch.partition.AbstractBatchPartitionHandler.java

@Override
public final Collection<StepExecution> handle(StepExecutionSplitter stepSplitter, StepExecution stepExecution)
        throws Exception {

    if (log.isDebugEnabled()) {
        log.debug("partition job parameters:");
        for (Entry<String, JobParameter> entry : stepExecution.getJobParameters().getParameters().entrySet()) {
            log.debug("entry: " + entry.getKey() + " / " + entry.getValue());
        }//w ww .j  av  a  2 s .  c  o  m
    }

    Collection<StepExecution> result = new ArrayList<StepExecution>();

    Set<StepExecution> split = createStepExecutionSplits(stepSplitter, stepExecution);

    if (log.isDebugEnabled()) {
        log.debug("Created " + split.size() + " splits for stepName=" + stepName + " with parent stepExecution="
                + stepExecution);
        for (StepExecution execution : split) {
            log.debug("Splitted step execution: " + execution + " with executionContext="
                    + execution.getExecutionContext());
        }
    }

    Map<StepExecution, ContainerRequestHint> resourceRequests = createResourceRequestData(split);

    if (log.isDebugEnabled()) {
        log.debug("Resource request map size is " + resourceRequests.size());
        for (Entry<StepExecution, ContainerRequestHint> entry : resourceRequests.entrySet()) {
            log.debug("Entry stepExecution=" + entry.getKey() + " requestData=" + entry.getValue());
        }
    }

    batchAppmaster.addStepSplits(stepExecution, stepName, split, resourceRequests);

    waitCompleteState(stepExecution);

    result.addAll(batchAppmaster.getStepExecutions());
    if (log.isDebugEnabled()) {
        log.debug("Statuses of remote execution ");
        for (StepExecution execution : result) {
            log.debug("Remote step execution: " + execution);
        }
    }

    return result;
}

From source file:org.springframework.yarn.batch.partition.HdfsSplitBatchPartitionHandler.java

@Override
protected Map<StepExecution, ContainerRequestHint> createResourceRequestData(Set<StepExecution> stepExecutions)
        throws Exception {
    Map<StepExecution, ContainerRequestHint> requests = new HashMap<StepExecution, ContainerRequestHint>();

    for (StepExecution execution : stepExecutions) {
        String fileName = execution.getExecutionContext().getString("fileName");
        long splitStart = execution.getExecutionContext().getLong("splitStart");
        long splitLength = execution.getExecutionContext().getLong("splitLength");

        log.debug("Creating request data for stepExecution=" + execution + " with fileName=" + fileName
                + " splitStart=" + splitStart + " splitLength=" + splitLength);

        FileSystem fs = FileSystem.get(configuration);
        Path path = new Path(execution.getExecutionContext().getString("fileName"));

        HashSet<String> hostsSet = new HashSet<String>();

        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(path, splitStart, splitLength);
        for (BlockLocation blockLocation : fileBlockLocations) {
            for (String host : blockLocation.getHosts()) {
                hostsSet.add(host);//from   ww w. j a va  2  s.  c  o  m
            }
            log.debug("block: " + blockLocation + " topologypaths="
                    + StringUtils.arrayToCommaDelimitedString(blockLocation.getTopologyPaths()));
        }

        String[] hosts = hostsSet.toArray(new String[0]);
        String[] racks = new String[0];
        // hints only for hosts
        requests.put(execution, new ContainerRequestHint(execution, null, hosts, racks, null));
    }

    return requests;
}

From source file:simple.spring.batch.JobLauncherDetails.java

@SuppressWarnings("unchecked")
protected void executeInternal(JobExecutionContext context) {
    Map<String, Object> jobDataMap = context.getMergedJobDataMap();
    String jobName = (String) jobDataMap.get(JOB_NAME);

    log.info("Quartz trigger firing with Spring Batch jobName=" + jobName);

    // JobParameters?   
    // ?  ??  // w  ww . ja v  a2  s .  co m
    //jobDataMap.put("date", new Date());

    JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
    try {
        Job job = jobLocator.getJob(jobName);

        JobExecution jobExecution = jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
        System.out.println("<<< " + jobExecution.getExitStatus().getExitCode());

        if ("COMPLETED".equals(jobExecution.getStatus())) {
            System.out.println("COMPLETED!!");
        }

        Collection<StepExecution> collection = jobExecution.getStepExecutions();

        for (StepExecution s : collection) {
            System.out.println(s);
            ExecutionContext excutionContext = s.getExecutionContext();

        }

    } catch (JobExecutionException e) {
        // log.error("Could not execute job.", e);
    }
}

From source file:uk.ac.ebi.intact.dataexchange.dbimporter.listener.MailNotifierStepExecutionListener.java

public ExitStatus afterStep(StepExecution stepExecution) {

    SimpleMailMessage message = newSimpleMessage();
    message.setSubject("[IntAct_import] Finished step: " + stepExecution.getStepName() + " Exit status: "
            + stepExecution.getExitStatus().getExitCode());
    message.setText(stepExecution.toString() + "\n" + stepExecution.getExecutionContext());
    message.setText(stepExecution.toString() + "\n" + stepExecution.getSummary() + "\n"
            + stepExecution.getJobExecution());
    message.setTo(stepExecution.getJobParameters().getString("email.recipient"));

    //        try{
    //            mailSender.send(message);
    //        }// w  w  w . j a va 2s  . co m
    //        catch (MailException e){
    //            log.error("Impossible to send e-mail", e);
    //        }

    return stepExecution.getExitStatus();
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.exporter.listener.MailNotifierStepExecutionListener.java

public ExitStatus afterStep(StepExecution stepExecution) {

    SimpleMailMessage message = newSimpleMessage();
    message.setSubject("[IntAct_export] Finished step: " + stepExecution.getStepName() + " Exit status: "
            + stepExecution.getExitStatus().getExitCode());
    message.setText(stepExecution.toString() + "\n" + stepExecution.getExecutionContext());
    message.setText(stepExecution.toString() + "\n" + stepExecution.getSummary() + "\n"
            + stepExecution.getJobExecution());
    message.setTo(recipients);/*from   w w  w  . java  2  s.  co  m*/

    try {
        mailSender.send(message);
    } catch (MailException e) {
        log.error("Impossible to send e-mail", e);
    }

    return stepExecution.getExitStatus();
}