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

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

Introduction

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

Prototype


public void putString(String key, @Nullable String value) 

Source Link

Document

Adds a String value to the context.

Usage

From source file:com.cat.ic.listener.impl.OutputFileListenerMVNO.java

@BeforeStep
public void createOutputNameFromInput(StepExecution stepExecution) {
    ExecutionContext executionContext = stepExecution.getExecutionContext();
    String inputName = stepExecution.getStepName().replace(":", "-");

    if (executionContext.containsKey(inputKeyName)) {
        inputName = executionContext.getString(inputKeyName);
    }//  www  .ja  v a2  s.  com
    if (!executionContext.containsKey(outputKeyName)) {
        executionContext.putString(outputKeyName, path + FilenameUtils.getName(inputName) + ".mvno");
    }
    log.info("[" + executionContext.getString(outputKeyName) + "]");
}

From source file:com.ifeng.computing.batch.job.partitioner.RangePartitioner.java

@Override
public Map<String, ExecutionContext> partition(int gridSize) {

    Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();

    int range = 100;
    int fromId = 81289;
    int toId = range + fromId;

    for (int i = 1; i <= gridSize; i++) {
        ExecutionContext value = new ExecutionContext();

        log.debug("\nStarting : Thread" + i);
        log.debug("fromId : " + fromId);
        log.debug("toId : " + toId);

        value.putInt("fromId", fromId);
        value.putInt("toId", toId);

        // give each thread a name, thread 1,2,3
        value.putString("name", "Thread" + i);

        result.put("partition" + i, value);

        fromId = toId + 1;/*w w  w  .j a v  a2  s .c  o m*/
        toId += range;

    }

    return result;
}

From source file:egovframework.rte.bat.core.listener.EgovOutputFileListener.java

/**
 * stepExecutionContext? inputKeyName ? ? outputKeyName? put 
 * // w w  w  .j  av  a2s  .  c  o m
 * @param stepExecution
 */
@BeforeStep
public void createOutputNameFromInput(StepExecution stepExecution) {
    ExecutionContext executionContext = stepExecution.getExecutionContext();
    String inputName = stepExecution.getStepName().replace(":", "-");
    if (executionContext.containsKey(inputKeyName)) {
        inputName = executionContext.getString(inputKeyName);
    }
    if (!executionContext.containsKey(outputKeyName)) {
        executionContext.putString(outputKeyName, path + FilenameUtils.getBaseName(inputName) + ".csv");
    }
}

From source file:batch.demo.job.FlatFilePartitioner.java

/**
 * Creates a standard {@link ExecutionContext} with the specified parameters.
 * @param partitionName the name of the partition
 * @param startAt the number of bytes for a partition thread to skip before starting reading
 * @param itemsCount the number of items to read
 * @return the execution context (output)
 *///  www.j  a v a  2s .com
protected ExecutionContext createExecutionContext(String partitionName, long startAt, long itemsCount) {
    final ExecutionContext executionContext = new ExecutionContext();
    executionContext.putLong(startAtKeyName, startAt);
    executionContext.putLong(itemsCountKeyName, itemsCount);
    try {
        executionContext.putString(resourceKeyName, "file:" + resource.getFile().getPath());
    } catch (IOException e) {
        throw new IllegalArgumentException("File could not be located for: " + resource, e);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Added partition [" + partitionName + "] with [" + executionContext + "]");
    }
    return executionContext;
}

From source file:org.springframework.batch.core.partition.support.FlatFilePartitioner.java

/**
 * Creates a standard {@link ExecutionContext} with the specified parameters.
 * @param partitionName the name of the partition
 * @param startAt the number of bytes for a partition thread to skip before starting reading
 * @param itemsCount the number of items to read
 * @return the execution context (output)
 *///from w  w  w. j ava  2s.c  o  m
protected ExecutionContext createExecutionContext(String partitionName, long startAt, long itemsCount,
        long previousItemsCount) {
    final ExecutionContext executionContext = new ExecutionContext();
    executionContext.putLong(startAtKeyName, startAt);
    executionContext.putLong(itemsCountKeyName, itemsCount);
    executionContext.putLong(previousItemsCountKeyName, previousItemsCount);
    try {
        executionContext.putString(resourceKeyName, "file:" + resource.getFile().getPath());
    } catch (IOException e) {
        throw new IllegalArgumentException("File could not be located for: " + resource, e);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Added partition [" + partitionName + "] with [" + executionContext + "]");
    }
    return executionContext;
}

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

/**
 * Partition a database table assuming that the data in the column specified
 * are uniformly distributed. The execution context values will have keys
 * <code>minValue</code> and <code>maxValue</code> specifying the range of
 * values to consider in each partition.
 *
 * @see Partitioner#partition(int)/*from   w w w .  j  a v a 2s. com*/
 */
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
    StringBuilder incrementalClause = new StringBuilder();
    Map<String, ExecutionContext> result = new HashMap<>();

    if (!StringUtils.hasText(checkColumn) && !StringUtils.hasText(column)) {
        ExecutionContext value = new ExecutionContext();
        value.put("partClause", "");
        result.put("partition0", value);
        value.put("partSuffix", "");
    } else {
        if (StringUtils.hasText(checkColumn)) {
            incrementalClause.append(checkColumn).append(" > ").append(this.incrementalMin);
        }

        long targetSize = (this.partitionMax - this.partitionMin) / partitions + 1;

        int number = 0;
        long start = this.partitionMin;
        long end = start + targetSize - 1;

        while (start >= 0 && start <= this.partitionMax) {
            ExecutionContext value = new ExecutionContext();
            result.put("partition" + number, value);

            if (end >= this.partitionMax) {
                end = this.partitionMax;
            }

            if (StringUtils.hasText(checkColumn)) {
                value.putString("partClause", String.format("WHERE (%s BETWEEN %s AND %s) AND %s", column,
                        start, end, incrementalClause.toString()));
            } else {
                value.putString("partClause",
                        String.format("WHERE (%s BETWEEN %s AND %s)", column, start, end));
            }

            value.putString("partSuffix", "-p" + number);
            start += targetSize;
            end += targetSize;
            number++;

            log.debug("Current ExecutionContext = " + value);
        }
    }

    return result;
}