我有一個用例需要我從應用程序內部控制Glassfish服務器上應用程序的部署(以及其他)。以編程方式在Glassfish3 +上部署應用程序
是否可以在glassfish服務器上使用一個應用程序來部署和控制同一個glassfish服務器上的其他應用程序?
如果任何人有任何建議,出發點,演示,警告等將非常感激。
感謝
我有一個用例需要我從應用程序內部控制Glassfish服務器上應用程序的部署(以及其他)。以編程方式在Glassfish3 +上部署應用程序
是否可以在glassfish服務器上使用一個應用程序來部署和控制同一個glassfish服務器上的其他應用程序?
如果任何人有任何建議,出發點,演示,警告等將非常感激。
感謝
雖然GlassFish的可獨立啓動,it can also be embedded into your own application(不要以爲Java EE的在這裏,但簡單的(或不那麼簡單)的Java應用程序)。
您可以通過這些API定義一組管理API,啓動和配置GlassFish以及manage that glassfish instance。將API暴露給運行在glassfish下的應用程序也應該是可行的。
我認爲在glassfish中自動部署應用程序的最簡單方法是使用glassfish的autodeploy文件夾(位於glassfish \ domains \ autodeploy中)。每個戰爭或jar複製到該文件夾將自動部署在服務器(如果它正常工作)。
因此,您只需使用計劃控制(如Quartz和一些方法)在服務器中複製文件(例如,將工作目錄複製到autodeploy文件夾中)即可。
另一種選擇是在應用程序中的東西,如運行shell腳本這個
$ ./asadmin部署--name \ --contextroot /absolute/path/to/.war
我相信基本的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.jar
和development-client.jar
文件。您可以在glassfish安裝目錄下的「glassfish-4.0 \ glassfish \ modules \」目錄下找到development-client.jar
。
更新:我測試了Netbeans 7.4的應用程序,它在IDE內工作,但在IDE之外,它產生了一個錯誤消息,不容易修復,因爲沒有線索出現問題。該錯誤消息是
javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException:無法獲取的DeploymentManager
的根本原因是,一些必需的庫失蹤。在瀏覽完整的glassfish庫列表後,我發現如果我們想要獨立運行應用程序,那麼需要以下庫。找到完整列表下文:
ŧ漢克斯理查德。 – tarka 2012-08-13 06:49:10