0

I have the following method that creates and deploys applications in different PaaS:

private void deployModulesInPaaS() {

        ExecutorService executor = Executors.newFixedThreadPool(listModules
                .size());

        ModuleParsed mod;

        for (Iterator<ModuleParsed> iterator = listModules.iterator(); iterator
                .hasNext();) {
            mod = (ModuleParsed) iterator.next();
            try {
                switch (mod.getId_paas()) {
                case 1:                     
                    GAEDeployer gaeDeployer = new GAEDeployer(mod.getId_paas(),
                            mod.getId_component(), "//whatever/path");
                    FutureTask<URI> gaeFuture = new FutureTask<URI>(gaeDeployer);
                    executor.execute(gaeFuture);
                    mod.setDeployedURI(gaeFuture.get());
                    break;
                case 2:
                    AzureDeployer azureDeployer = new AzureDeployer(
                            "subscription", "path_certificate", "password",
                            "storageAccountName", "storageAccountKey");
                    FutureTask<URI> azureFuture = new FutureTask<URI>(
                            azureDeployer);
                    executor.execute(azureFuture);
                    mod.setDeployedURI(azureFuture.get());
                    break;                  
                default:
                    System.out.println("The PaaS identifier of module "
                            + mod.getId_component() + " is unknown.");
                    break;
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

    }

How can I call another method once all FutureTask have finished their computations? I have read about Command pattern and about Listener but I'm not sure if these would be the right ones nor how to implement them in this case.

eskalera
  • 1,072
  • 2
  • 21
  • 36

1 Answers1

0

You really need ListenableFuture, please search the keyword "fan-in" on this page.

Another way with CountDownLatch and overriding FutureTask.done(),not recommended:

private void deployModulesInPaaS() {

    CountDownLatch countDownLatch = new CountDownLatch(listModules.size());

    ExecutorService executor = Executors.newFixedThreadPool(listModules
            .size());

    ModuleParsed mod;

    for (Iterator<ModuleParsed> iterator = listModules.iterator(); iterator
            .hasNext();) {
        mod = (ModuleParsed) iterator.next();
        try {
            switch (mod.getId_paas()) {
            case 1:
                GAEDeployer gaeDeployer = new GAEDeployer(mod.getId_paas(),
                        mod.getId_component(), "//whatever/path");
                FutureTask<URI> gaeFuture = new FutureTask<URI>(gaeDeployer) {
                    @Override
                    protected void done() {

                        super.done();
                        countDownLatch.countDown();
                    }
                };
                executor.execute(gaeFuture);
                mod.setDeployedURI(gaeFuture.get());
                break;
            case 2:
                AzureDeployer azureDeployer = new AzureDeployer(
                        "subscription", "path_certificate", "password",
                        "storageAccountName", "storageAccountKey");
                FutureTask<URI> azureFuture = new FutureTask<URI>(
                        azureDeployer) {
                    @Override
                    protected void done() {

                        super.done();
                        countDownLatch.countDown();
                    }
                };
                executor.execute(azureFuture);
                mod.setDeployedURI(azureFuture.get());
                break;
            default:
                System.out.println("The PaaS identifier of module "
                        + mod.getId_component() + " is unknown.");
                break;
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    countDownLatch.await();
    // do finally
}
Anderson
  • 2,496
  • 1
  • 27
  • 41