0

我有以下方法可以在不同的 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();
            }
        }

    }

FutureTask一旦所有人都完成了计算,我该如何调用另一个方法?我已经阅读了关于Command模式和关于Listener但我不确定这些是否是正确的,也不确定在这种情况下如何实现它们。

4

1 回答 1

0

您确实需要ListenableFuture,请在本页搜索关键词“fan-in”。

不推荐使用CountDownLatch和覆盖的另一种方法:FutureTask.done()

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
}
于 2013-10-27T13:05:37.883 回答