Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java background/daemon/service cross platform best practices

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]).

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

  1. require a minimum amount of platform-specific code.
  2. not require the user to do anything a general computer user couldn't/wouldn't do
  3. 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?

like image 280
Jack Avatar asked Nov 28 '08 19:11

Jack


2 Answers

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

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.

Platforms

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

Java code

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

* 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() 
like image 108
pro Avatar answered Oct 10 '22 08:10

pro


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.

like image 24
Chris Lieb Avatar answered Oct 10 '22 08:10

Chris Lieb