我正在尝试使用 Apache Commons Daemon 1.2.0 将嵌入式码头作为 Windows 服务运行。我可以使用 java -jar 运行嵌入式码头 jar 并成功启动服务器,但是当我尝试使用 Apache Daemon 运行它时它不起作用。
我正在使用 maven shade 插件来生成嵌入式 jar。
使用以下命令使用 prunsrv 安装后启动服务时,日志中会打印以下内容并且服务未启动:
prunsrv.exe //IS//winserv --Description="winserv jetty Service" --Classpath="D:\procrun\winserv.jar" --StartMode=jvm --StartClass=com.ebdjetty.winserv.App --StartParams=start --StdOutput=auto --StdError=auto --JavaHome="C:\Program Files (x86)\Java\jre1.8.0_211\bin" --StartMethod=startServer --Jvm="C:\Program Files (x86)\Java\jre1.8.0_211\bin\client\jvm.dll" --StopMode=jvm --StopMethod=stopServer --StopParams=stop --StopClass=com.ebdjetty.winserv.App --StdOutput=D:\procrun\logs\stdOut.log --StdError=D:\procrun\logs\stdErr.log --LogLevel=Debug --LogPath=D:\procrun\logs\ --LogPrefix=winserv-daemon
winserv 守护进程日志:
[2019-08-19 19:22:06] [debug] ( prunsrv.c:1754) [16324] Apache Commons Daemon procrun log initialized.
[2019-08-19 19:22:06] [info] ( prunsrv.c:1758) [16324] Apache Commons Daemon procrun (1.2.0.0 32-bit) started.
[2019-08-19 19:22:06] [info] ( prunsrv.c:1668) [16324] Running Service 'winserv'...
[2019-08-19 19:22:06] [debug] ( prunsrv.c:1441) [ 2984] Inside ServiceMain...
[2019-08-19 19:22:06] [debug] ( prunsrv.c:904 ) [ 2984] reportServiceStatusE: dwCurrentState = 2, dwWin32ExitCode = 0, dwWaitHint = 3000, dwServiceSpecificExitCode = 0.
[2019-08-19 19:22:06] [info] ( prunsrv.c:1196) [ 2984] Starting service...
在说明使用 Windows 管理工具 > 服务时,我得到:
Error 1067:The process terminated unexpectedly.
试过此服务将无法启动:错误 1067:进程意外终止,但没有帮助。
下面是创建嵌入式码头 jar 的代码: 启动码头的类:
package com.ebdjetty.winserv;
import org.eclipse.jetty.server.Server;
public class App
{
static Server server = null;
public static void main( String[] args )
{
startServer(args);
}
public static void startServer(String[] args) {
System.out.println("In startServer method.");
try {
start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void stopServer(String[] args) {
System.out.println("In stopServer method.");
try {
stop();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void start()throws Exception{
server = new Server(9092);
System.out.println("Starting server on port 9092");
server.start();
System.out.println("Started");
server.join();
}
public static void stop()throws Exception{
System.out.println("Stopping Server");
server.stop();
}
}
pom.xml 用于使用 maven shade 生成嵌入式码头 jar:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ebdjetty.winserv</groupId>
<artifactId>AppEbdTest</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>winserv</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<servlet-api.version>3.1.0</servlet-api.version>
<jetty.version>9.4.15.v20190215</jetty.version>
<!-- Plugin versions -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.4.15.v20190215</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
</dependencies>
<build>
<finalName>winserv</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.ebdjetty.winserv.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>