Example usage for org.springframework.batch.item ExecutionContext containsKey

List of usage examples for org.springframework.batch.item ExecutionContext containsKey

Introduction

In this page you can find the example usage for org.springframework.batch.item ExecutionContext containsKey.

Prototype

public boolean containsKey(String key) 

Source Link

Document

Indicates whether or not a key is represented in this context.

Usage

From source file:egovframework.rte.bat.core.item.file.EgovPartitionFlatFileItemWriter.java

/**
 * ?? open ? ?//from www.  ja v a2s  .  com
 * state ? BufferedWriter  
 * @see ItemStream#close()
 */
private void doOpen(ExecutionContext executionContext) throws ItemStreamException {

    OutputState outputState = getOutputState();

    if (executionContext.containsKey(getKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }
    try {
        outputState.initializeBufferedWriter();
        fileOpenTime = resource.getFile().lastModified();
        // ? Thread ?   ? Close ? Stream ?  ??    
        if ((fileOpenTime - fileCloseTime) / 1000.0 < 1) {

            throw new IOException("Failed to initialize writer");
        }
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
    if (outputState.lastMarkedByteOffsetPosition == 0 && !outputState.appending) {
        if (headerCallback != null) {
            try {
                headerCallback.writeHeader(outputState.outputBufferedWriter);
                outputState.write(lineSeparator);
            } catch (IOException e) {
                throw new ItemStreamException("Could not write headers.  The file may be corrupt.", e);
            }
        }
    }
}

From source file:fr.acxio.tools.agia.file.ExtendedMultiResourceItemReader.java

/**
 * Figure out which resource to start with in case of restart, open the
 * delegate and restore delegate's position in the resource.
 */// w  w w . ja v  a 2s.co  m
public void open(ExecutionContext executionContext) throws ItemStreamException {
    Assert.isTrue((resources != null) || (resourcesFactory != null),
            "Resources and ResourcesFactory must not be both null");
    try {
        if ((resources == null) && (resourcesFactory != null)) {
            Map<String, Object> aSourceParams = new HashMap<String, Object>();
            aSourceParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC, stepExecution);
            resources = resourcesFactory.getResources(aSourceParams);
            isResourcesSet = false;
        }
    } catch (ResourceCreationException e) {
        throw new ItemStreamException(e);
    }

    noInput = false;
    if ((resources == null) || (resources.length == 0)) {
        if (strict) {
            throw new IllegalStateException(
                    "No resources to read. Set strict=false if this is not an error condition.");
        } else {
            LOGGER.warn("No resources to read. Set strict=true if this should be an error condition.");
            noInput = true;
            return;
        }
    }

    Arrays.sort(resources, comparator);

    if (executionContext.containsKey(executionContextUserSupport.getKey(RESOURCE_KEY))) {
        currentResource = executionContext.getInt(executionContextUserSupport.getKey(RESOURCE_KEY));

        // context could have been saved before reading anything
        if (currentResource == -1) {
            currentResource = 0;
        }

        delegate.setResource(resources[currentResource]);
        delegate.open(executionContext);
    } else {
        currentResource = -1;
    }
}

From source file:org.emonocot.job.io.StaxEventItemWriter.java

/**
 * Open the output source.//from   ww  w  .  j  a v a  2  s .  co m
 * @param newExecutionContext Set the execution context
 * @see org.springframework.batch.item.ItemStream#open(ExecutionContext)
 */
public final void open(final ExecutionContext newExecutionContext) {

    Assert.notNull(resource, "The resource must be set");

    long startAtPosition = 0;
    boolean restarted = false;

    // if restart data is provided, restart from provided offset
    // otherwise start from beginning
    if (newExecutionContext.containsKey(getKey(RESTART_DATA_NAME))) {
        startAtPosition = newExecutionContext.getLong(getKey(RESTART_DATA_NAME));
        restarted = true;
    }

    open(startAtPosition, restarted);

    if (startAtPosition == 0) {
        try {
            if (headerCallback != null) {
                headerCallback.write(delegateEventWriter);
            }
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write headerItems", e);
        }
    }

}

From source file:org.geoserver.backuprestore.reader.CatalogMultiResourceItemReader.java

/**
 * Figure out which resource to start with in case of restart, open the delegate and restore delegate's position in the resource.
 *//*w w w. j a v a  2  s .co m*/
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
    super.open(executionContext);
    Assert.notNull(resources, "Resources must be set");

    noInput = false;
    if (resources.length == 0) {
        if (strict) {
            throw new IllegalStateException(
                    "No resources to read. Set strict=false if this is not an error condition.");
        } else {
            logger.warn("No resources to read. Set strict=true if this should be an error condition.");
            noInput = true;
            return;
        }
    }

    Arrays.sort(resources, comparator);

    if (executionContext.containsKey(getExecutionContextKey(RESOURCE_KEY))) {
        currentResource = executionContext.getInt(getExecutionContextKey(RESOURCE_KEY));

        // context could have been saved before reading anything
        if (currentResource == -1) {
            currentResource = 0;
        }

        delegate.setResource(resources[currentResource]);
        delegate.open(executionContext);
    } else {
        currentResource = -1;
    }
}

From source file:org.geoserver.backuprestore.writer.CatalogFileWriter.java

private void doOpen(ExecutionContext executionContext) throws ItemStreamException {
    OutputState outputState = getOutputState();

    if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }// ww  w  .j  ava2  s . c  om

    try {
        outputState.initializeBufferedWriter();
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
}

From source file:org.springframework.batch.item.file.FlatFileItemWriter.java

private void doOpen(ExecutionContext executionContext) throws ItemStreamException {
    OutputState outputState = getOutputState();
    if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }//from w w  w  . ja  va  2 s .  c o  m
    try {
        outputState.initializeBufferedWriter();
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
    if (outputState.lastMarkedByteOffsetPosition == 0 && !outputState.appending) {
        if (headerCallback != null) {
            try {
                headerCallback.writeHeader(outputState.outputBufferedWriter);
                outputState.write(lineSeparator);
            } catch (IOException e) {
                throw new ItemStreamException("Could not write headers.  The file may be corrupt.", e);
            }
        }
    }
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Open the output source/*w w  w .j  a  v  a 2s .  c o m*/
 * 
 * @see org.springframework.batch.item.ItemStream#open(ExecutionContext)
 */
@SuppressWarnings("unchecked")
@Override
public void open(ExecutionContext executionContext) {
    super.open(executionContext);

    Assert.notNull(resource, "The resource must be set");

    long startAtPosition = 0;

    // if restart data is provided, restart from provided offset
    // otherwise start from beginning
    if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
        startAtPosition = executionContext.getLong(getExecutionContextKey(RESTART_DATA_NAME));
        currentRecordCount = executionContext.getLong(getExecutionContextKey(WRITE_STATISTICS_NAME));
        if (executionContext.containsKey(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME))) {
            unclosedHeaderCallbackElements = (List<QName>) executionContext
                    .get(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME));
        }

        restarted = true;
        if (shouldDeleteIfEmpty && currentRecordCount == 0) {
            // previous execution deleted the output file because no items were written
            restarted = false;
            startAtPosition = 0;
        } else {
            restarted = true;
        }
    } else {
        currentRecordCount = 0;
        restarted = false;
    }

    open(startAtPosition);

    if (startAtPosition == 0) {
        try {
            if (headerCallback != null) {
                UnclosedElementCollectingEventWriter headerCallbackWriter = new UnclosedElementCollectingEventWriter(
                        delegateEventWriter);
                headerCallback.write(headerCallbackWriter);
                unclosedHeaderCallbackElements = headerCallbackWriter.getUnclosedElements();
            }
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write headerItems", e);
        }
    }

    this.initialized = true;

}

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.//w w w .j  av a  2  s .  c om
 * @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;
}