Home CompletableFuture not blocking during join when inside a stream
Reply: 0

CompletableFuture not blocking during join when inside a stream

Criel
1#
Criel Published in 2018-01-11 21:34:12Z

I have been working on this for a few weeks now off and on, thought I had it working but seems like sporadically some of the information we are getting back from the various cruds we hit is not included in the response. I can only assume this is due to some processing not getting included in the mapper object since we aren't 'joining' the responses and waiting for all processing to complete.

@Autowired
FinancialsExecutor financialsExecutor;

@Autowired
AccountDetailsExecutor accountDetailsExecutor;

@Autowired
AccountDemographicsExecutor accountDemographicsExecutor;


List<AccountPojo> accountList; //can contain 1-100 account numbers 

Map<String, CompletableFuture<Pojo1>> accountDetails = new HashMap<>();
Map<String, CompletableFuture<Pojo2>> accountDemographic = new HashMap<>();
Map<String, CompletableFuture<Pojo3>> accountFinancials = new HashMap<>();

if (!accountList.isEmpty()) {
    accountList.stream()
        .forEach(x -> {
            accountDemographic.put(x.getAccountNumber(), 
                CompletableFuture.supplyAsync(() -> {       
                    return async.getAccountDemographics(x);                             
                },
                AccountDemographicsExecutor.concurrentTaskExecutor()).join());
            accountDetails.put(x.getAccountNumber(), 
                CompletableFuture.supplyAsync(() -> {                               
                    return async.getAccountDetails(x);                                                                                      
                },
                AccountDetailsExecutor.concurrentTaskExecutor()).join());
            accountFinancials.put(x.getAccountNumber(),
                CompletableFuture.supplyAsync(() -> {                           
                   return async.getAccountFinancials(x);                                                                                            
                },
                FinancialsExecutor.concurrentTaskExecutor()).join());                                                                                               
        });
    }   

@Async("accountDemographicsExecutor")
public CompletableFuture<POjo2> getAccountDemographics(AccountPojo account) {
    ..
    return CompletableFuture.completedFuture(crudResponse)
}

The autowired executors are custom made executors to run every async call on it's own executor threadpool. Example below :

@Configuration
public class AccountDemographicsExecutor {

    public AccountDemographicsExecutor (){
    }

    @Bean(name = "accountDemographicsExecutor")
    public Executor concurrentTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(60);
        executor.setMaxPoolSize(120);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("acctD-Exec-");
        executor.initialize();
        return executor;
    }
} 

Since we have a map of String keys and CompletableFuture values, is there some quick way to join these besides having to loop through each one of them and joining them manually? The join() in the forEach stream doesn't seem to be joining them as was expected.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31442 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO