Spring 启动 JAR 作为 Windows 服务 [英] Spring boot JAR as windows service

查看:31
本文介绍了Spring 启动 JAR 作为 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 procrun 包装一个弹簧靴uber JAR".

I am trying to wrap a spring boot "uber JAR" with procrun.

按预期运行以下工作:

java -jar my.jar

java -jar my.jar

我需要我的 spring 启动 jar 在 Windows 启动时自动启动.最好的解决方案是将 jar 作为服务运行(与独立的 tomcat 相同).

I need my spring boot jar to automatically start on windows boot. The nicest solution for this would be to run the jar as a service (same as a standalone tomcat).

当我尝试运行它时,我收到Commons Daemon procrun failed with exit value: 3"

When I try to run this I am getting "Commons Daemon procrun failed with exit value: 3"

查看 spring-boot 源,它看起来好像使用自定义类加载器:

Looking at the spring-boot source it looks as if it uses a custom classloader:

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/JarLauncher.java

当我尝试直接运行我的 main 方法时,我也得到了一个ClassNotFoundException".

I also get a "ClassNotFoundException" when trying to run my main method directly.

java -cp my.jar my.MainClass

java -cp my.jar my.MainClass

有没有一种方法可以用来在 Spring Boot jar 中运行我的 main 方法(不是通过 JarLauncher)?

Is there a method I can use to run my main method in a spring boot jar (not via JarLauncher)?

有没有人成功地将 spring-boot 与 procrun 集成?

Has anyone successfully integrated spring-boot with procrun?

我知道http://wrapper.tanukisoftware.com/.但是由于他们的许可,我无法使用它.

I am aware of http://wrapper.tanukisoftware.com/. However due to their licence I can't use it.

更新

我现在已经设法使用 procrun 启动了该服务.

I have now managed to start the service using procrun.

set SERVICE_NAME=MyService
set BASE_DIR=C:\MyService\Path
set PR_INSTALL=%BASE_DIR%prunsrv.exe

REM Service log configuration
set PR_LOGPREFIX=%SERVICE_NAME%
set PR_LOGPATH=%BASE_DIR%
set PR_STDOUTPUT=%BASE_DIR%stdout.txt
set PR_STDERROR=%BASE_DIR%stderr.txt
set PR_LOGLEVEL=Error

REM Path to java installation
set PR_JVM=auto
set PR_CLASSPATH=%BASE_DIR%%SERVICE_NAME%.jar

REM Startup configuration
set PR_STARTUP=auto
set PR_STARTIMAGE=c:\Program Files\Java\jre7\bin\java.exe 
set PR_STARTMODE=exe
set PR_STARTPARAMS=-jar#%PR_CLASSPATH%

REM Shutdown configuration
set PR_STOPMODE=java
set PR_STOPCLASS=TODO
set PR_STOPMETHOD=stop

REM JVM configuration
set PR_JVMMS=64
set PR_JVMMX=256

REM Install service
%PR_INSTALL% //IS//%SERVICE_NAME%

我现在只需要练习如何停止服务.我正在考虑使用 spring-boot 执行器关闭 JMX Bean.

I now just need to workout how to stop the service. I am thinking of doing someting with the spring-boot actuator shutdown JMX Bean.

当我现在停止服务时会发生什么;windows 无法停止该服务(但将其标记为已停止),该服务仍在运行(我可以浏览到 localhost),在任务管理器中没有提及该进程(不太好!除非我瞎了眼).

What happens when I stop the service at the moment is; windows fails to stop the service (but marks it as stopped), the service is still running (I can browse to localhost), There is no mention of the process in task manager (Not very good! unless I am being blind).

推荐答案

从 Spring Boot 1.3 开始,现在可以使用 winsw.

This is now possible since Spring Boot 1.3 using winsw.

文档您可以查看参考实现,其中展示了如何设置服务.

The documentation directs you to a reference implementation which shows how to setup a service.

这篇关于Spring 启动 JAR 作为 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆