在ItemReader中设置属性名称和列。春季批

时间:2018-08-08 12:08:39

标签: java spring

我尝试在运行时设置ItemReader的属性,但是我不知道该怎么做。因此,我想在每个块之后传递属性值,以从文件中读取每个字符串,该文件由许多类型的字符串组成。

这是读者:

<bean id="itemReader" class="com.genericjob.reader.MyReader" scope="step">
    <property name="encoding" value="UTF-8"/>
    <property name="engineDao" ref="engineDao"/>
    <property name="resource" value="file:#{stepExecutionContext['fileSource']}"/>

    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
                    <property name="names" value="#{stepExecutionContext['names']}"/>
                    <property name="columns" value="#{stepExecutionContext['columns']}"/>
                </bean>
            </property>

            <property name="fieldSetMapper">
                <bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper"/>
            </property>
        </bean>
    </property>
</bean>

这是我使用的阅读器:

public class MyReader extends FlatFileItemReader {
    private EngineDao engineDao;

    public EngineDao getEngineDao() {
        return engineDao;
    }

    public void setEngineDao(EngineDao engineDao) {
        this.engineDao = engineDao;
    }

    public String pathToFile() {
        String path = engineDao.getDataByNativeQuery(String.class, ForQuery.GET_FILE_FOLDER, new HashMap<>()).get(0);
        return path;
    }

    @BeforeStep
    public void saveStepExecution(StepExecution stepExecution) {
        for (Map.Entry<String, Object> entry : setParameters().entrySet()) {
             stepExecution.getExecutionContext().put(entry.getKey(), entry.getValue());
        }
    }

    public Map<String, Object> setParameters() {
        Map<String, Object> parameters = new HashMap<>();

        List<String> distinctIdList = engineDao.getDataByNativeQuery(String.class, ForQuery.GET_FILE_FOLDER, parameters);
        String fileFolder = distinctIdList.get(0);

        List<String> columnsList = engineDao.getDataByNativeQuery(String.class, ForQuery.GET_NAMES, parameters);
        List<Long> rangesList = engineDao.getDataByNativeQuery(Long.class, ForQuery.GET_RANGES, parameters);

        String columns = getStringParam(columnsList);
        String ranges = getStringParam(rangesList);
        ranges = ranges.replace("46", "");

        parameters.put("fileSource", fileFolder);
        parameters.put("names", columns);
        parameters.put("columns", ranges);

        return parameters;
    }

    private static <T> String getStringParam(List<T> columnsList) {
        StringBuilder columns = new StringBuilder();

        for (int i = 0; i < columnsList.size(); i++) {
            columns.append(columnsList.get(i));

            if (i < columnsList.size() - 1) {
                columns.append(", ");
            }
        }

        return columns.toString();
    }
}

所以,主要问题是:如何在每次提交之前传递属性(名称,列,文件来源)的值。

0 个答案:

没有答案