- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个从数据库读取的多线程批处理作业,我担心不同的线程会重新读取记录,因为 ItemReader 在 Spring 批处理中不是线程安全的。我经历了SpringBatch FAQ部分指出
You can synchronize the read() method (e.g. by wrapping it in a delegator that does the synchronization). Remember that you will lose restartability, so best practice is to mark the step as not restartable and to be safe (and efficient) you can also set saveState=false on the reader.
public SynchronizedItemReader<T> implements ItemReader<T> {
private final ItemReader<T> delegate;
public SynchronizedItemReader(ItemReader<T> delegate) {
this.delegate = delegate;
}
public synchronized T read () {
return delegate.read();
}
}
最佳答案
当使用带有多线程的 ItemReader 时,缺乏可重新启动性与读取本身无关。这是关于保存更新方法中发生的阅读器状态。问题是调用 read() - 提供数据的方法和 update() - 保持状态的方法之间需要协调。当您使用多个线程时,读取器的内部状态(以及因此 update() 调用)可能会也可能不会反射(reflect)已完成的工作。以 FlatFileItemReader 为例,它使用 5 的 block 大小并在多个线程上运行。您可能让线程 1 读取了 5 个项目(更新时间),但线程 2 可能已经读取了另外 3 个项目。这意味着对更新的调用将保存已读取的 8 个项目。如果线程 2 上的 block 失败,则状态将不正确,并且重新启动将丢失已读取的三个项目。
这并不是说不可能编写线程安全的 ItemReader。但是,正如您上面的示例所示,如果委托(delegate)是有状态的 ItemReader (也实现了 ItemStream),则状态将不会通过调用 update 正确持久化(实际上,您上面的示例甚至没有采用分阶段阅读器的 ItemStream 方面考虑)。
关于multithreading - <Spring Batch> 为什么使 ItemReader 线程安全会导致我们失去可重新启动性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19971973/
我已经创建了我的自定义 ItemReader: @Component("pricereader") public class MyItemReader implements ItemReader{ @
我正在尝试访问 itemReader 中的 JobParameters,我遵循了 Spring 用户指南中的“作业和步骤属性的后期绑定(bind)”,但无法使其工作。 这是我的配置: Spring 批
我正在从数据库中读取数据,它有很多行,是的,但是“我认为读者在该步骤中释放了内存”。它需要超过 1Gb 的内存。怎样才能改善呢?使用另一种阅读器? ...
我正在使用 Spring Batch 的 ItemReader 来读取文件 我的输入文件包含由 分隔的一长串字符串, 如下所示: 2333,443445,5555,3344... 每个字符串都应该转换
我用 JdbcTemplate 实现了 ItemReader 。 问题是 read() 在无限循环中被调用。 public class MyReader implements ItemReader,
使用 Spring Batch 我试图将输入文件的每一行作为 String 提供给 ItemProcessor 而 中没有任何“CSV 解析”项目阅读器。 我提出了一个包含以下 reader() 方法
在我的 springbatch+quartz 设置中,我正在使用 FlatFileItemReader 读取一个 CSV 文件。我想为读者设置光标以使用给定的读者参数启动下一个作业实例。是否可以?
以下是我的 Spring Batch 用例。 从 Web 服务读取输入。 Web 服务将返回所有记录。 处理记录。 将处理过的记录一一写入。 我很清楚第 2 步和第 3 步,但无法弄清楚如何实现一个可
我有一个 Spring Batch 应用程序,它使用 JdbcCursorItemReader 从数据库表中读取数据,并使用 FlatFileItemWriter 将其写入平面文件。 当我测试我的应用
我有一个 Spring Batch 应用程序,它使用 JdbcCursorItemReader 从数据库表中读取数据,并使用 FlatFileItemWriter 将其写入平面文件。 当我测试我的应用
我正在尝试读取固定长度的文本文件并使用 spring Batch 将其寄存器导入数据库,但输入文件的常规布局是嵌套的,类似于(添加制表只是为了简化可视化): FILE HEADER ENTER
我想验证文件名和记录。我的应用上下文如下 我的项目阅读器上下文如下所示
我在我的批处理 Spring 项目中定义了一个作业流,并定义了 ItemReader、ItemProcessor、ItemWriter 等 我的 ItemReader 如下代码: @Component
我有一个带有简单自定义读取器和写入器的 Spring Batch 项目。当我运行代码时,我最终会出现一个无限循环,打印第一个项目“项目 1”。我究竟做错了什么? 这是我的代码: Reader.java
我刚刚开始使用 Spring Batch,想知道维护要在 ItemReader 中读取的项目列表的最佳实践是什么。我看到的例子是这样的: private static List items = new
我目前有一个应用程序,我正在尝试诊断我在设置中所做的错误,但没有运气确定为什么它在非常特定的情况之外无法工作。 首先是我正在使用的代码。 配置.java @EnableBatchProcessing
我正在尝试使用 ListItemReader 创建一个 Spring Batch 作业, ItemProcessor和 ItemWriter . XML 如下所示,
我有一个从数据库读取的多线程批处理作业,我担心不同的线程会重新读取记录,因为 ItemReader 在 Spring 批处理中不是线程安全的。我经历了SpringBatch FAQ部分指出 You c
当我尝试读取 csv 文件时,Spring 批处理遇到了这个问题。 我必须读取具有如下结构的CSV: valueHeader;valueHeader;valueHeader;valueHeader;v
我有一个 Spring Batch 项目,其中有 JsonResouurceAsyncPagingItemReader,这是一个自定义 ItemReader,它从 Web 服务异步检索数据。 此自定义
我是一名优秀的程序员,十分优秀!