java java后台/守护进程/服务跨平台最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/326509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java background/daemon/service cross platform best practices
提问by Hyman
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]).
我正在寻找使我的桌面 Java 程序在大多数平台(Windows、Mac OS、Linux [特别是 Ubuntu])的后台运行(守护进程/服务?)的最佳方法。
By "best way" I am hoping to find a way that will:
通过“最佳方式”,我希望找到一种方法:
- require a minimumamount of platform-specific code.
- not require the user to do anything a general computer user couldn't/wouldn't do
- not be a resource hog.
- 需要最少量的特定于平台的代码。
- 不要求用户做一般计算机用户不能/不会做的任何事情
- 不要成为资源猪。
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?
如何前进?
采纳答案by OscarRyz
You can use the SystemTrayclasses 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.
它不会是一个真正的恶魔,而是一个像 Google 桌面这样的应用程序。
I've heard Quartz is a good option. But I've never used it.
我听说石英是一个不错的选择。但我从来没有用过。
回答by pro
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.
守护进程由两部分组成。一个用 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
Java代码
You have to write a Class (MyClass) that implements the following methods:
您必须编写一个实现以下方法的类(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()
回答by Chris Lieb
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 上创建服务,或者在 Linux 上将 init 脚本复制到正确的运行级别。
回答by tomasb
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 安装程序,等等..
回答by tomasb
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
您根本不需要实现守护程序接口也不需要下载它,prunsrv 可用于将任何应用程序转换为 Windows 服务
browse that archive for other platforms
浏览其他平台的存档
回答by erickson
Check out JDIC,the Java Desktop Integration Components project. It supports desktop integration like system tray (or equivalent) with a cross-platform API.
查看JDIC,Java 桌面集成组件项目。它支持桌面集成,如系统托盘(或等效)与跨平台 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,它是一个企业作业调度程序。它可以是轻量级的,具体取决于计划的作业,但它没有任何本质上面向桌面的功能。相反,它的许多功能都依赖于企业支持,如关系数据库。如果您的应用程序主要是调度任务,那么使用 Headless Quartz 服务执行作业,并使用桌面客户端与服务进行交互是合理的方法。
回答by ogrisel
The Apache Directory Daemonproject sounds like the best cross platform way to do this (with Java wrappers for JSVC under POSIX and procrun under windows).
在Apache目录守护进程是最好的跨平台的方式项目的声音做到这一点(与POSIX下Java包装为JSVC和windows下procrun)。

