Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
This is my method:
private List<OfficerAccountFilter> createResult(List<Account> accountList) {
List<List<Account>> partedList = Lists.partition(accountList, MAX_LIMIT);
List<OfficerAccountFilter> result = new ArrayList<>();
System.out.println("size of account" + accountList.size());
int size = accountList.size() / 800;
ExecutorService executorService = Executors.newFixedThreadPool(NTHREDS);
List<Future<List<OfficerAccountFilter>>> futureList = new ArrayList<Future<List<OfficerAccountFilter>>>();
for (int i = 0; i <= size + 1; i++) {
Callable<List<OfficerAccountFilter>> worker = new AccountCallable(accountList);
Future<List<OfficerAccountFilter>> submit = executorService.submit(worker);
futureList.add(submit);
for (Future<List<OfficerAccountFilter>> future : futureList) {
try {
result = future.get();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
executorService.shutdown();
return result;
my callable class:
public class AccountCallable implements Callable<List<OfficerAccountFilter>> {
@Autowired
private OfficerAccountSOAPRunner officerAccountSOAPRunner;
private List<Account> accountList;
public AccountCallable(List<AccountIdentifier> accountList) {
this.accountList = accountList;
@Override
public List<OfficerAccountFilter> call() throws Exception {
List<OfficerAccountFilter> result = new ArrayList<>();
for (Account account : accountList) {
OfficerAccountRequest request = new OfficerAccountRequest();
request.getAccounts().add(account);
List<OfficerAccountFilter> accounts = officerAccountSOAPRunner.getAccounts(request);
result.addAll(accounts);
return result;
officerAccountSOAPRunner.getAccounts(request);
this is where im invoking Soap service which gives the filtered accounts back to officer. Not sure why is it returning null at this point. If i do it sequentially, it works absolutely fine. Am i not setting the future Object right?
Error stack:
java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.test.service.impl.OfficerServiceImpl.createResult(OfficerServiceImpl.java:92)
at com.test.ServiceImpl.limitOfficerAccounts(OfficerServiceImpl.java:61)
at com.test.service.impl.OfficerServiceImpl.getOfficerAccounts(OfficerServiceImpl.java:54)
Caused by: java.lang.NullPointerException
at com.test.impl.AccountIdentifierCallable.call(AccountCallable.java:36)
at com.test.impl.AccountIdentifierCallable.call(AccountCallable.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
The error is SOAPRunner call: List<OfficerAccountFilter> accounts = officerAccountSOAPRunner.getAccounts(request);
I put it in debug mode and it's coming as soon as i set on @Autowired. So it's coming from there. Do i need to set the cope differently?
–
–
The NullPointerException
is because you are autowiring your soap service in your callable:
@Autowired
private OfficerAccountSOAPRunner officerAccountSOAPRunner;
You are creating the instances of AccountCallable
yourself and not getting them from Spring, which is correct since you need to create the callables yourself. But Spring cannot then help you with autowiring the soap runner. So you should instead pass in OfficerAccountSOAPRunner
to your callable as a constructor parameter when your create it's instances in createResult
method.
You can get the OfficerAccountSOAPRunner
autowired in the class that contains createResult
method, so that then it can pass it to the AccountCallable
objects when it creates them.
On a side note, you don't need to create multiple callables. The following:
for (int i = 0; i <= size + 1; i++) {
Callable<List<OfficerAccountFilter>> worker = new AccountCallable(accountList);
Future<List<OfficerAccountFilter>> submit = executorService.submit(worker);
futureList.add(submit);
can be replaced with:
Callable<List<OfficerAccountFilter>> worker = new AccountCallable(accountList);
Future<List<OfficerAccountFilter>> future = executorService.submit(worker);
Then you can get the results from the single future:
future.get();
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.