开发者

Simpler init script for Java daemons?

I've written a small library for creating services/daemons in Java. The idea is simple. When you start your application you pass a command and a port number. If the command is a start command, a new instance of your application will be started on the specified port. Otherwise, the command will be sent to any instance that might be running on that port.

In short, the library provides a daemonize method that starts a daemon controller thread. It uses sockets to make your application communicate with instances of itself (as you've probably already figured out).

For clarity, here's an example of how you would use it:

public class MyApp extends Daemon
{
    public static void main(String[] args)
    {
        i开发者_开发技巧f (daemonize(MyApp.class, args))
        {
            // normal main body
            startMyServerOrWhatever();
        }
        else
        {
            // failed to start or send command to daemon
            // probably wrong syntax or unknown command
            printUsageInfoAndExit();
        }
    }

    @Command(start = true)
    public static int start()
    {
        // executed on "start" command, e.g. java -jar MyApp.jar start 8899
        doSomeInitializing();
        return 0; // return 0 or void to detach from console
    }

    @Command
    public static void mycmd()
    {
        // executed on "mycmd" command, i.e. java -jar MyApp.jar mycmd 8899
        doSomethingCool();
    }

    @Command(stop = true)
    public static int stop()
    {
        // executed on "stop" command, i.e. java -jar MyApp.jar stop 8899
        doSomeCleanup();
        return 0; // used as application exit code
    }
}

The library works really well and I've used it to create a couple of daemons that will run on a Linux server. What's missing now is some scripts to let the admins control these daemons like they control other daemons on the server (e.g. start at boot).

Unfortunately my *nix skills, especially when it comes to scripting, are not top level. I have a basic understanding of the BSD-style init procedure (rc.d), but looking at sample scripts like this one I feel a little lost.

So my question is, isn't there a simpler way to do this in my case? I mean, my daemons already understand the commands and should themselves be responsible for any actions (except in the case where a daemon doesn't respond to stop - it should then be killed after some timeout).


You should really have a look at the java service wrapper by tanuki software.
See http://wrapper.tanukisoftware.com/

What I like about their approach is that they have standardized deamon and windows service processes with a single tool, and common scripts.

I have noticed a good level of adoption of this tool across some high profile projects such as nexus, servicemix and others.

And when I encounter a project that has adopted the Java Service Wrapper for managing deamon processes, then the command set and configuration is already familiar to me, which lowers the learning curve.

Perhaps you could fit your socket controller mechanism into this existing framework.


isn't there a simpler way

I wrote once daemon scripts to start our java application over SSH. They are minimalistic - no force kill or rc.d/SMF integration, only daemon startup and shutdown using TERM signal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜