2

アプリケーション内から Glassfish サーバー上のアプリケーションの展開を (とりわけ) 制御する必要があるユース ケースがあります。

Glassfish サーバー上の 1 つのアプリケーションを使用して、同じ Glassfish サーバー上の他のアプリケーションを展開および制御することはできますか?

4

3 に答える 3

1

Glassfish はスタンドアロンで起動できますが、独自のアプリケーションに組み込むこともできます(ここでは Java EE とは考えないでください。単純な (またはそれほど単純ではない) Java アプリです)。

管理 API のセットを定義し、GlassFish を起動して構成し、それらの API を介してその GlassFish インスタンスを管理できます。Glassfish で実行されているアプリケーションに API を公開することも可能です。

于 2012-08-11T21:35:44.637 に答える
0

glassfishにアプリケーションを自動的にデプロイする最も簡単な方法は、glassfishのautodeployフォルダー(glassfish \ domains \ autodeployにあります)を使用することだと思います。そのフォルダーにコピーするすべてのwarまたはjarは、サーバーに自動的にデプロイされます(正しく機能する場合)。

したがって、必要なのは、Quartzなどのスケジュールコントロールといくつかのメソッドを使用して、サーバー内のファイルをコピーすることです(たとえば、作業ディレクトリからautodeployフォルダーに)。

別のオプションは、次のようなものを使用してアプリケーションでシェルスクリプトを実行することです

$ ./asadmin deploy --name \ --contextroot/absolute/path/to/.war

于 2012-08-13T09:50:12.837 に答える
0

基本的な Glassfish サーバー管理は、JSR88 API を使用して処理できると思います。これらの API は Java EE 7 ではオプションとしてマークされていますが、機能します。これらの API は、Java SE と EE の両方のアプリケーションで使用できると思います。

すべての主要な Java EE コンテナーを操作するために使用できるラッパー API がここにあります。これらの API も JSR88 を使用します。

Java EE アプリケーションをデプロイ/アンデプロイするためのサンプル コードがここにあります。このサンプルは、古いバージョンの Glassfish (おそらく Glassfish2x) で動作します。

ここに投稿している Glassfish4x で動作するように、サンプル コードを少し変更しました。

package simplewardeployer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.deploy.shared.ModuleType;
import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.TargetModuleID;
import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
import javax.enterprise.deploy.spi.exceptions.TargetException;
import javax.enterprise.deploy.spi.factories.DeploymentFactory;
import javax.enterprise.deploy.spi.status.ProgressEvent;
import javax.enterprise.deploy.spi.status.ProgressListener;
import javax.enterprise.deploy.spi.status.ProgressObject;


public class Main {

class DeploymentListener implements ProgressListener {

    Main driver;

    String warContext;

    DeploymentListener(Main driver, String warContext) {

        this.driver = driver;

        this.warContext = warContext;

    }

    public void handleProgressEvent(ProgressEvent event) {

        System.out.println(event.getDeploymentStatus().getMessage());

        if (event.getDeploymentStatus().isCompleted()) {

            try {

                TargetModuleID[] ids = getDeploymentManager().getNonRunningModules(ModuleType.WAR, getDeploymentManager().getTargets());

                TargetModuleID[] myIDs = new TargetModuleID[1];

                for (TargetModuleID id : ids) {

                    if (warContext.equals(id.getModuleID())) {

                        myIDs[0] = id;

                        ProgressObject startProgress = driver.getDeploymentManager().start(myIDs);

                        startProgress.addProgressListener(new ProgressListener() {

                            public void handleProgressEvent(ProgressEvent event) {

                                System.out.println(event.getDeploymentStatus().getMessage());

                                if (event.getDeploymentStatus().isCompleted()) {
                                    driver.setAppStarted(true);

                                }

                            }

                        });

                    }

                }

            } catch (IllegalStateException ex) {

                ex.printStackTrace();

            } catch (TargetException ex) {

                ex.printStackTrace();

            }

        }

    }

}

DeploymentManager deploymentManager;

boolean appStarted;

boolean appUndeployed;

String warContext;

String warFilename;

String wsdlUrl;

synchronized void setAppStarted(boolean appStarted) {

    this.appStarted = appStarted;

    notifyAll();

}

synchronized void setAppUndeployed(boolean appUndeployed) {

    this.appUndeployed = appUndeployed;

    notifyAll();

}

private String getParam(String param) {

    return (null == deploymentProperties) ? null : deploymentProperties.getProperty(param);

}

public DeploymentManager getDeploymentManager() {

    if (null == deploymentManager) {

        DeploymentFactoryManager dfm = DeploymentFactoryManager.getInstance();

        try {

            Class dfClass = Class.forName(getParam("jsr88.df.classname"));

            DeploymentFactory dfInstance;

            dfInstance = (DeploymentFactory) dfClass.newInstance();

            dfm.registerDeploymentFactory(dfInstance);

        } catch (ClassNotFoundException ex) {

            ex.printStackTrace();

        } catch (IllegalAccessException ex) {

            ex.printStackTrace();

        } catch (InstantiationException ex) {

            ex.printStackTrace();

        }

        try {

            deploymentManager
                    = dfm.getDeploymentManager(
                            getParam("jsr88.dm.id"), getParam("jsr88.dm.user"), getParam("jsr88.dm.passwd"));

        } catch (DeploymentManagerCreationException ex) {

            ex.printStackTrace();

        }

    }

    return deploymentManager;

}

TargetModuleID getDeployedModuleId(String warContext) {       
    TargetModuleID foundId= null;
    TargetModuleID[] ids = null;
    try {
        ids = getDeploymentManager().getAvailableModules(ModuleType.WAR, getDeploymentManager().getTargets());
        for (TargetModuleID id : ids) {
            if (warContext.equals(id.getModuleID())) {
                foundId = id;
                break;
            }
        }
    } catch (TargetException | IllegalStateException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    return foundId;
}

void runApp(String warFilename, String warContext) {

    setAppStarted(false);
    ProgressObject deplProgress;
    TargetModuleID foundId = getDeployedModuleId(warContext);
    if (foundId != null) {
        TargetModuleID[] myIDs = new TargetModuleID[1];
        myIDs[0] = foundId;
        deplProgress = getDeploymentManager().redeploy(myIDs, new File(warFilename), null);
    } else {
        deplProgress = getDeploymentManager().distribute(getDeploymentManager().getTargets(),
                new File(warFilename), null);
    }
    if (deplProgress != null) {
        deplProgress.addProgressListener(new DeploymentListener(this, warContext));
        waitForAppStart();
    }

}

void undeployApp(String warContext) {

    setAppUndeployed(false);

    try {

        TargetModuleID[] ids = getDeploymentManager().getRunningModules(ModuleType.WAR, getDeploymentManager().getTargets());

        TargetModuleID[] myIDs = new TargetModuleID[1];

        for (TargetModuleID id : ids) {

            if (warContext.equals(id.getModuleID())) {

                myIDs[0] = id;

                ProgressObject startProgress = getDeploymentManager().undeploy(myIDs);

                startProgress.addProgressListener(new ProgressListener() {

                    public void handleProgressEvent(ProgressEvent event) {

                        System.out.println(event.getDeploymentStatus().getMessage());

                        if (event.getDeploymentStatus().isCompleted()) {

                            setAppUndeployed(true);

                        }

                    }

                });

            }

        }

    } catch (IllegalStateException ex) {

        ex.printStackTrace();

    } catch (TargetException ex) {

        ex.printStackTrace();

    }

    waitForAppUndeployment();

}

void releaseDeploymentManager() {

    if (null != deploymentManager) {

        deploymentManager.release();

    }

}

synchronized void waitForAppStart() {

    while (!appStarted) {

        try {

            wait();

        } catch (InterruptedException e) {
        }

    }

}

synchronized void waitForAppUndeployment() {

    while (!appUndeployed) {

        try {

            wait();

        } catch (InterruptedException e) {
        }

    }

}

public Main() {

}

public Main(String filename) {

    setProperties(filename);

}

private final static String SyntaxHelp = "syntax:\\n\\tdeploy \\n\\tundeploy ";

private final static String PropertiesFilename = "wardeployment.properties";

private Properties deploymentProperties;

private void setProperties(String filename) {

    FileInputStream fis = null;

    try {

        fis = new FileInputStream(filename);

        deploymentProperties = new Properties();

        deploymentProperties.load(fis);

        fis.close();

    } catch (FileNotFoundException ex) {

        ex.printStackTrace();

    } catch (IOException ex) {

        ex.printStackTrace();

    }

}

private static void printHelpAndExit() {

    System.out.println(SyntaxHelp);

    System.exit(1);

}

public static void main(String[] args) {

    if (args.length < 1) {

        printHelpAndExit();

    }

    Main worker = new Main(PropertiesFilename);

    if ("deploy".equals(args[0])) {

        System.out.println("Deploying app...");
        String warContext = new File(args[1]).getName();
        warContext = warContext.substring(0, warContext.length() - 4);

        worker.runApp(args[1], warContext);

        worker.releaseDeploymentManager();

    } else if ("undeploy".equals(args[0])) {

        System.out.println("Undeploying app...");
        String warContext = new File(args[1]).getName();
        warContext = warContext.substring(0, warContext.lastIndexOf("."));

        worker.undeployApp(warContext);

        worker.releaseDeploymentManager();

        }

    }

}

変更されたwardeployment.propertiesファイルは次のとおりです。

jsr88.dm.id=deployer:Sun:AppServer::10.9.80.117:4848
jsr88.dm.user=admin
jsr88.dm.passwd=adminadmin
jsr88.df.classname=org.glassfish.deployapi.SunDeploymentFactory

クラスパスjavaee-api-7.0.jarにファイルを追加する必要があります。「glassfish-4.0\glassfish\modules\」ディレクトリの下のglassfishインストールディレクトリにありますdevelopment-client.jardevelopment-client.jar

更新: Netbeans 7.4 のアプリケーションをテストしたところ、IDE 内では動作しましたが、IDE の外部ではエラー メッセージが生成されました。これは、問題がどこにあるのか手がかりがなかったため、修正が容易ではありませんでした。エラーメッセージは

javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException: DeploymentManager を取得できませんでした

基本的な理由は、いくつかの必要なライブラリーが欠落していたことです。Glassfish ライブラリの全リストを確認した後、アプリケーションをスタンドアロンで実行するには、次のライブラリが必要であることがわかりました。完全なリストは次のとおりです。

  1. admin-cli.jar
  2. admin-util.jar
  3. cglib.jar
  4. common-util.jar
  5. config-types.jar
  6. core.jar
  7. 展開クライアント.jar
  8. デプロイメント共通.jar
  9. glassfish-api.jar
  10. hk2-api.jar
  11. hk2-config.jar
  12. hk2-locator.jar
  13. hk2-utils.jar
  14. internal-api.jar
  15. javax.enterprise.deploy-api.jar
  16. javax.inject.jar
  17. osgi-resource-locator.jar
于 2013-11-13T14:32:03.933 に答える