ResourceAware 而不是 LineCallbackHandlers 的 MultiResourceItemReader.getCurrentResource()
ResourceAware instead of MultiResourceItemReader.getCurrentResource() for LineCallbackHandlers
我正在使用 MultiResourceItemReader
,它的 getCurrentResource()
方法最近已弃用(参见:https://github.com/spring-projects/spring-batch/issues/3776)。
替代方法是使用 ResourceAware
界面,但我遇到了一个问题,想知道是否有人可以提供帮助。
当前设置:
- 我有一个
MultiResourceItemReader
使用 FlatFileItemReader
委托
FlatFileItemReader
使用 LineCallbackHandler
,它是使用 FlatFileItemReaderBuilder
的 skippedLinesCallback()
方法设置的
- 当前实现已将
MultiResourceItemReader
注入到 LineCallbackHandler
中,以便 handleLine(String line)
方法可以了解当前正在处理的文件
- 据我了解,
ResouceAware
接口是在域项上实现的,但就我而言,我还没有域项的实例...我只有文件中的输入字符串因为该行已被跳过
我将如何让我的 LineCallbackHandler 成为“资源感知”,以便我可以继续我刚才的逻辑?
代码:
配置:
@Bean
public MultiResourceItemReader<Items> reader(@Value(SPRING_VALUE_TO_LOOK_FOR) final String paths) {
final Resource[] resources = EtlHelpers.convertFilesToProcessFromContextString(paths);
final MultiResourceItemReader<Items> reader = new MultiResourceItemReader<>();
reader.setName("multiFileReader");
reader.setDelegate(fileReaderDelegate());
reader.setResources(resources);
return reader;
}
@Bean
public FlatFileItemReader<Items> fileReaderDelegate() {
return new FlatFileItemReaderBuilder<Items>()
.name("fileReader")
.linesToSkip(1)
.skippedLinesCallback(headerValidator)
// (extra unrelated config)
.build();
}
名为 HeaderValidator
的 LineCallbackHandler
@Service
@RequiredArgsConstructor
public class HeaderValidator implements LineCallbackHandler {
private final MultiResourceItemReader<Items> itemReader;
@Override
public void handleLine(@NonNull final String line) {
final String desc = Optional.ofNullable(itemReader.getCurrentResource()).map(Resource::getDescription).orElse("file");
log.info("{}: Ensuring headings are on the first line and are correct (using case-insensitive comparisons)", desc);
// ... other uses of itemReader.getCurrentResource() omitted for brevity
}
}
如果我理解正确,您的目标是跳过 header 并了解每个 header 来自哪个资源。尽管这是非常具体的用例,但我认为它是有效的,因为它可以应用于任何被视为注释的行(比如知道每个注释行的源文件)。
Current implementation has the MultiResourceItemReader injected into the LineCallbackHandler so that the handleLine(String line) method can make use of knowing which file is currently being processed
我希望你同意这将 LineCallbackHandler
与 MultiResourceItemReader
结合起来,这并不理想(如 issue that deprecates MultiResourceItemReader#getCurrentResource
). If this coupling is not an issue for you, then you can get the current resource from the FlatFileItemReader
itself, since the MultiResourceItemReader
sets the current resource on the delegate each time a new resource is processed. You can find a complete example here 中所述(请注意,这需要一个新的 getter 用于 FlatFileItemReader
).
中的资源
就是说,我认为没有明显的方法可以使用 Spring Batch 当前提供的方法来实现您的用例。 skippedLinesCallback
是在doOpen
方法中调用的,早于通过ResourceAware
设置资源的映射项。我想到解决此类用例的最直接方法是使用 ResourceAwareLineCallbackHandler
并使 FlatFileItemReader
在其上设置当前资源。
请参考此 SO 问题打开一个问题,我们可以讨论引入此功能的最佳方式。先谢谢你。
我正在使用 MultiResourceItemReader
,它的 getCurrentResource()
方法最近已弃用(参见:https://github.com/spring-projects/spring-batch/issues/3776)。
替代方法是使用 ResourceAware
界面,但我遇到了一个问题,想知道是否有人可以提供帮助。
当前设置:
- 我有一个
MultiResourceItemReader
使用FlatFileItemReader
委托 FlatFileItemReader
使用LineCallbackHandler
,它是使用FlatFileItemReaderBuilder
的skippedLinesCallback()
方法设置的- 当前实现已将
MultiResourceItemReader
注入到LineCallbackHandler
中,以便handleLine(String line)
方法可以了解当前正在处理的文件 - 据我了解,
ResouceAware
接口是在域项上实现的,但就我而言,我还没有域项的实例...我只有文件中的输入字符串因为该行已被跳过
我将如何让我的 LineCallbackHandler 成为“资源感知”,以便我可以继续我刚才的逻辑?
代码:
配置:
@Bean
public MultiResourceItemReader<Items> reader(@Value(SPRING_VALUE_TO_LOOK_FOR) final String paths) {
final Resource[] resources = EtlHelpers.convertFilesToProcessFromContextString(paths);
final MultiResourceItemReader<Items> reader = new MultiResourceItemReader<>();
reader.setName("multiFileReader");
reader.setDelegate(fileReaderDelegate());
reader.setResources(resources);
return reader;
}
@Bean
public FlatFileItemReader<Items> fileReaderDelegate() {
return new FlatFileItemReaderBuilder<Items>()
.name("fileReader")
.linesToSkip(1)
.skippedLinesCallback(headerValidator)
// (extra unrelated config)
.build();
}
名为 HeaderValidator
@Service
@RequiredArgsConstructor
public class HeaderValidator implements LineCallbackHandler {
private final MultiResourceItemReader<Items> itemReader;
@Override
public void handleLine(@NonNull final String line) {
final String desc = Optional.ofNullable(itemReader.getCurrentResource()).map(Resource::getDescription).orElse("file");
log.info("{}: Ensuring headings are on the first line and are correct (using case-insensitive comparisons)", desc);
// ... other uses of itemReader.getCurrentResource() omitted for brevity
}
}
如果我理解正确,您的目标是跳过 header 并了解每个 header 来自哪个资源。尽管这是非常具体的用例,但我认为它是有效的,因为它可以应用于任何被视为注释的行(比如知道每个注释行的源文件)。
Current implementation has the MultiResourceItemReader injected into the LineCallbackHandler so that the handleLine(String line) method can make use of knowing which file is currently being processed
我希望你同意这将 LineCallbackHandler
与 MultiResourceItemReader
结合起来,这并不理想(如 issue that deprecates MultiResourceItemReader#getCurrentResource
). If this coupling is not an issue for you, then you can get the current resource from the FlatFileItemReader
itself, since the MultiResourceItemReader
sets the current resource on the delegate each time a new resource is processed. You can find a complete example here 中所述(请注意,这需要一个新的 getter 用于 FlatFileItemReader
).
就是说,我认为没有明显的方法可以使用 Spring Batch 当前提供的方法来实现您的用例。 skippedLinesCallback
是在doOpen
方法中调用的,早于通过ResourceAware
设置资源的映射项。我想到解决此类用例的最直接方法是使用 ResourceAwareLineCallbackHandler
并使 FlatFileItemReader
在其上设置当前资源。
请参考此 SO 问题打开一个问题,我们可以讨论引入此功能的最佳方式。先谢谢你。