vorburger commented on a change in pull request #464: FINERACT-428 
Parallelization of Jobs :Parallelizing and paging of update loan summary
URL: https://github.com/apache/fineract/pull/464#discussion_r253849965
 
 

 ##########
 File path: 
fineract-provider/src/main/java/org/apache/fineract/scheduledjobs/service/ScheduledJobRunnerServiceImpl.java
 ##########
 @@ -443,43 +466,109 @@ public void updateTrialBalanceDetails() throws 
JobExecutionException {
                     .append("from acc_gl_journal_entry je WHERE 
je.transaction_date = ? ")
                     .append("group by je.account_id, je.office_id, 
je.transaction_date, Date(je.entry_date)");
 
-            final int result = jdbcTemplate.update(sqlBuilder.toString(), new 
Object[] {
+            final int result = jdbcTemplate.update(sqlBuilder.toString(), new 
Object[]{
                     formattedDate
             });
             logger.info(ThreadLocalContextUtil.getTenant().getName() + ": 
Results affected by update: " + result);
         }
 
+
         // Updating closing balance
-        String distinctOfficeQuery = "select distinct(office_id) from 
m_trial_balance where closing_balance is null group by office_id";
-        final List<Long> officeIds = 
jdbcTemplate.queryForList(distinctOfficeQuery, new Object[] {}, Long.class);
+        String distinctOfficeQuery = "SELECT DISTINCT(office_id) FROM 
m_trial_balance WHERE closing_balance IS NULL GROUP BY office_id";
+        final List<Long> officeIds = 
jdbcTemplate.queryForList(distinctOfficeQuery, new Object[]{}, Long.class);
 
 
-        for(Long officeId : officeIds) {
-            String distinctAccountQuery = "select distinct(account_id) from 
m_trial_balance where office_id=? and closing_balance is null group by 
account_id";
-            final List<Long> accountIds = 
jdbcTemplate.queryForList(distinctAccountQuery, new Object[] {officeId}, 
Long.class);
-            for(Long accountId : accountIds) {
-                final String closingBalanceQuery = "select closing_balance 
from m_trial_balance where office_id=? and account_id=? and closing_balance " +
-                        "is not null order by created_date desc, entry_date 
desc limit 1";
-                List<BigDecimal> closingBalanceData = 
jdbcTemplate.queryForList(closingBalanceQuery, new Object[] {officeId, 
accountId}, BigDecimal.class);
+        for (Long officeId : officeIds) {
+            String distinctAccountQuery = "SELECT DISTINCT(account_id) FROM 
m_trial_balance WHERE office_id=? AND closing_balance IS NULL GROUP BY 
account_id";
+            final List<Long> accountIds = 
jdbcTemplate.queryForList(distinctAccountQuery, new Object[]{officeId}, 
Long.class);
+            for (Long accountId : accountIds) {
+                final String closingBalanceQuery = "SELECT closing_balance 
FROM m_trial_balance WHERE office_id=? AND account_id=? AND closing_balance " +
+                        "IS NOT NULL ORDER BY created_date DESC, entry_date 
DESC LIMIT 1";
+                List<BigDecimal> closingBalanceData = 
jdbcTemplate.queryForList(closingBalanceQuery, new Object[]{officeId, 
accountId}, BigDecimal.class);
                 List<TrialBalance> tbRows = 
this.trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, 
accountId);
                 BigDecimal closingBalance = null;
-                if(!CollectionUtils.isEmpty(closingBalanceData))
+                if (!CollectionUtils.isEmpty(closingBalanceData))
                     closingBalance = closingBalanceData.get(0);
-                if(CollectionUtils.isEmpty(closingBalanceData)) {
+                if (CollectionUtils.isEmpty(closingBalanceData)) {
                     closingBalance = BigDecimal.ZERO;
-                    for(TrialBalance row : tbRows) {
+                    for (TrialBalance row : tbRows) {
                         closingBalance = closingBalance.add(row.getAmount());
                         row.setClosingBalance(closingBalance);
                     }
                 } else {
-                    for(TrialBalance tbRow : tbRows) {
+                    for (TrialBalance tbRow : tbRows) {
                         closingBalance = closingBalance.add(tbRow.getAmount());
                         tbRow.setClosingBalance(closingBalance);
                     }
                 }
                 this.trialBalanceRepositoryWrapper.save(tbRows);
             }
         }
+    }
+
+    @Override
+    @CronTarget(jobName = JobName.UPDATE_LOAN_SUMMARY)
+    public void updateLoanSummaryDetails(@SuppressWarnings("unused") final 
Map<String, String> jobParameters) throws JobExecutionException {
+
+        final int 
threadPoolSize=Integer.parseInt(jobParameters.get("thread-pool-size"));
+        final String officeId = jobParameters.get("officeId");
+        final ExecutorService executorService = 
Executors.newFixedThreadPool(threadPoolSize);
+        final Office office = 
this.officeRepository.findOne(Long.parseLong(officeId));
+
+        List<Callable<Object>> posters = new ArrayList<Callable<Object>>();
+
+        //Get the  total count of loans
+        Integer 
numberofLoans=loanReadPlatformService.retrieveNumberOfActiveLoans();
+
+        Integer batchSize = (int) Math.ceil(numberofLoans/ threadPoolSize);
+        Integer maxLoanId=0;
+        Integer offset=0;
+
+        do{
+            UpdateLoanSummaryPoster poster = (UpdateLoanSummaryPoster) 
this.applicationContext.getBean("updateLoanSummaryPoster");
+            poster.setLoanApplicationWriteService(loanApplicationWriteService);
+            poster.setTenant(ThreadLocalContextUtil.getTenant());
+            poster.setBatchSize(batchSize);
+            maxLoanId+=batchSize+1;
+            poster.setMaxLoanId(maxLoanId);
+            poster.setOffSet(offset);
+            offset+=batchSize;
+            poster.setOfficeHierachy(office.getHierarchy()+ "%");
+            posters.add(Executors.callable(poster));
+            numberofLoans-=batchSize;
+        }while(numberofLoans>=0);
+
+        try {
+            List<Future<Object>> responses = 
executorService.invokeAll(posters);
+            checkCompletion(responses);
+        } catch (InterruptedException e1) {
+            e1.printStackTrace();
+        }
+    }
+
+    //checks the execution of task by each thread in the executor service
+    private void checkCompletion(List<Future<Object>> responses) {
+        try {
+            for(Future f : responses) {
+                f.get();
+            }
+            boolean allThreadsExecuted = false;
+            int noOfThreadsExecuted = 0;
+            for (Future<Object> future : responses) {
+                if (future.isDone()) {
+                    noOfThreadsExecuted++;
+                }
+            }
+            allThreadsExecuted = noOfThreadsExecuted == responses.size();
+            if(!allThreadsExecuted)
+                logger.error("All threads could not execute.");
+        } catch (InterruptedException e1) {
+            e1.printStackTrace();
+            logger.error("Interrupted while posting IRD entries", e1);
+        }  catch (ExecutionException e2) {
+            e2.printStackTrace();
 
 Review comment:
   remove this as well

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to