gpt4 book ai didi

java - 如何确保 MultiResourceItemReader 在每次执行作业时刷新资源

转载 作者:搜寻专家 更新时间:2023-11-01 01:34:45 24 4
gpt4 key购买 nike

我有一个使用 spring 任务调度和批处理作业的 java 应用程序。我依靠 job 中的 MultiResourceItemReader 从目录中读取一些文件、处理它们并删除这些文件。外部进程负责定期将新文件放入该目录。但问题是,每次 job 运行时,它都会尝试读取启动应用程序时存在的相同文件资源,因此失败,因为这些资源已经消失,现在新文件已经存在.

问题是,我如何配置应用程序以便为给定的 job 的每个计划执行计算资源属性。

相关bean贴在下面:

  <bean id="multiResourceReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="file:/opt/data/*.csv" />
<property name="delegate" ref="testFlatFileItemReader" />
</bean>

<batch:job id="MyJob">
<batch:step id="readandstore">
<batch:tasklet>
<batch:chunk reader="multiResourceReader" writer="oracleItemWriter" commit-interval="10" />
</batch:tasklet>
</batch:step>
</batch:job>

<bean id="runScheduler" class="com.myapp.Scheduler">
<property name="jobLauncher" ref="jobLauncher" />
<property name="job" ref="MyJob" />
</bean>

<task:scheduled-tasks>
<task:scheduled ref="runScheduler" method="run" cron="*/30 * * * * *" />
</task:scheduled-tasks>

最佳答案

在将近一周没有收到任何回复后,我设法解决了自己发布的问题。

我删除了 multiResourceReader 的资源属性, 并添加了一个 StepListener <batch:tasklet>下的听众实现StepExecutionListener .这个监听器有两个方法,一个在step执行前调用,一个在step执行后调用。 StepListener接受名为 filePattern 的属性其值将类似于您提供给 multiResourceReader 的资源属性的值前。下面是更新后的 bean 的样子:

  <batch:job id="MyJob">
<batch:step id="readandstore">
<batch:tasklet>
<batch:chunk reader="multiResourceReader" writer="oracleItemWriter" commit-interval="10" />
<batch:listeners>
<batch:listener ref="StepListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
</batch:job>

<bean id="multiResourceReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="delegate" ref="csvFileItemReader" />
</bean>

<bean id="StepListener" class="com.myapp.StepListener">
<property name="filePattern" value="file:/opt/data/*.csv" />
</bean>

这是 com.myapp.StepListener 的样子:

package com.myapp;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.apache.commons.io.FileUtils;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

public class StepListener implements StepExecutionListener, ApplicationContextAware {

private static final Logger logger = Logger.getLogger(StepListener.class.getName());
private Resource[] resources;
private ApplicationContext applicationContext;
private String filePattern;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public void setFilePattern(String filePattern) {
this.filePattern = filePattern;
}

@Override
public void beforeStep(StepExecution stepExecution) {

MultiResourceItemReader reader = (MultiResourceItemReader) applicationContext.getBean("multiResourceReader");
try {
resources = applicationContext.getResources(filePattern);
reader.setResources(resources);
} catch (IOException ex) {
logger.log(Level.SEVERE, "Unable to set file resources to bean multiResourceItemReader", ex);
}
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {

if (stepExecution.getExitStatus().equals(ExitStatus.COMPLETED)
&& stepExecution.getStatus().equals(BatchStatus.COMPLETED)
&& resources.length > 0) {

for (Resource resource : resources) {
try {
File oldFile = new File(resource.getFile().getAbsolutePath());
File newFile = new File(resource.getFile().getAbsolutePath() + ".processed");
FileUtils.copyFile(oldFile, newFile);
oldFile.delete();
} catch (IOException ex) {
logger.log(Level.SEVERE, "Encountered problem when trying to remove the processed file(s)", ex);
}
}
}

return stepExecution.getExitStatus();
}
}

关于java - 如何确保 MultiResourceItemReader 在每次执行作业时刷新资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880300/

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