gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:09:31 32 4
gpt4 key购买 nike

我有这个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其中添加了 jobIdExecutionContext
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>

最佳答案

后期绑定(bind)支持的名称是:

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

  • 如果 jobId不能直接访问,看 this question .

    另外, resource可以直接注入(inject)
    <property name="resource">
    <value>file://${csv.file}_#{jobExecutionContext['jobId']}</value>
    </property>

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

    关于spring-batch - Spring Batch FlatFileItemWriter - 如何使用 stepExecution.jobId 生成文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21549951/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com