0

I am trying to build a callable method, in order to execute it every minute. The problem is that my method needs to use an autowired field, which remains null in the callable task, even though before turning this method into a callable one it worked.

Pieces of code:

List<AccountKey> initialLoadKeys = initialLoadController.startInitialLoad();
        for (int i = 0; i < initialLoadKeys.size(); i++) {
            try {
                FutureTask<Void> task = new FutureTask(new AccountController(initialLoadKeys.get(i)));

                ScheduledExecutorService es = Executors.newScheduledThreadPool(1);
                es.scheduleAtFixedRate(task, 1,1, TimeUnit.MINUTES);
                es.execute(task);
                es.shutdown();
            } catch (RuntimeException e) {
                logger.debug(e);
            }
        }
public class AccountController implements Callable<Void>{

    public AccountController () {
    }

    private AccountKey accountKey;

    public AccountController(AccountKey accountKey) {
        this.accountKey= accountKey;
    }

    private AccountManagerWebClient accountManagerWebClient;

    public AccountManagerWebClient getAccountManagerWebClient() {
        return accountManagerWebClient;
    }

    public void setAccountManagerWebClient(AccountManagerWebClient accountManagerWebClient) {
        this.accountManagerWebClient = accountManagerWebClient;
    }

    @Override
    @Transactional(value = "clientTransaction", propagation = Propagation.REQUIRES_NEW)
    public Void call() throws Exception {

        accountManagerWebClient.prepareWebServiceRequest(accountKey);
        accountManagerWebClient.callWebService(accountKey);

 accountManagerWebClient.obtainAccountManagerObjectFromServiceResponseAndPersist(accountKey);
        return null;
}

}

The autowired class which is null is accountManagerWebClient...I tried to add this into the constructor, as I did with accountKey, but it did not work. As I mentioned above, it worked before using Callable, but I really have to make usage of this in order to schedule the execution. Can anyone help ?

Maria1995
  • 439
  • 1
  • 5
  • 21
  • You instantiate the `AccountController` in your `FutureTask`, so it is not a `Spring` bean. What I do not understand is why adding the `AccountManagerWebClient` to the constructor does not work. Is it instantiated from where construct the future task? – hotzst Nov 10 '19 at 13:06
  • @hotzst the problem is that AccountManagerWebClient has a lot of autowired fields and they are all returned as being null if I add it in the constructor – Maria1995 Nov 10 '19 at 14:52

0 Answers0