FINERACT-186 batch job optimisation

This commit is contained in:
ShruthiRajaram
2018-12-25 18:16:26 +05:30
2 changed files with 35 additions and 21 deletions
@@ -20,6 +20,8 @@ package org.apache.fineract.portfolio.savings.domain;
import java.util.List; import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@@ -49,4 +51,6 @@ public interface SavingsAccountRepository extends JpaRepository<SavingsAccount,
@Query("select sa from SavingsAccount sa where sa.accountNumber = :accountNumber and sa.status in (100, 200, 300, 303, 304) ") @Query("select sa from SavingsAccount sa where sa.accountNumber = :accountNumber and sa.status in (100, 200, 300, 303, 304) ")
SavingsAccount findNonClosedAccountByAccountNumber(@Param("accountNumber") String accountNumber); SavingsAccount findNonClosedAccountByAccountNumber(@Param("accountNumber") String accountNumber);
Page<SavingsAccount> findByStatus(Integer status,Pageable pageable);
} }
@@ -26,10 +26,12 @@ import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.apache.fineract.infrastructure.jobs.service.JobName; import org.apache.fineract.infrastructure.jobs.service.JobName;
import org.apache.fineract.portfolio.savings.domain.SavingsAccount; import org.apache.fineract.portfolio.savings.domain.SavingsAccount;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountAssembler; import org.apache.fineract.portfolio.savings.domain.SavingsAccountAssembler;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountRepositoryWrapper; import org.apache.fineract.portfolio.savings.domain.SavingsAccountRepository;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountStatusType; import org.apache.fineract.portfolio.savings.domain.SavingsAccountStatusType;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@@ -37,32 +39,37 @@ public class SavingsSchedularServiceImpl implements SavingsSchedularService {
private final SavingsAccountAssembler savingAccountAssembler; private final SavingsAccountAssembler savingAccountAssembler;
private final SavingsAccountWritePlatformService savingsAccountWritePlatformService; private final SavingsAccountWritePlatformService savingsAccountWritePlatformService;
private final SavingsAccountRepositoryWrapper savingAccountRepositoryWrapper;
private final SavingsAccountReadPlatformService savingAccountReadPlatformService; private final SavingsAccountReadPlatformService savingAccountReadPlatformService;
private final SavingsAccountRepository savingsAccountRepository;
@Autowired @Autowired
public SavingsSchedularServiceImpl(final SavingsAccountAssembler savingAccountAssembler, public SavingsSchedularServiceImpl(final SavingsAccountAssembler savingAccountAssembler,
final SavingsAccountWritePlatformService savingsAccountWritePlatformService, final SavingsAccountWritePlatformService savingsAccountWritePlatformService,
final SavingsAccountRepositoryWrapper savingAccountRepositoryWrapper, final SavingsAccountReadPlatformService savingAccountReadPlatformService, final SavingsAccountRepository savingsAccountRepository) {
final SavingsAccountReadPlatformService savingAccountReadPlatformService) {
this.savingAccountAssembler = savingAccountAssembler; this.savingAccountAssembler = savingAccountAssembler;
this.savingsAccountWritePlatformService = savingsAccountWritePlatformService; this.savingsAccountWritePlatformService = savingsAccountWritePlatformService;
this.savingAccountRepositoryWrapper = savingAccountRepositoryWrapper;
this.savingAccountReadPlatformService = savingAccountReadPlatformService; this.savingAccountReadPlatformService = savingAccountReadPlatformService;
this.savingsAccountRepository = savingsAccountRepository;
} }
@CronTarget(jobName = JobName.POST_INTEREST_FOR_SAVINGS) @CronTarget(jobName = JobName.POST_INTEREST_FOR_SAVINGS)
@Override @Override
public void postInterestForAccounts() throws JobExecutionException { public void postInterestForAccounts() throws JobExecutionException {
final List<SavingsAccount> savingsAccounts = this.savingAccountRepositoryWrapper.findSavingAccountByStatus(SavingsAccountStatusType.ACTIVE int page = 0;
.getValue()); Integer initialSize = 500;
Integer totalPageSize = 0;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (final SavingsAccount savingsAccount : savingsAccounts) { do {
PageRequest pageRequest = new PageRequest(page, initialSize);
Page<SavingsAccount> savingsAccounts = this.savingsAccountRepository.findByStatus(SavingsAccountStatusType.ACTIVE.getValue(),
pageRequest);
for (SavingsAccount savingsAccount : savingsAccounts.getContent()) {
try { try {
this.savingAccountAssembler.assignSavingAccountHelpers(savingsAccount); this.savingAccountAssembler.assignSavingAccountHelpers(savingsAccount);
boolean postInterestAsOn = false; boolean postInterestAsOn = false;
LocalDate transactionDate = null; LocalDate transactionDate = null;
this.savingsAccountWritePlatformService.postInterest(savingsAccount, postInterestAsOn, transactionDate); this.savingsAccountWritePlatformService.postInterest(savingsAccount, postInterestAsOn,
transactionDate);
} catch (Exception e) { } catch (Exception e) {
Throwable realCause = e; Throwable realCause = e;
if (e.getCause() != null) { if (e.getCause() != null) {
@@ -72,6 +79,9 @@ public class SavingsSchedularServiceImpl implements SavingsSchedularService {
+ realCause.getMessage()); + realCause.getMessage());
} }
} }
page++;
totalPageSize = savingsAccounts.getTotalPages();
} while (page < totalPageSize);
if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); }
} }