App下載

將 JAVA 應(yīng)用程序安裝為 WINDOWS 服務(wù)

廢話輸出機(jī)器 2021-09-24 11:38:50 瀏覽數(shù) (2542)
反饋

這聽起來(lái)像是您永遠(yuǎn)不需要的東西,但有時(shí),當(dāng)您分發(fā)最終用戶軟件時(shí),您可能需要安裝 Java 程序作為 Windows 服務(wù)。

一個(gè)很少人擁有的重要先決條件——擁有一個(gè)捆綁的 JRE,所以沒有人必須下載和安裝一個(gè)JRE(會(huì)使安裝過(guò)程不必要地復(fù)雜化,并且目標(biāo)受眾不一定精通技術(shù))。

所以,用jar打包的maven項(xiàng)目,我首先想到的是打包一個(gè)exe(用?launch4j?),然后注冊(cè)為服務(wù)。問題在于 java 程序使用預(yù)定的執(zhí)行程序,因此它永遠(yuǎn)不會(huì)退出,這使得將其作為進(jìn)程啟動(dòng)是不可能的。

所以我不得不使用? ?commons-daemon? ?procrun?對(duì)其進(jìn)行"守護(hù)"。在此之前,我必須將所需的每個(gè)組件組裝到一個(gè)目標(biāo)文件夾中——?fat jar?(包括所有依賴項(xiàng))、JRE、commons-daemon 二進(jìn)制文件和配置文件。

相關(guān)位是(其中?${installer.dir}?是?${project.basedir}/target/installer}?):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>opendata-ckan-pusher</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <copy todir="${installer.dir}/jre1.8.0_91">
                        <fileset dir="${project.basedir}/jre1.8.0_91" />
                    </copy>
                    <copy todir="${installer.dir}/commons-daemon">
                        <fileset dir="${project.basedir}/commons-daemon" />
                    </copy>
                    <copy file="${project.build.directory}/opendata-ckan-pusher.jar" todir="${installer.dir}" />
                    <copy file="${project.basedir}/install.bat" todir="${installer.dir}" />
                    <copy file="${project.basedir}/uninstall.bat" todir="${installer.dir}" />
                    <copy file="${project.basedir}/config/pusher.yml" todir="${installer.dir}" />
                    <copy file="${project.basedir}/LICENSE" todir="${installer.dir}" />
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

您會(huì)注意到 installer.bat 和 uninstaller.bat 是使用 commons-daemon 管理服務(wù)的文件。安裝程序創(chuàng)建服務(wù)。Commons-daemon 有 3 種模式:exe(允許你包裝任意可執(zhí)行文件)、Java(類似于 exe,但用于 java 應(yīng)用程序)和 jvm(在同一進(jìn)程中運(yùn)行 java 應(yīng)用程序)。

我可以使用所有三個(gè)選項(xiàng)(包括 launch4j 創(chuàng)建的 exe),但是 jvm 允許您使用指定的方法來(lái)控制正在運(yùn)行的應(yīng)用程序。StartClass/StartMethod/StopClass/StopMethod 參數(shù)就是為了這個(gè)。這是整個(gè) installer.bat:

commons-daemon\prunsrv //IS//OpenDataPusher --DisplayName="OpenData Pusher" --Description="OpenData Pusher"^
     --Install="%cd%\commons-daemon\prunsrv.exe" --Jvm="%cd%\jre1.8.0_91\bin\client\jvm.dll" --StartMode=jvm --StopMode=jvm^
     --Startup=auto --StartClass=bg.government.opendatapusher.Pusher --StopClass=bg.government.opendatapusher.Pusher^
     --StartParams=start --StopParams=stop --StartMethod=windowsService --StopMethod=windowsService^
     --Classpath="%cd%\opendata-ckan-pusher.jar" --LogLevel=DEBUG^ --LogPath="%cd%\logs" --LogPrefix=procrun.log^
     --StdOutput="%cd%\logs\stdout.log" --StdError="%cd%\logs\stderr.log"
      
      
commons-daemon\prunsrv //ES//OpenDataPusher

幾點(diǎn)說(shuō)明:

  • jvm參數(shù)指向jvm dll
  • StartClass/StartMethod/StopClass/StopMethod 指向用于控制正在運(yùn)行的應(yīng)用程序的指定方法。在這種情況下,starting 只會(huì)調(diào)用 main 方法,stoping 會(huì)關(guān)閉調(diào)度的執(zhí)行器,以便應(yīng)用程序可以退出
  • classpath 參數(shù)指向fat jar
  • 使用 %cd% 確定當(dāng)前目錄的路徑有風(fēng)險(xiǎn),但由于最終用戶將始終從它所在的目錄啟動(dòng)它,因此在這種情況下是安全的。

在windowsService看起來(lái)像這樣:

public static void windowsService(String args[])throws Exception {
     String cmd ="start";
     if (args.length >0) {
        cmd = args[0];
    }
 
    if ("start".equals(cmd)) {
        Pusher.main(new String[]{});
    }else {
        executor.shutdownNow();
        System.exit(0);
    }
}

然后我有一個(gè)“安裝程序”文件夾,其中包含 jre 和 commons-daemon 文件夾以及兩個(gè) bat 文件和一個(gè) fat jar。然后我可以將其打包為可自解壓的存檔并分發(fā)(當(dāng)然還有手冊(cè))。我也查看了?IzPack?,但找不到如何捆綁 JRE(也許你可以)。這里的一個(gè)重要注意事項(xiàng)是您可能遇到的 32 位/64 位問題。這就是為什么捆綁 32 位 JRE 并使用 32 位(默認(rèn))prunsrv.exe 更安全的原因。

這是一個(gè)非常小眾的場(chǎng)景——通常我們?yōu)椴渴鸬?Linux 服務(wù)器而開發(fā),但有時(shí)可能需要為使用 Java 的大型組織提供本地工具。在我的例子中,長(zhǎng)時(shí)間運(yùn)行的部分是一個(gè)預(yù)定的執(zhí)行器,但它也可以運(yùn)行一個(gè)提供 Web 界面的碼頭服務(wù)。為什么要這樣做,而不是提供 URL——在訪問本地機(jī)器很重要的情況下。它甚至可以是一個(gè)分布式搜索引擎或另一個(gè)你想用 Java 編寫的 p2p 軟件。


0 人點(diǎn)贊