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

问题描述 投票:0回答:2

我有这个

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>
spring spring-batch
2个回答
7
投票

后期绑定支持的名称是:

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

如果无法直接访问

jobId
,请查看这个问题

此外,

resource
可以直接注入为

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

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


1
投票

#{stepExecution.jobExecution.id}
#{stepExecution.jobExecutionId}
应该可以工作。

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

© www.soinside.com 2019 - 2024. All rights reserved.