Spring Batch FlatFileItemWriter - 如何使用stepExecution.jobId生成文件名

时间:2014-02-04 10:33:28

标签: spring-batch

我有这个FileWriter,我正在尝试将当前的作业ID附加到生成的文件名中。

<bean id="csvFileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    <property name="resource">
        <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg type="java.lang.String">
                <value>${csv.file}_#{stepExecution.jobExecution.jobId}</value>
            </constructor-arg>
        </bean>
    </property>
    <property name="lineAggregator">
        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <property name="delimiter">
                <util:constant
                    static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_COMMA"/>
            </property>
            <property name="fieldExtractor">
                <bean class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" />
            </property>
        </bean>
    </property>
    ....
....

但它正在用

轰炸
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'stepExecution' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:208)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:72)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:52)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:97)
    at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:82)
    at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:1)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:139)
    ... 45 more

在这种情况下我知道如何正确引用jobId吗?

更新:添加工作解决方案

我实施了JobExecutionListener,将jobId添加到ExecutionContext

public class MyExecutionListener implements JobExecutionListener {

    public void beforeJob(JobExecution jobExecution) {
        long jobId = jobExecution.getJobId();
        jobExecution.getExecutionContext().put("jobId",jobId);
        jobExecution.getExecutionContext().put("date",date);
    }

    public void afterJob(JobExecution jobExecution) {

将侦听器注册到批处理作业

<batch:job id="batchJob">
    <batch:listeners>
        <batch:listener ref="myExecutionListener"/>
    </batch:listeners>

最后,CSV编写器更新为

<bean id="fundAssetCsvFileWriter"
    class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    <property name="resource">
        <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg value="${csv.file.name}_#{jobExecutionContext['date']}_#{jobExecutionContext['jobId']}.csv" type="java.lang.String"/>
        </bean>

2 个答案:

答案 0 :(得分:5)

late-bindig支持的名称是:

  • #{jobParameters}
  • #{jobExecutionContext}
  • #{stepExecutionContext}

如果无法直接访问jobId,请查看this question

此外,resource可以直接注入

<property name="resource">
  <value>file://${csv.file}_#{jobExecutionContext['jobId']}</value>
</property>

因为使用转换器创建了正确的资源类型。

答案 1 :(得分:0)

#{stepExecution.jobExecution.id}#{stepExecution.jobExecutionId}应该可以使用。

StepContext 提供对StepExecution的访问,以便通过SpEL表达式进行后期绑定。