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

eme*_*ava 7 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>
    ....
....
Run Code Online (Sandbox Code Playgroud)

但它正在轰炸

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
Run Code Online (Sandbox Code Playgroud)

知道如何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) {
Run Code Online (Sandbox Code Playgroud)

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

<batch:job id="batchJob">
    <batch:listeners>
        <batch:listener ref="myExecutionListener"/>
    </batch:listeners>
Run Code Online (Sandbox Code Playgroud)

最后,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>
Run Code Online (Sandbox Code Playgroud)

Luc*_*cci 5

late-bindig支持的名称是:

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

如果jobId无法直接访问,请查看此问题.

另外,resource可以直接注入

<property name="resource">
  <value>file://${csv.file}_#{jobExecutionContext['jobId']}</value>
</property>
Run Code Online (Sandbox Code Playgroud)

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