【问题标题】:Deploy Application on Glassfish3+ programmatically以编程方式在 Glassfish3+ 上部署应用程序
【发布时间】:2019-01-03 13:54:10
【问题描述】:

我有一个用例要求我从应用程序内部控制 Glassfish 服务器上的应用程序的部署(除其他外)。

是否可以在 glassfish 服务器上使用一个应用程序来部署和控制同一 glassfish 服务器上的其他应用程序?

【问题讨论】:

    标签: jakarta-ee glassfish-3


    【解决方案1】:

    虽然 glassfish 可以独立启动,但 it can also be embedded into your own application(这里不要认为 Java EE,而是简单(或不那么简单)的 Java 应用程序)。

    您可以通过这些 API 定义一组管理 API、启动和配置 GlassFish 以及 manage that glassfish instance。将您的 API 暴露给在 glassfish 下运行的应用程序也应该是可行的。

    【讨论】:

      【解决方案2】:

      我认为在 glassfish 中自动部署应用程序的最简单方法是使用 glassfish 的 autodeploy 文件夹(位于 glassfish\domains\autodeploy 中)。您复制到该文件夹​​的每个 war 或 jar 都将自动部署在服务器中(如果它工作正常)。

      所以您需要做的就是使用 Quartz 之类的计划控件和几种方法来复制服务器中的文件(例如,复制到您的工作目录到 autodeploy 文件夹)。

      另一种选择是在您的应用程序中使用类似这样的方式运行 shell 脚本

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

      【讨论】:

        【解决方案3】:

        我相信基本的 Glassfish 服务器管理可以使用 JSR88 API 来处理。尽管这些 API 在 Java EE 7 中被标记为可选,但它们可以工作。我认为您可以在 Java SE 和 EE 应用程序中使用这些 API。

        有可用的包装 API here 可用于操作所有主要的 Java EE 容器。这些 API 也使用 JSR88。

        有一个示例代码here 可用于部署/取消部署 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.jardevelopment-client.jar 文件。您可以在“glassfish-4.0\glassfish\modules\”目录下的 glassfish 安装目录中找到 development-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. 部署-client.jar
        8. 部署-common.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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-27
          • 1970-01-01
          • 2016-02-26
          • 1970-01-01
          • 2011-06-04
          • 1970-01-01
          相关资源
          最近更新 更多