当前位置: 首页 > 知识库问答 >
问题:

Spring批处理分区:ItemReader Bean在@value(“#{stepexecutioncontext[fromId]}”)读取中存在问题

马德厚
2023-03-14

spring batch的新版本,并尝试为大量数据输入实现分区。我已经检查了下面的解决方案,这些解决方案与我的问题相似,但不能解决问题。

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'stepExecutionContext' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
    ... 19 more
@Configuration
public class ProfileJobItemReader{

    @Value("#{stepExecutionContext[fromId]}")
    private int fromId;

    @Bean(name = "profileMongoItemReader")
    @StepScope
    public ItemReader<ProfileCollection> profileMongoItemReader()
            throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        System.out.println(fromId);
    }

}
<bean id="profileItemProcessor" class="...........ProfileProcessor" scope="step" />
<bean id="profileItemWriter" class="...........ProfileWriter" scope="step" />

.............

<bean id="rangePartitioner" class="...........ProfilePartitioner" />

<batch:job id="profileJob">
    <batch:step id="masterStep">
        <partition step="profileMainStep" partitioner="rangePartitioner">
            <handler grid-size="100" task-executor="taskExecutor" />
        </partition>
    </batch:step>
</batch:job>

<batch:step id="profileMainStep">
    <batch:tasklet task-executor="profileTaskExecutor" throttle-limit="30">
        <batch:chunk reader="profileMongoItemReader" processor="profileItemProcessor"
            writer="profileItemWriter" commit-interval="${profileCommitInterval}" />
    </batch:tasklet>
</batch:step>
@Value("#{stepExecutionContext[fromId]}")
public class ProfileProcessor implements ItemProcessor<ProfileCollection, ProfileCollection> {

    @Value("#{stepExecutionContext[name]}")
    private String threadName;

    @Override
    public ProfileCollection process(ProfileCollection item) throws Exception {
        System.out.println(threadName);

共有1个答案

刘博雅
2023-03-14

问题是阅读器中的@configuration注释。在作业开始前加载配置类。

只有在启动一个步骤之后,您才会获得stepexecutioncontext。处理器类获得StepExecutionContext,因为它没有Component/Configuration注释。

 类似资料:
  • 我们有一个spring批处理作业,我们试图处理大约1000万条记录。现在,在单线程中执行此操作将非常慢,因为我们必须匹配SLA。 为了提高性能,我们开发了一个POC,其中主步骤是创建分区,每个分区代表一个唯一的prod ID。这可以从500到4500之间的任何地方。在POC中,我们有500个这样唯一的产品ID。现在,每个分区都有一个prod id和一个步骤。所有这一切都很顺利。 我们注意到的是主步

  • 我的Spring批处理配置: 我的类是: 这不是正确的做法吗?请建议。

  • 是否有方法访问分区步骤中定义为JobScope的bean? 我们将http客户端bean定义为JobScope,因为每个作业都是唯一的,但它是动态创建的,我们需要在从属步骤中使用它来发出post请求。当我们自动连线我们得到的一切 这里是作业配置类(我删除了所有不需要的内容,只剩下分区步骤和客户机类的配置,它们位于作业范围中,因为在每次作业运行时都需要特定于作业: 下面是CaptureerStock

  • 我正在使用JpaPagingItemReaderBuilder查询一个DB,结果被插入到另一个DB中。 查询返回的结果没有任何问题,但我得到了一个错误与读取器的返回,在处理器中,您可以检查我的编码和错误下面。 有谁能给我一点启示吗?为什么我不能处理结果?

  • 我正在尝试创建一个应用程序,该应用程序使用spring-batch-excel扩展名来读取用户通过web界面上传的Excel文件,以便解析Excel文件中的地址。 当代码运行时,没有错误,但我得到的只是我日志中的以下内容。即使我的处理器和Writer中都有log/syso(它们从未被调用过,我所能想象的是它没有正确读取文件,也没有返回要处理/写入的数据)。是的,这个文件有数据,实际上有几千条记录。