[英]Recommended way to install glassfish as a service with install4j?
我們使用的是install4j 6.1.5版,我需要將glassfish安裝為Windows服務。 因此,我閱讀了文檔並嘗試實現Windows服務包裝器類,以便能夠為安裝創建服務啟動器,以將其用於服務操作。 這是代碼:
public class WindowsServiceWrapper implements Runnable {
private static volatile boolean keepRunning = true;
public static void main(String[] args) {
final Thread mainThread = Thread.currentThread();
String glassfishInstallDir = System.getProperty("installation.dir");
if (glassfishInstallDir == null || glassfishInstallDir.isEmpty()) {
System.err.println("Path not set to glassfish installation but required for startup the service.");
System.exit(1);
} else if (!new File(glassfishInstallDir).exists()) {
System.err.println("Path '" + glassfishInstallDir + "' do not exists. Startup of the service aborted.");
System.exit(1);
}
System.out.println("Starting the service.");
String asadminPath = glassfishInstallDir + "/bin/asadmin.bat";
CommandLine commandLine = CommandLine.parse(asadminPath + " start-domain");
runCommand(commandLine);
// register to jvm shutdown to handle service shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
keepRunning = false;
try {
System.out.println("Shutdown of the service was triggered.");
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
new WindowsServiceWrapper(asadminPath).run();
}
private final String ASADMIN_PATH;
private WindowsServiceWrapper(String asadminPath) {
ASADMIN_PATH = asadminPath;
}
public void run() {
while (keepRunning) {
// nothing to do here but waiting for shutdown hook
}
System.out.println("Shutdown the service.");
CommandLine commandLine = CommandLine.parse(ASADMIN_PATH + " stop-domain");
runCommand(commandLine);
}
private static int runCommand(CommandLine cmdLine) {
DefaultExecutor executor = new DefaultExecutor();
int exitCode;
try {
exitCode = executor.execute(cmdLine);
} catch (IOException e) {
System.out.println("Error occured during command execution: " + cmdLine.toString());
e.printStackTrace();
exitCode = 1;
keepRunning = false;
}
return exitCode;
}
}
要啟動和停止glassfish服務器,我使用了Apache的commons-exec庫。 此代碼可以很好地安裝和啟動服務以及卸載服務。 剩下的只有一件事:在服務啟動期間,我需要等待,直到glassfish啟動完全完成並且commons-exec調用將完成。 需要進行相關的安裝步驟。 現在,“啟動服務”操作將立即返回。 我想我包裝器類的主要方法被實例化為未分叉的線程,是嗎? 那么,有沒有推薦的方法來處理這樣的事情?
您可以使用“等待HTTP服務器”操作來等待服務器啟動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.