简体   繁体   中英

Spring Batch empty file using FlatFileItemReader xML

I'm trying to validate if an input file is empty or has data. I'm working with spring batch thru XML beans config, my actual config is like:

<!-- READERS -->
    <bean id="mgbfItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="strict" value="false" />
        <!-- Read a file -->
        <property name="resource" value="file:///fichtemcomp/datent/#{processDataClass.fileName}" />
        <property name="linesToSkip" value="10" />
        <property name="encoding" value="UTF-8" />

        <!-- Mapper -->
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <!-- split it -->
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="|" />
                        <!-- Valores del layout -->
                        <property name="names" value="name,secondName,lastName" />
                    </bean>
                </property>

                <!-- Wrapper -->
                <property name="fieldSetMapper">
                    <bean class="com.bbva.mgbf.batch.listeners.ScholarItemReader">
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    <!-- END READERS -->

In my class Scholar ItemReader, I have something like:

@Override
    public Map<String, Object> mapFieldSet(FieldSet fieldSet) throws BindException {
        LOGGER.info("Reading all items \t {}", fieldSet);
        LOGGER.info("FieldCount \t {}", fieldSet.getFieldCount());

        Map<String, Object> dataIn = new HashMap<>(65);
        if (fieldSet.getFieldCount() > 0 && !fieldSet.readString(0).trim().isEmpty()) {
            LOGGER.info("in if ");

            for (int i = 0; i < 65; i++) {
                String key = "" + i;
                String value = fieldSet.readString(i);
                dataIn.put(key, value);
            }
        }else{
            dataIn.put("empty", "emptyFile");
        }
        return dataIn;
}

But it never entered in the "else " declaration, it just ends the process.

Anyone knows if there are another way to validate if file is empty or not?

fieldSetMapper is called when the reader has already read a line, so trying to check if the file is empty in the mapper is too late in the process.

I'm trying to validate if an input file is empty or has data

I would use a pre-validation tasklet step that checks if the file is empty or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM