Example usage for org.springframework.batch.core.scope.context ChunkContext getStepContext

List of usage examples for org.springframework.batch.core.scope.context ChunkContext getStepContext

Introduction

In this page you can find the example usage for org.springframework.batch.core.scope.context ChunkContext getStepContext.

Prototype

public StepContext getStepContext() 

Source Link

Usage

From source file:org.springframework.batch.integration.x.RemoteFileToHadoopTasklet.java

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    final String filePath = chunkContext.getStepContext().getStepExecution().getExecutionContext()
            .getString("filePath");
    Assert.notNull(filePath);/*from ww w  . j  a v  a2 s  .c  o m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Transferring " + filePath + " to HDFS");
    }
    boolean result = this.template.get(filePath, new InputStreamCallback() {

        @Override
        public void doWithInputStream(InputStream stream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(configuration,
                    new Path(hdfsDirectory + filePath), null);
            byte[] buff = new byte[1024];
            int len;
            while ((len = stream.read(buff)) > 0) {
                if (len == buff.length) {
                    writer.write(buff);
                } else {
                    writer.write(Arrays.copyOf(buff, len));
                }
            }
            writer.close();
        }

    });
    if (!result) {
        throw new MessagingException("Error during file transfer");
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Transferred " + filePath + " to HDFS");
        }
        return RepeatStatus.FINISHED;
    }
}

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

/**
 * Executes the task as specified by the taskName with the associated
 * properties and arguments./*from   www  .java 2s  .  c  o m*/
 * @param contribution mutable state to be passed back to update the current step execution
 * @param chunkContext contains the task-execution-id used by the listener.
 * @return Repeat status of FINISHED.
 */
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    String tmpTaskName = this.taskName.substring(0, this.taskName.lastIndexOf('_'));

    List<String> args = this.arguments;

    ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution()
            .getExecutionContext();
    if (stepExecutionContext.containsKey("task-arguments")) {
        args = (List<String>) stepExecutionContext.get("task-arguments");
    }

    long executionId = this.taskOperations.launch(tmpTaskName, this.properties, args);

    stepExecutionContext.put("task-execution-id", executionId);
    stepExecutionContext.put("task-arguments", args);

    if (!waitForTaskToComplete(executionId)) {
        throw new TaskExecutionTimeoutException(
                String.format("Timeout occurred while processing task with Execution Id %s", executionId));
    }
    return RepeatStatus.FINISHED;
}

From source file:org.springframework.xd.dirt.plugins.job.support.listener.SimpleXdChunkListener.java

private ChunkContextInfo convertChunkContext(ChunkContext context) {

    final ChunkContextInfo chunkContextInfo = new ChunkContextInfo();
    chunkContextInfo.setComplete(context.isComplete());
    chunkContextInfo.setStepExecution(context.getStepContext().getStepExecution());

    final String[] attributeNames = context.attributeNames();

    for (String attributeName : attributeNames) {
        final Object attribute = context.getAttribute(attributeName);
        chunkContextInfo.getAttributes().put(attributeName, attribute);
    }/* w  w w .  ja v  a  2  s  . co m*/

    return chunkContextInfo;
}

From source file:org.springframework.xd.shell.command.SimpleTasklet.java

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    logger.info(//from   w  w  w  . j av a  2 s. c o  m
            "Procssing partition, foo=" + chunkContext.getStepContext().getStepExecutionContext().get("foo"));
    return RepeatStatus.FINISHED;
}