This is an automated email from the ASF dual-hosted git repository.

meonkeys pushed a commit to branch release/1.16.1
in repository https://gitbox.apache.org/repos/asf/fineract.git

commit c643dab306fe29798951591ce825fd5e5272923e
Author: Jose Alberto Hernandez <[email protected]>
AuthorDate: Mon Oct 26 10:01:49 2026 -0500

    FINERACT-2995: WC COB worker shares reader and processor across concurrent 
partitions
---
 .../WorkingCapitalLoanCOBWorkerConfiguration.java  | 39 +++++++++--
 .../WorkingCapitalLoanCOBWorkerStepScopeTest.java  | 78 ++++++++++++++++++++++
 2 files changed, 110 insertions(+), 7 deletions(-)

diff --git 
a/fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerConfiguration.java
 
b/fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerConfiguration.java
index 00e6e87266..66ea832dbf 100644
--- 
a/fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerConfiguration.java
+++ 
b/fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerConfiguration.java
@@ -35,6 +35,7 @@ import 
org.apache.fineract.infrastructure.springbatch.StepRetryPolicyFactory;
 import 
org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
 import 
org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanRepository;
 import 
org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanModelProcessingService;
+import org.springframework.batch.core.configuration.annotation.StepScope;
 import org.springframework.batch.core.repository.JobRepository;
 import org.springframework.batch.core.step.Step;
 import org.springframework.batch.core.step.builder.ChunkOrientedStepBuilder;
@@ -72,18 +73,14 @@ public class WorkingCapitalLoanCOBWorkerConfiguration {
     private final ResetContextTasklet resetContextTasklet;
 
     @Bean(WORKING_CAPITAL_LOAN_COB_WORKER_STEP)
-    public Step workingCapitalLoanCOBWorkerStep(final COBBusinessStepService 
cobBusinessStepService,
-            final WorkingCapitalLoanModelProcessingService 
modelProcessingService) {
-        WorkingCapitalLoanCOBWorkerItemReader reader = new 
WorkingCapitalLoanCOBWorkerItemReader(workingCapitalLoanRepository,
-                new BeforeStepLockingItemReaderHelper(retrieveIdService, 
wpcLoanLockingService));
-        WorkingCapitalLoanCOBWorkerItemProcessor processor = new 
WorkingCapitalLoanCOBWorkerItemProcessor(cobBusinessStepService,
-                modelProcessingService);
+    public Step workingCapitalLoanCOBWorkerStep(final 
WorkingCapitalLoanCOBWorkerItemReader reader,
+            final WorkingCapitalLoanCOBWorkerItemProcessor processor, final 
WorkingCapitalLoanCOBWorkerItemWriter writer) {
         final ChunkOrientedStepBuilder<WorkingCapitalLoan, WorkingCapitalLoan> 
stepBuilder = stepBuilderFactory
                 
.get(WORKING_CAPITAL_LOAN_COB_WORKER_STEP).inputChannel(inboundRequests)
                 .<WorkingCapitalLoan, 
WorkingCapitalLoan>chunk(propertyService.getChunkSize(WORKING_CAPITAL_JOB_NAME))
 //
                 .reader(reader) //
                 .processor(processor) //
-                .writer(new 
WorkingCapitalLoanCOBWorkerItemWriter(wpcLoanLockingService, 
workingCapitalLoanRepository)) //
+                .writer(writer) //
                 .faultTolerant() //
                 
.retryPolicy(StepRetryPolicyFactory.immediateRetryPolicy(propertyService.getRetryLimit(WORKING_CAPITAL_JOB_NAME),
                         Exception.class)) //
@@ -115,4 +112,32 @@ public class WorkingCapitalLoanCOBWorkerConfiguration {
         return new ApplyWorkingCapitalLoanLockTasklet(fineractProperties, 
wpcLoanLockingService, retrieveIdService,
                 requiresNewTransactionJdbcTemplate);
     }
+
+    // Reader, processor and writer are step-scoped, as in 
LoanCOBWorkerConfiguration. The reader keeps the partition's
+    // loan ids and the processor keeps the step's execution context and 
business date in instance fields, so every
+    // partition (and every tenant) needs its own instance. A single shared 
instance let concurrent partitions - more
+    // eligible loans than the partition size in one tenant, or several 
tenants running WC COB at once - overwrite each
+    // other's queue: one partition then read nothing, finished COMPLETED and 
left its loan a day behind with a stale
+    // LOAN_COB_CHUNK_PROCESSING lock. The bean methods return the concrete 
classes on purpose: the scoped proxy then
+    // subclasses them, so it is still a StepExecutionListener and Spring 
Batch registers it for beforeStep.
+
+    @Bean
+    @StepScope
+    public WorkingCapitalLoanCOBWorkerItemReader 
workingCapitalLoanCOBWorkerItemReader() {
+        return new 
WorkingCapitalLoanCOBWorkerItemReader(workingCapitalLoanRepository,
+                new BeforeStepLockingItemReaderHelper(retrieveIdService, 
wpcLoanLockingService));
+    }
+
+    @Bean
+    @StepScope
+    public WorkingCapitalLoanCOBWorkerItemProcessor 
workingCapitalLoanCOBWorkerItemProcessor(
+            final COBBusinessStepService cobBusinessStepService, final 
WorkingCapitalLoanModelProcessingService modelProcessingService) {
+        return new 
WorkingCapitalLoanCOBWorkerItemProcessor(cobBusinessStepService, 
modelProcessingService);
+    }
+
+    @Bean
+    @StepScope
+    public WorkingCapitalLoanCOBWorkerItemWriter 
workingCapitalLoanCOBWorkerItemWriter() {
+        return new 
WorkingCapitalLoanCOBWorkerItemWriter(wpcLoanLockingService, 
workingCapitalLoanRepository);
+    }
 }
diff --git 
a/fineract-working-capital-loan/src/test/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerStepScopeTest.java
 
b/fineract-working-capital-loan/src/test/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerStepScopeTest.java
new file mode 100644
index 0000000000..e100913f14
--- /dev/null
+++ 
b/fineract-working-capital-loan/src/test/java/org/apache/fineract/cob/workingcapitalloan/WorkingCapitalLoanCOBWorkerStepScopeTest.java
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cob.workingcapitalloan;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.springframework.batch.core.configuration.annotation.StepScope;
+import org.springframework.batch.core.step.Step;
+import org.springframework.context.annotation.Bean;
+
+/**
+ * Pins the WC COB worker reader, processor and writer to step scope.
+ * <p>
+ * All three keep per-partition state in instance fields: the reader its queue 
of loan ids, the processor the step's
+ * execution context and business date. With a single shared instance, two 
partitions running at the same time on one
+ * instance (more eligible loans than the partition size in one tenant, or 
several tenants running WC COB at once)
+ * overwrite each other's queue; one of them reads nothing, finishes COMPLETED 
and leaves its loan one COB day behind
+ * with a stale {@code LOAN_COB_CHUNK_PROCESSING} lock. LOAN_COB has always 
declared these components as
+ * {@code @StepScope} beans; this test fails if WC COB drifts back to building 
them inline.
+ */
+public class WorkingCapitalLoanCOBWorkerStepScopeTest {
+
+    private static Stream<Arguments> perPartitionComponents() {
+        return Stream.of(//
+                Arguments.of(WorkingCapitalLoanCOBWorkerItemReader.class), //
+                Arguments.of(WorkingCapitalLoanCOBWorkerItemProcessor.class), 
//
+                Arguments.of(WorkingCapitalLoanCOBWorkerItemWriter.class));
+    }
+
+    @ParameterizedTest(name = "{0} must be a @StepScope bean")
+    @MethodSource("perPartitionComponents")
+    public void shouldDeclareAStepScopedBeanReturningTheConcreteClass(Class<?> 
componentClass) {
+        Stream<Method> factories = 
Arrays.stream(WorkingCapitalLoanCOBWorkerConfiguration.class.getDeclaredMethods())
+                .filter(method -> 
method.getReturnType().equals(componentClass));
+
+        assertThat(factories)
+                .as("%s must come from a @Bean @StepScope factory method 
returning the concrete class, "
+                        + "so that each partition gets its own instance and 
Spring Batch registers it as a StepExecutionListener",
+                        componentClass.getSimpleName())
+                .singleElement().satisfies(method -> 
assertThat(method.isAnnotationPresent(Bean.class)).isTrue())
+                .satisfies(method -> 
assertThat(method.isAnnotationPresent(StepScope.class)).isTrue());
+    }
+
+    @Test
+    public void workerStepShouldBeBuiltFromTheStepScopedBeans() {
+        Stream<Method> stepFactories = 
Arrays.stream(WorkingCapitalLoanCOBWorkerConfiguration.class.getDeclaredMethods())
+                .filter(method -> method.getReturnType().equals(Step.class));
+
+        assertThat(stepFactories).as("the worker step must take the reader, 
processor and writer as injected step-scoped beans")
+                .singleElement()
+                .satisfies(method -> 
assertThat(method.getParameterTypes()).containsExactlyInAnyOrder(
+                        WorkingCapitalLoanCOBWorkerItemReader.class, 
WorkingCapitalLoanCOBWorkerItemProcessor.class,
+                        WorkingCapitalLoanCOBWorkerItemWriter.class));
+    }
+}

Reply via email to