java background / daemon / service跨平台最佳实践

时间:2022-05-17 02:27:25

I am looking for the best way to make my desktop java program run in the background (daemon/service?) across most platforms (Windows, Mac OS, Linux [Ubuntu in particular]).

我正在寻找在大多数平台(Windows,Mac OS,Linux [特别是Ubuntu])中使我的桌面java程序在后台(守护程序/服务?)运行的最佳方法。

By "best way" I am hoping to find a way that will:

通过“最好的方式”,我希望找到一种方式:

  1. require a minimum amount of platform-specific code.
  2. 需要最少量的平台特定代码。

  3. not require the user to do anything a general computer user couldn't/wouldn't do
  4. 不要求用户做一般计算机用户不能/不会做的任何事情

  5. not be a resource hog.
  6. 不是资源匮乏。

I understand that my requirements may be unrealistic but I am hoping there is some sort of "best practice" for this type of situation.

我理解我的要求可能不切实际,但我希望这种情况有某种“最佳实践”。

How to go forward?

怎么前进?

7 个解决方案

#1


7  

You can use the SystemTray classes and install your app as any other in the default platform.

您可以使用SystemTray类并在默认平台中安装任何其他应用程序。

For windows it could be an scheduled task that run at startup. For Linux and OSX I don't know (besides crontab wich is somehow too technical) but I'm pretty sure they both have a way to do the same thing easily.

对于Windows,它可以是在启动时运行的计划任务。对于Linux和OSX我不知道(除了crontab在某种程度上太技术化了)但我很确定他们都有办法轻松地做同样的事情。

Unfortunately (as of today) Apple hasn't finished the 1.6 port.

不幸的是(截至今天)Apple尚未完成1.6端口。

It won't be a real demon, but an app like Google Desktop.

它不会是一个真正的恶魔,而是像谷歌桌面这样的应用程序。

I've heard Quartz is a good option. But I've never used it.

我听说过Quartz是个不错的选择。但我从来没有用过它。

#2


15  

You can run a Java application as a service (Windows) or daemon (Linux) using the Apache Commons daemon code.

您可以使用Apache Commons守护程序代码将Java应用程序作为服务(Windows)或守护程序(Linux)运行。

Structure

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.

守护进程由2部分组成。一个用C语言编写,用于创建操作系统的接口,另一个用Java编写,提供Daemon API。

Platforms

Both Win32 and UNIX like platforms are supported. For Win32 platforms use procrun. For UNIX like platforms use jsvc.

支持Win32和UNIX类平台。对于Win32平台,请使用procrun。对于类似UNIX的平台,请使用jsvc。

Java code

You have to write a Class (MyClass) that implements the following methods:

您必须编写一个实现以下方法的Class(MyClass):

* void load(String[] arguments): Here open the configuration files, create the trace file, create the ServerSockets, the Threads
* void start(): Start the Thread, accept incoming connections
* void stop(): Inform the Thread to live the run(), close the ServerSockets
* void destroy(): Destroy any object created in init()

#3


9  

You can turn any Java program into a service/daemon using the Java Service Wrapper. It is used by multiple OSS projects, and ships as part of the Nexus Maven Repository Manager so that it can be installed as a service out of the box. To use it, you, the author, just need to create a configuration file and then run a simple batch file to create the service on Windows or copy an init script to the correct runlevel on Linux.

您可以使用Java Service Wrapper将任何Java程序转换为服务/守护程序。它由多个OSS项目使用,并作为Nexus Maven Repository Manager的一部分提供,因此可以作为开箱即用的服务安装。要使用它,作者,您只需要创建一个配置文件,然后运行一个简单的批处理文件在Windows上创建服务或将init脚本复制到Linux上的正确运行级别。

#4


1  

If you dont need free solution, you can use Advanced Installer (www.advancedinstaller.com), it can make win-service as well as MacOS installer from your JAR, and more..

如果您不需要免费解决方案,您可以使用Advanced Installer(www.advancedinstaller.com),它可以从您的JAR中获得win-service以及MacOS安装程序等等。

#5


0  

Check out JDIC, the Java Desktop Integration Components project. It supports desktop integration like system tray (or equivalent) with a cross-platform API.

查看Java Desktop Integration Components项目JDIC。它支持桌面集成,如系统托盘(或等效)与跨平台API。

Others have mentioned Quartz, which is an enterprise job scheduler. It can be lightweight, depending on the jobs that are scheduled, but it doesn't have any features that are inherently desktop-oriented. On the contrary, many of its features depend on enterprise support like a relational database. If your application is primarily scheduling tasks, a headless Quartz service executing jobs, with a desktop client to interact with the service is reasonable approach.

其他人提到了Quartz,它是一个企业作业调度程序。它可以是轻量级的,具体取决于计划的作业,但它没有任何本质上面向桌面的功能。相反,它的许多功能都依赖于企业支持,如关系数据库。如果您的应用程序主要是调度任务,那么无头的Quartz服务执行作业,与桌面客户端进行交互是合理的方法。

#6


0  

The Apache Directory Daemon project sounds like the best cross platform way to do this (with Java wrappers for JSVC under POSIX and procrun under windows).

Apache Directory Daemon项目听起来像是最好的跨平台方式(在POSIX下使用Java包装,在Windows下使用procrun)。

#7


0  

People sometimes have difficulties finding prunsrv.exe and/or prunmgr.exe (components of procrun), its not well documented on the apache site, generally it can be found in the archives (note. they say that procrun is tomcat5.exe)

人们有时很难找到prunsrv.exe和/或prunmgr.exe(procrun的组件),它在apache网站上没有很好的记录,一般它可以在档案中找到(注意。他们说procrun是tomcat5.exe)

for windows you can find it here: http://archive.apache.org/dist/commons/daemon/binaries/windows/commons-daemon-1.0.7-bin-windows.zip (contains both 32 and 64 bit versions)

对于Windows,你可以在这里找到它:http://archive.apache.org/dist/commons/daemon/binaries/windows/commons-daemon-1.0.7-bin-windows.zip(包含32位和64位版本)

you dont need to implement daemon interface nor download it at all, prunsrv can be used to turn any app to windows service

browse that archive for other platforms

浏览其他平台的存档

#1


7  

You can use the SystemTray classes and install your app as any other in the default platform.

您可以使用SystemTray类并在默认平台中安装任何其他应用程序。

For windows it could be an scheduled task that run at startup. For Linux and OSX I don't know (besides crontab wich is somehow too technical) but I'm pretty sure they both have a way to do the same thing easily.

对于Windows,它可以是在启动时运行的计划任务。对于Linux和OSX我不知道(除了crontab在某种程度上太技术化了)但我很确定他们都有办法轻松地做同样的事情。

Unfortunately (as of today) Apple hasn't finished the 1.6 port.

不幸的是(截至今天)Apple尚未完成1.6端口。

It won't be a real demon, but an app like Google Desktop.

它不会是一个真正的恶魔,而是像谷歌桌面这样的应用程序。

I've heard Quartz is a good option. But I've never used it.

我听说过Quartz是个不错的选择。但我从来没有用过它。

#2


15  

You can run a Java application as a service (Windows) or daemon (Linux) using the Apache Commons daemon code.

您可以使用Apache Commons守护程序代码将Java应用程序作为服务(Windows)或守护程序(Linux)运行。

Structure

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.

守护进程由2部分组成。一个用C语言编写,用于创建操作系统的接口,另一个用Java编写,提供Daemon API。

Platforms

Both Win32 and UNIX like platforms are supported. For Win32 platforms use procrun. For UNIX like platforms use jsvc.

支持Win32和UNIX类平台。对于Win32平台,请使用procrun。对于类似UNIX的平台,请使用jsvc。

Java code

You have to write a Class (MyClass) that implements the following methods:

您必须编写一个实现以下方法的Class(MyClass):

* void load(String[] arguments): Here open the configuration files, create the trace file, create the ServerSockets, the Threads
* void start(): Start the Thread, accept incoming connections
* void stop(): Inform the Thread to live the run(), close the ServerSockets
* void destroy(): Destroy any object created in init()

#3


9  

You can turn any Java program into a service/daemon using the Java Service Wrapper. It is used by multiple OSS projects, and ships as part of the Nexus Maven Repository Manager so that it can be installed as a service out of the box. To use it, you, the author, just need to create a configuration file and then run a simple batch file to create the service on Windows or copy an init script to the correct runlevel on Linux.

您可以使用Java Service Wrapper将任何Java程序转换为服务/守护程序。它由多个OSS项目使用,并作为Nexus Maven Repository Manager的一部分提供,因此可以作为开箱即用的服务安装。要使用它,作者,您只需要创建一个配置文件,然后运行一个简单的批处理文件在Windows上创建服务或将init脚本复制到Linux上的正确运行级别。

#4


1  

If you dont need free solution, you can use Advanced Installer (www.advancedinstaller.com), it can make win-service as well as MacOS installer from your JAR, and more..

如果您不需要免费解决方案,您可以使用Advanced Installer(www.advancedinstaller.com),它可以从您的JAR中获得win-service以及MacOS安装程序等等。

#5


0  

Check out JDIC, the Java Desktop Integration Components project. It supports desktop integration like system tray (or equivalent) with a cross-platform API.

查看Java Desktop Integration Components项目JDIC。它支持桌面集成,如系统托盘(或等效)与跨平台API。

Others have mentioned Quartz, which is an enterprise job scheduler. It can be lightweight, depending on the jobs that are scheduled, but it doesn't have any features that are inherently desktop-oriented. On the contrary, many of its features depend on enterprise support like a relational database. If your application is primarily scheduling tasks, a headless Quartz service executing jobs, with a desktop client to interact with the service is reasonable approach.

其他人提到了Quartz,它是一个企业作业调度程序。它可以是轻量级的,具体取决于计划的作业,但它没有任何本质上面向桌面的功能。相反,它的许多功能都依赖于企业支持,如关系数据库。如果您的应用程序主要是调度任务,那么无头的Quartz服务执行作业,与桌面客户端进行交互是合理的方法。

#6


0  

The Apache Directory Daemon project sounds like the best cross platform way to do this (with Java wrappers for JSVC under POSIX and procrun under windows).

Apache Directory Daemon项目听起来像是最好的跨平台方式(在POSIX下使用Java包装,在Windows下使用procrun)。

#7


0  

People sometimes have difficulties finding prunsrv.exe and/or prunmgr.exe (components of procrun), its not well documented on the apache site, generally it can be found in the archives (note. they say that procrun is tomcat5.exe)

人们有时很难找到prunsrv.exe和/或prunmgr.exe(procrun的组件),它在apache网站上没有很好的记录,一般它可以在档案中找到(注意。他们说procrun是tomcat5.exe)

for windows you can find it here: http://archive.apache.org/dist/commons/daemon/binaries/windows/commons-daemon-1.0.7-bin-windows.zip (contains both 32 and 64 bit versions)

对于Windows,你可以在这里找到它:http://archive.apache.org/dist/commons/daemon/binaries/windows/commons-daemon-1.0.7-bin-windows.zip(包含32位和64位版本)

you dont need to implement daemon interface nor download it at all, prunsrv can be used to turn any app to windows service

browse that archive for other platforms

浏览其他平台的存档