- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个使用 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/
有人告诉我,如果我只有一个“东西”,比如家(不是多个家),我应该在 routes.rb 中使用资源 :home,而不是资源 :home。但是当我查看路由时,POST 函数似乎想要 home#creat
Activity 开始。这些代码框架顺利通过。 // Initialize array adapters. One for already paired devices and //
资源 search-hadoop.com search-hadoop.com索引所有邮件列表,非常适合历史搜索。当你遇到问题时首先在这里搜索,因为很可能有人已经遇到了你的问题。 邮件列表 在A
我是 WPF 的新手,正在努力使用位于单独程序集中的样式。这就是我正在做的:- 我有一个带有\Themes 文件夹的类库项目,其中包含一个“generic.xaml”,它合并了\Themes 内的子文
我正在编写一个使用虚拟树状文件结构的插件。基本上它就像一个包含文件的标准文件系统,区别在于这些文件实际上并不存在于文件系统中的特定位置,而只是 java 对象。 这些当前由使用 SettingProv
如果我在 XAML 中使用以下内容,我会收到错误消息: 错
我正在使用 laravel 资源来获取 api 的数据: return [ 'id' => $this->id, 'unread' =>
我有以下 pom.xml: 4.0.0 mycompany resource-fail 0.0.1-SNAPSHOT BazBat
许多GDI +类都实现IDisposable,但是我不确定何时应该调用Dispose。对于使用new或静态方法(例如Graphics.CreateGraphics)创建的实例来说,这很明显。但是,由属
我正在构建一组 RESTful 资源,其工作方式如下:(我将使用“people”作为示例): 获取/people/{key} - 返回一个人对象 (JSON) GET/people?first_nam
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一个使用 $resource 的简单 Controller : var Regions = $resource('mocks/regions.json'); $scope.regions =
在 Azure 门户中,如何查看不同资源之间的依赖关系。我特别想查看哪些资源正在使用我要删除的存储。 最佳答案 您可以使用应用程序洞察应用程序 map 来执行此操作: 您还可以打开存储帐户的日志记录:
我正在使用 ionic 生成资源(图标和启动画面)。我正在使用 ionic v2.1.0 和 cordova v6.4.0。 到目前为止我一直在使用(它在以前的版本中工作): cordova plat
是否可以使用 Assets 包含子文件夹中的文件? 示例:[base_url]/assets/css/pepper-grinder/jquery-ui-1.8.11.custom.min.css 最佳
我正在阅读一些尝试教授 Android 开发的书。在书中,作者概述了 res/下的一些目录。他提到 res/menu 包含基于 XML 的菜单规范。他还提到了保存“通用文件”的 res/raw。当我创
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在服务器上使用 express-resource。在我的 AngularJS Controller 中: var User = $resource('/services/users/:use
因此,每当我运行我的应用程序时,它都会立即崩溃并给出以下错误: No package identifier when getting value for resource number 0x00000
对于我正在创建的(网络)应用程序,我需要使用基本身份验证在我的 UIWebView 中加载页面。 现在设置我使用的授权 header : NSString *result = [NSString st
我是一名优秀的程序员,十分优秀!