以编程方式在Glassfish3 +上部署应用程序

时间:2012-08-06 08:31:23

标签: java-ee glassfish-3

我有一个用例,要求我从应用程序中控制Glassfish服务器上的应用程序的部署(以及其他事项)。

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

另一种选择是使用类似这样的

在您的应用程序中运行shell脚本
  

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

答案 2 :(得分:0)

我相信可以使用JSR88 API处理基本的Glassfish服务器管理。虽然这些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安装目录中的“glassfish-4.0 \ glassfish \ modules \”目录下找到development-client.jar

更新:我测试了Netbeans 7.4中的应用程序并且它在IDE中工作但是在IDE之外它生成了一个错误消息,这个消息不容易解决,因为没有线索问题出在哪里。错误消息是

javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException:无法获取DeploymentManager

基本原因是缺少一些必需的库。在浏览了glassfish库的完整列表之后,我发现如果我们想要独立运行应用程序,则需要以下库。查找下面的完整列表:

  1. 管理员-cli.jar
  2. 管理员-util.jar
  3. cglib.jar
  4. 共util.jar
  5. 配置-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. 内部api.jar文件
  15. javax.enterprise.deploy-api.jar文件
  16. javax.inject.jar
  17. OSGi的资源locator.jar