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

Spring batch Spring Batch FlatFileItemWriter-如何使用stepExecution.jobId生成文件名,spring-batch,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

我有一个
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>
    ....
....
知道在这种情况下如何正确引用
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>

后期绑定支持的名称有:

  • {jobParameters}
  • {jobExecutionContext}
  • {stepExecutionContext}
如果无法直接访问
jobId
,请查看

另外,
资源
可以作为

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

文件://${csv.file}{jobExecutionContext['jobId']}
因为正确的资源类型是使用转换器创建的。

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

通过SpEL表达式提供对后期绑定的StepExecution的访问

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