想要在没有自定义编写器的情况下获得弹簧批量页脚的总行数(委托模式)

时间:2017-08-07 08:55:26

标签: spring-batch footer rowcount

这是我的页脚课: -

public class SummaryFooterCallback extends StepExecutionListenerSupport implements FlatFileFooterCallback{

    private StepExecution stepExecution;

    @Override
    public void writeFooter(Writer writer) throws IOException {
        writer.write("footer - number of items written: " + stepExecution.getWriteCount());
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }


}

这是我的xml: -

<bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter"> <property name="resource" ref="outputResource" /> <property name="lineAggregator"> <bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" /> </property> <property name="headerCallback" ref="headerCopier" /> <property name="footerCallback" ref="footerCallback" /> </bean> <bean id="footerCallback" class="org.springframework.batch.sample.support.SummaryFooterCallback"/>

在stepExecution.getWriteCount()失败,返回值为null。

不,我没有在步骤中将回调注册为侦听器。我是Java和Spring Batch的新手,参考了您的Pro Spring Batch一书,但无法获得所分配任务的解决方案。

1 个答案:

答案 0 :(得分:0)

您需要在scope step中设置编写者。这里有一个基于java的配置,对我有用。

 public HashSet<SelectListItem> GetAllStates()
    {
        var states = new HashSet<SelectListItem>()
        {
            new SelectListItem() { ItemValue = "AL", Name = "Alabama" },
            new SelectListItem() { ItemValue = "AK", Name = "Alaska" },
            new SelectListItem() { ItemValue = "AZ", Name = "Arizona" }, 
            new SelectListItem() { ItemValue = "AR", Name = "Arkansas" },
            new SelectListItem() { ItemValue = "CA", Name = "California" },
            new SelectListItem() { ItemValue = "CO", Name = "Colorado" },
            new SelectListItem() { ItemValue = "CT", Name = "Connecticut" },
            new SelectListItem() { ItemValue = "DE", Name = "Delaware" },
            new SelectListItem() { ItemValue = "DC", Name = "District Of Columbia" },
            new SelectListItem() { ItemValue = "FL", Name = "Florida" },
            new SelectListItem() { ItemValue = "GA", Name = "Georgia" },
            new SelectListItem() { ItemValue = "HI", Name = "Hawaii" },
            new SelectListItem() { ItemValue = "ID", Name = "Idaho" },
            new SelectListItem() { ItemValue = "IL", Name = "Illinois" },
            new SelectListItem() { ItemValue = "IN", Name = "Indiana" },
            new SelectListItem() { ItemValue = "IA", Name = "Iowa" },
            new SelectListItem() { ItemValue = "KS", Name = "Kansas" },
            new SelectListItem() { ItemValue = "KY", Name = "Kentucky" },
            new SelectListItem() { ItemValue = "LA", Name = "Louisiana" },
            new SelectListItem() { ItemValue = "ME", Name = "Maine" },
            new SelectListItem() { ItemValue = "MD", Name = "Maryland" },
            new SelectListItem() { ItemValue = "MA", Name = "Massachusetts" },
            new SelectListItem() { ItemValue = "MI", Name = "Michigan" },
            new SelectListItem() { ItemValue = "MN", Name = "Minnesota" },
            new SelectListItem() { ItemValue = "MS", Name = "Mississippi" },
            new SelectListItem() { ItemValue = "MO", Name = "Missouri" },
            new SelectListItem() { ItemValue = "MT", Name = "Montana" },
            new SelectListItem() { ItemValue = "NE", Name = "Nebraska" },
            new SelectListItem() { ItemValue = "NV", Name = "Nevada" },
            new SelectListItem() { ItemValue = "NH", Name = "New Hampshire" },
            new SelectListItem() { ItemValue = "NJ", Name = "New Jersey" },
            new SelectListItem() { ItemValue = "NM", Name = "New Mexico" },
            new SelectListItem() { ItemValue = "NY", Name = "New York" },
            new SelectListItem() { ItemValue = "NC", Name = "North Carolina"},
            new SelectListItem() { ItemValue = "ND", Name = "North Dakota"},
            new SelectListItem() { ItemValue = "OH", Name = "Ohio" },
            new SelectListItem() { ItemValue = "OK", Name = "Oklahoma" },
            new SelectListItem() { ItemValue = "OR", Name = "Oregon" },
            new SelectListItem() { ItemValue = "PA", Name = "Pennsylvania" },
            new SelectListItem() { ItemValue = "RI", Name = "Rhode Island" },
            new SelectListItem() { ItemValue = "SC", Name = "South Carolina" },
            new SelectListItem() { ItemValue = "SD", Name = "South Dakota" },
            new SelectListItem() { ItemValue = "TN", Name = "Tennessee" },
            new SelectListItem() { ItemValue = "TX", Name = "Texas" },
            new SelectListItem() { ItemValue = "UT", Name = "Utah" },
            new SelectListItem() { ItemValue = "VT", Name = "Vermont" },
            new SelectListItem() { ItemValue = "VA", Name = "Virginia" },
            new SelectListItem() { ItemValue = "WA", Name = "Washington" },
            new SelectListItem() { ItemValue = "WV", Name = "West Virginia" },
            new SelectListItem() { ItemValue = "WI", Name = "Wisconsin" },
            new SelectListItem() { ItemValue = "WY", Name = "Wyoming" }
        };

        return states;
    }