通过Commons Daemon Procrun让Java应用程序作为Windows服务运行

Java应用Windows服务化实战:使用Apache Commons Daemon部署与调优 在Java后端开发与运维中,将可执行JAR包部署为系统服务是保障应用长期稳定运行的关键需求。其核心原理是通过服务包装器与Windows服务控制管理器交互,实现对Java进程生命周期的系统级管理。这项技术对于需要高可用性、开机自启的后台服务(如数据同步、消息队列消费者)具有重要价值,能显著提升运维效率和系统可靠性。Apache Commons Daemon作为成熟方案,通过配置JVM参数、日志重定向和失败恢复策略,可将Spring Boot等应用无缝转换为Windows服务。本文聚焦于使用procrun进行实 阅读详情

转自:点击打开链接

Overview

Apache Commons Daemon is made of 2 parts. One written in C that makes the interface to the operating system and the other in Java that provides the Daemon API. Both Win32 and UNIX like platforms are supported, but for this article I will only focus on the former and show you how to use Procrun to run your java application as a windows service.

Prerequisites

If  you want to run the sample application, you will need Java 6 and Maven installed. GIT is also recommended.

In a nutshell

1. Download the commons daemon windows binaries and extract the contents
2. Rename prunsrv.exe  to [YOUR_SERVICE_NAME].exe e.g. LoggerService.exe
3. Rename prunmgr.exe to [YOUR_SERVICE_NAME]w.exe e.g. LoggerServicew.exe
4. Create a java class start/stop your service and compile it
5. Run the commands to install your service and start your service

For more details run the sample application and/or see the How It Works section.

Running the Sample Application

1. Using GIT, get the code from JavaWindowsServiceUsingCommonsDaemon.git, or if you don’t have GIT, download the zip  and extract it locally.
2. Open up a command prompt to the directory where the GIT project/zip file was extracted to.
3. Run mvn package to build the project
4. Cd to the bin directory and run Install-Remove-Service.bat install to install the service

a. To verify that the service is installed, open up the services window from the Control panel.

5. Before starting the service from the Services Window, run LoggerService.exe to see the service run in console mode and confirm that everything was installed correctly.
6. You can now start/stop the service from the Services window to confirm that it is working

How does it work

Java Code

The bulk of the code is in the LoggerService class, which is a slightly modified version of the  ProcrunService class. This is the code that you need to write to wrap your application, namely the main, start, stop and run methods.
You can run the LoggerService class from the command line or as a service. The main method is executed when running it from the command line. It handles the case where start/stop are called from different consoles (and hence processes) and so uses a file to communicate between the processes. If, instead, you use Ctrl-C to stop in your version then you can omit this extra code.

 
 

Next, the start method, like stop, must conform to the signature public void methodName (String[] args). Your application service will run as long as the start method does not return. The start method in the sample ignores the arguments, starts a thread to run the application and blocks till the thread dies or is interrupted.

public static void start(String[] args) {
   startThread(null); //#1
   while (loggerThread.isAlive()) {
     try {
       loggerThread.join();
     } catch (InterruptedException ie) {
       // Ignored
   }
  }
}

Stop also ignores the arguments and interrupts the service thread to stop the service.

public static void stop(String[] args) {
  if (loggerThread != null) {
    logger.log("Interrupting the thread");
    loggerThread.interrupt();
  } else {
    logger.log("No thread to interrupt");
  }
}

Run is where your application (logger in the sample) is invoked. If interrupted, it will exit the while loop and end the service thread to stop the service.

public void run() {
  ...
  while (stopFile == null || stopFile.exists()) {
    ...
    try {
      logger.log("pausing " + sleepTime + " seconds");
      Thread.sleep(sleepTime * MS_PER_SEC);
    } catch (InterruptedException e) {
      logger.log("Exiting");
      break;
    }
  }
}

Installation Script

The batch file Install-Remove-Service.bat relies on LoggerService.exe which was renamed from prunsrv.exe and can be found in the commons daemon windows binarieszip file. The convention, as stated before, is to rename prunsrv.exe to [YOUR_SERVICE_NAME].exe.

Now let’s take a look at the Install-Remove-Service.bat and how to modify it for your own use.

1. The first line sets SERVICE_NAME defined, which as noted above, should exactly match the name you gave
to the prunsrv.exe

set SERVICE_NAME=LoggerService
set EXECUTABLE_NAME=%SERVICE_NAME%.exe
set EXECUTABLE=%APPLICATION_SERVICE_HOME%\bin\%EXECUTABLE_NAME%

2. Next, you need to provide the fully qualified classes that will be used to start and stop your service. These can be, and usually are the same class.

set CG_START_CLASS=com.ulrichpalha.service.LoggerService
set CG_STOP_CLASS=%CG_START_CLASS%

3. Optionally, you can provide the start and stop methods for the classes defined above. The methods must be of type public void methodName (String[] args). If none are provide, these default to the classes static void main(String[] args) method.

set CG_START_METHOD=start
set CG_STOP_METHOD=stop

4. Optional parameters to the start/stop methods above.

set CG_START_PARAMS=start
set CG_STOP_PARAMS=stop

5. Next provide the full path to the jar that contains your start/stop methods. This is used in 8 below.

set CG_PATH_TO_JAR_CONTAINING_SERVICE=%APPLICATION_SERVICE_HOME%\target\JavaWindowsServiceUsingCommonsDaemon-0.0.1-SNAPSHOT.jar

6. You can also choose to have your service start automatically upon reboot. The default is manual startup.

set CG_STARTUP_TYPE=auto

7. Set this if you want to use a different JVM that is configured in your registry, or if the JVM is not configured in your registry.

set CG_PATH_TO_JVM=%JAVA_HOME%\jre\bin\server\jvm.dll

8. Set the class path to include all jars needed to run your service

set PR_CLASSPATH=%APPLICATION_SERVICE_HOME%;%CG_PATH_TO_JAR_CONTAINING_SERVICE%

9. Next the service is installed with the //IS command. Note that if you use the –DependsOn option e.g. if your service depends on another service like ActiveMQ, then it must be included here as it will NOT work with the //US command

set EXECUTE_STRING= %EXECUTABLE% //IS//%SERVICE_NAME% --Startup %CG_STARTUP_TYPE% --StartClass %CG_START_CLASS% --StopClass %CG_STOP_CLASS%

10.The next series of //US commands update the service configuration

set EXECUTE_STRING= "%EXECUTABLE%" //US//%SERVICE_NAME% --StartMode jvm --StopMode jvm --Jvm %CG_PATH_TO_JVM%

The commons daemon windows binaries also include prunmgr.exe, which by convention is renamed to [YOUR_SERVICE_NAME]w.exe. After the service is installed, you can use it to start/stop your service and change most of the options that were set in the installation script. Below are some screenshots showing the General and Startup tabs.

Questions/Comments

Let me know if you have any questions or comments, especially if you used this to turn your java application into a windows service.

References/Further Reading

Commons Daemon Home Page
Procrun Home Page Provides a detailed description of all Prunmgr and Prunsrv command line options.

- See more at: http://ulrichpalha.com/myblog/2011/11/19/using-commons-daemon-procrun-to-run-your-java-application-as-a-windows-service/#sthash.JX4Iae3i.dpuf



Java应用部署为Windows服务:Procrun方案详解与实战 在服务器端部署中,将Java应用服务化是保障其稳定、自动化运行的核心需求。服务化是指将应用程序注册为系统服务,由操作系统服务管理器接管其生命周期,实现开机自启动、故障自动恢复等关键运维能力。其技术原理在于通过一个原生服务包装器(如Apache Commons Daemon的prunsrv.exe)作为宿主进程,由它来创建和管理JVM进程,从而将Java应用的生命周期与Windows服务控制管理器(SCM)深度集成。这项技术的核心价值在于极大地提升了后台服务的可靠性和可维护性,减少了人工干预,是微服务、API 阅读详情

相关推荐

commons Daemon+Procrun将java程序安装为windows服务的实例

本资源是打包好的一套demo实例,适用于64位jre。如需32位请自行更换jre包或引用路径,同时更换支持32位的prunsrv.exe,相关32位和64位的工具请下载我的资源 commons Daemon+Procrun

解决tomcat:不能在本地计算机启动Apache Tomcat 8.5的问题(Apache Commons Daemon procrun stderr initialized)

早上起来,好端端地想着上系统看一下,意外地发现系统登录不上,一看服务,怎么也重启不了,一直报错: 解决方法: 1.win+E,打开计算机,右键点击管理,看看事件管理器下的windows系统日志,发现有一个报错: 2.然后立刻去找tomcat的日志,在安装路径的log下,按修改日期排序,查看最近的报错日志std-err.log,发现报错:Apache Commons Daemon procrun...

Albert_LG的博客 7714

java服务安装(二):使用commons-daemon 之 procrun-附件资源

java服务安装(二):使用commons-daemon 之 procrun-附件资源

java服务安装(二):使用commons-daemon

1概述 1为什么使用commons daemon 2commons daemon介绍 3本文主要内容 2程序示例 3linux下使用commons-daemon 1下载commons-daemon 2安装jsvc 3编写程序入口类 4编写安装脚本 5程序打包及启动 4windows下使用commons-daemon 1下载commons-daemon及procrun 2编写程序入口类 3编写安装脚本

masson32的专栏 1万+

【清华大学软院】15-19年推免试题历年整理

整理的题目为清软预推免机试(外校)硕士机考题目。 年代 题目1 题目2 题目3 2019 质因数分解 二叉搜索树 区间DP(折叠字符串) 2018 日期转换 高精度模拟 区间DP(戳气球) 2017 进制素数 被遗漏的数字 DP(收集苹果) 2016 大数运算 DP DFS 2015 进制素数: http://acm.hdu.edu.cn/showproblem.php?pid=1230 2017年题目(这个是找的最准确的题目): 1题见【链接】 2题见【链接】

云端筑梦师 2256

西门子S7-1200控制伺服/步进电机方法与接线(全)

西门子S7-1200控制伺服/步进电机方法与接线(全) 伺服/步进电机在非标自动化控制中十分常用,但作者发现在各类开源网站上很少有人做西门子1200PLC控制伺服/步进电机的教程,于是今天想着跟大家分享一下。 本文共分为一下几个四个内容。 文章目录西门子S7-1200控制伺服/步进电机方法与接线(全)前言一、准备二、接线1.PLC接线:2.伺服接线:3. 步进电机的接线:三、代码组态代码四、可能出现的问题 前言 工程控制常用到三项异步电机、直流电机等,但他们都无法准确的控制电机角度;于是有了步进电机、

weixin_46060068的博客 7万+

使用Apache的Procrun将Springboot项目注册为windows服务(commons-daemon Procrun)

Java程序做出Windows服务、Springboot项目做成windows服务的几种方式 3种方法: 1、利用JAVA Service Wrapper 缺点:官方分专业版、标准版和社区版,64位没有社区版,非社区版收费; 2、使用WinSW, 地址:http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/ 缺点:需要.Net运行环境支持 优点:网上资料很多,简单,稳定可靠; 3、使用Apache的Procrun 地址:http://commons.

kxlele 2043

Unity3D实战:用EasyAR4.0稀疏空间地图打造AR导航应用(附完整代码)

本文详细介绍了如何使用Unity3D和EasyAR4.0的SparseSpatialMap功能构建AR导航应用。通过稀疏空间地图技术,开发者可以创建室内导航系统,解决传统GPS在室内环境中的局限性。文章包含完整代码示例,涵盖环境配置、地图构建、导航实现及性能优化等关键步骤,助力开发者快速掌握AR导航开发技巧。

t8u9v0的博客 1197

Commons Daemon 之 procrun

Procrun是一套让Java应用程序运行在WIN#@下更容易的库和应用程序。 Procrun服务应用程序 Prunsrv一个让应用程序作为服务运行的服务程序,它能转换任一应用程序作为服务运行。 Procrun监视器应用程序 Prunmgr是一个可视化应用

1万+

ArcGIS Pro 3.4 二次开发 - 地理数据库

本文介绍了在ArcGIS Pro SDK 3.4和.NET 8环境下,如何操作地理数据库的多种方法。首先,通过路径打开文件地理数据库,并处理可能出现的异常。其次,使用连接属性或sde文件路径打开企业级地理数据库。此外,还展示了如何从项目项、要素图层中获取地理数据库,以及如何执行SQL语句。最后,文章详细说明了如何从地理数据库中获取单个定义或定义列表,包括表定义、要素类定义、关系类定义和要素数据集定义。这些操作均在后台线程中执行,确保应用程序的响应性。

Winemonk 944

Android 系统状态栏中实现高斯模糊背景

至此,我们已经完成了在 Android 系统的 SystemUI 中实现高斯模糊背景的过程。通过以上的布局和代码修改,我们可以在状态栏中看到具有高斯模糊效果的背景。同时,为了在不同版本的 Android 系统上适配高斯模糊效果,可能需要进行一些额外的代码调整和兼容性处理。希望本文能够帮助你理解如何在 Android 系统的 SystemUI 中实现高斯模糊背景,并通过提供的源代码进行相应的开发和调试。首先,我们需要在 SystemUI 的布局文件中添加一个用于展示高斯模糊背景的视图。属性来开启模糊效果。

CyberSparkZ的博客 870

windows环境下安装zookeeper(单机版)

一.安装 1.1 下载zookeeper https://zookeeper.apache.org/releases.html#download 我用的是apache-zookeeper-3.6.2-bin.tar.gz 注意:zookeeper的安装路径不要有中文,建议也不要有空格,比如Program Files这样的路径 1.2 生成并修改zoo.cfg 在zookeeper的conf目录下复制一份zoo_sample.cfg文件,并重命名为zoo.cfg 修改zoo..

晨港飞燕的专栏 1万+

小论文/大论文必备 | YOLO多模态目标检测、绘制曲线对比图 | 引入多种绘制曲线对比图,包括mAP0.5,mAP0.5:0.95,Loss损失变化的曲线对比

本文通过可视化方法分析YOLO多模态模型的训练过程,对比不同实验的检测精度和损失函数变化。主要内容包括:1)绘制mAP@0.5和mAP@0.5:0.95曲线评估检测精度;2)分析边界框回归损失(box_loss)、分类损失(cls_loss)和分布式损失(dfl_loss)的收敛趋势;3)提供完整的Python代码实现,支持多实验对比,可生成高清曲线图。该方法能直观展示模型训练效果,便于优化策略评估和参数调优。

Ai缝合怪、助力小伙伴高效发论文 261

Apache Commons Daemon使用手册,使用commons-daemon把java程序注册为服务,监视服务和GUI程序状态

Apache Commons Daemon简介 Apache Commons Daemon软件是用于将Java应用程序作为服务器进程运行的一组实用程序和Java支持类。这些在Unix术语中通常称为“守护进程”(因此得名)。在Windows上,它们称为“服务”。 前言 自1994年以来,Java编程语言获得了强有力的发展,并不仅仅是applet和客户端应用程序,在服务器应用程序领域也是安全可靠和高性能的开发工具。Java平台的主要缺点在于,直到今天,启动Java应用程序的唯一可移植的方式仍然是依赖于单个入口,

GIS技术方向,GIS地图技术分享(GIS入门、Openlayers教程、Leaflet教程),做最好的GIS地图开发教程。关注博主公众号:eguid 3069

国科大学习资料--人工智能原理与算法-2020年期末考试题解析(学长整理)

国科大学习资料–人工智能原理与算法-2020年期末考试题解析 张文生老师主讲 试卷共有6道题,题目不难,希望大家能取得好的成绩。 3.9 传教士和野人问题。三个传教士和三个野人在河的一岸,有一条能载一个人或者两个人的船。请设法使所有人都渡到河的另一岸,要求在任何地方野人数都不能多于传教士的人数。这个问题在AI领域中很有名,是因为它是第一个从分析的观点探讨问题形式化的论文的主题(Amarel,1968)。 a. 请对该问题进行详细形式化,只描述确保该问题求解所必需的特性。画出完整的状态空间图。 b. 应用合适

无名何许人的博客 4789

四舵轮底盘运动学模型(源码)(四轮四独立转向)二

舵轮4转向活动范围 x 轴正半轴,x轴正方向为0弧度,左正右负, -M_PI/2 < 转向范围 < M_PI/2;移动底盘的正前方为x方向,左侧为y轴方向(遵循右手坐标系,右手大拇指指向天空为z轴方向,食指与中指垂直,食指指向小车正前方为x轴,中指指向为移动底盘y轴方向)转向:逆时针旋转角速度w为正,顺时针旋转角速度w为负。(遵循右手螺旋法则,右手握拳大拇指指向天空为旋转z轴方向,拳垂直于地面,4指方向即底盘转动的正方向)左前轮为1号轮,右前轮为2号轮,左后轮为3号轮,右后轮为4号轮。

苏凯的博客 528

common daemon

http://zdsyouxiang.iteye.com/blog/1940202 http://commons.apache.org/proper/commons-daemon/procrun.html http://blog.youkuaiyun.com/lingling1420q/article/details/17348925 转载于:https://www.cnblogs.com/diyunpe...

weixin_30922589的博客 93

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

以Windows服务方式运行Java程序

如题,怎样将Java程序变身为Windows服务,不要跟我说Win接口,我不熟。 一、将exe程序变为Win服务 即将java启动批处理命令编译为win程序,可通过 批处理潜行者V6.0或 quickbfc 3.6.1实现; 然后利用系统自带的sc命令将其创建为系统服务:(这里为了测试效果,我加上了交互式运行服务的参数) ? 1

260

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

将java程序运行为windows服务

1、首先到http://javaservice.objectweb.org/下载javaservice2、解压缩到硬盘目录,运行TestSampleService.bat就可看到实际效果。3、将JavaService.exe拷贝到java程序运行目录。4、例子解释@echo off@echo .@echo . TestSampleService.bat - test JavaService e

1047

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【亲测免费】 Apache Commons Daemon 教程

Apache Commons Daemon 教程 Apache Commons Daemon 是一个用于在各种操作系统上以守护进程(Unix中称为'daemon',Windows中称为'service')形式运行Java应用程序的库。以下是关于该项目的目录结构、启动文件和配置文件的简要介绍。 1. 项目目录结构及介绍 Apache Commons Daemon 的源代码仓库中包含了以下关键目录: ...

gitblog_00297的博客 1336

java服务安装(二):使用commons-daemon 之 procrun

Procrun是一套让Java应用程序运行在WIN#@下更容易的库和应用程序。 Procrun服务应用程序 Prunsrv一个让应用程序作为服务运行的服务程序,它能转换任一应用程序作为服务运行。 Procrun监视器应用程序 Prunmgr是一个可视化应用程序,用来监视和配置procrun服务。 可用的命令行选项: //ES//     编辑服务的配置     

liu_dong_kang的博客 5390

spring boot项目 部署 使用Procrun 注册为windows服务。

1.下载Procrun 下载链接:https://downloads.apache.org/commons/daemon/binaries/windows/ 2.新建部署目录workSpace 3.添加子目录 JAR、Logs、Service 4.复制 amd64 目录下的 prunsrv.exe 和 根目录下的prunmgr.exe 到Service目录中 5.将prunmgr.exe 重命名为你的服务名 6.在Service 目录中添加 install.bat 和 uninstall.bat int

zhigang的博客 1231

Deamon java_java服务安装(二):使用commons-daemon

tags: java daemon1、概述1.1、为什么使用commons daemon上一篇已对使用java service wrapper工具与java程序集成进行了讲解,java service wrapper使用简单,集成方法简单,不修改任何代码,一般情况下已满足需求。但是,java service wrapper只对java程序的开启及关闭进行操作, 若需要对程序启动前及关闭前进行一些...

weixin_35224065的博客 846

java程序服务不能启动不了_java – apache procrun无法启动服务

编辑我正在使用loaderDeamon接口来安装和运行java应用程序作为Windows服务.为此,Apache的procrun服务工具(与tomcat一起安装的相同的东西)处理服务的安装和执行.最近,当通过服务管理器执行服务时,服务声称正在运行,但没有响应.没有写入日志,并且应用程序的客户端无法进行通信.这让我觉得服务实际上并没有运行.我试图通过// TS命令通过procrun启动服务,如下所示...

weixin_39731807的博客 787

将Java应用程序作为Windows服务安装

这听起来像是您不需要的东西,但是有时候,当您分发最终用户软件时,可能需要将Java程序安装为Windows服务。 我之所以必须这样做,是因为我开发了一种用于公务员的工具 ,可以自动将其Excel文件转换并将其推入我国的opendata门户。 该工具必须定期运行,因此它是服务的主要候选者(即使公务员完全忘记了此任务,这也可以使上传成为可能,此外,重复的手动上传是浪费时间)。 尽管有很多关于...

最佳 Java 编程 238
上一篇: Android sdk manager 下载安装中的错误收集
下一篇: Qt中隐藏滚动条重新实现鼠标滚轮事件wheelEvent
Willib
博客等级 码龄14年 83粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值