java service wrapper, very basic step by step setup with startup failure

AgostinoX picture AgostinoX · May 21, 2012 · Viewed 15k times · Source

I have simply donwloaded JSW community edition, unwrapped into a directory:
c:\servicetest
So here i have a bin, conf, lib and log subdirs, among others. From now on this will be (root).
I referenced the (root)/lib/wrapper.jar into my ide (netbeans) and create a very simple service (remember the class name is Main):

public class Main extends WrapperSimpleApp {

public Main(String[] args) {
    super(args);
}

@Override
public void run() {

    while(true) {
        Logger.getLogger(Main.class.getName()).log(Level.INFO, "I'm alive");

        try 
           { Thread.sleep(2000); } 
        catch (InterruptedException ex) 
           { return; }
    }
}

}

As you see, it basically does nothing but logging a message. But actually it neither starts.

I compiled the project (MyProject.jar), copied the jar into the (root) directory and modified the (root)/config/wrapper.conf adding:

wrapper.java.classpath.3=../MyProject.jar

and

wrapper.java.mainclass=textappender.Main

Then i've installed the service in command line, with:

C:\servicetest\bin>wrapper -i ../conf/wrapper.conf

then i've started the service, either via services.msc control panel or via

C:\servicetest\bin>wrapper -t ../conf/wrapper.conf

In logs/wrapper.log i get:

ERROR  | wrapper  | 2012/05/21 21:35:11 | JVM exited while loading the application.

UPDATE 1

Following Tanuki Software advice, i've set in my (root)/config/wrapper.conf (well, uncommented since it already was):

wrapper.debug=TRUE

And now i get this:

INFO   | jvm 1    | 2012/05/22 10:46:37 | WrapperManager Debug: WrapperManager.stop(1) called by thread: main
INFO   | jvm 1    | 2012/05/22 10:46:37 | WrapperManager Debug: Backend not connected, not sending packet STOP : 1
INFO   | jvm 1    | 2012/05/22 10:46:37 | WrapperManager Debug: Stopped checking for  control events.
DEBUG  | wrapper  | 2012/05/22 10:46:37 | Pause reading child process output to share cycles.
INFO   | jvm 1    | 2012/05/22 10:46:37 | WrapperManager Debug: Thread, main, handling the shutdown process.
INFO   | jvm 1    | 2012/05/22 10:46:37 | WrapperManager Debug: shutdownJVM(1) Thread: main
INFO   | jvm 1    | 2012/05/22 10:46:38 | WrapperManager Debug: wait for 0 shutdown locks to be released.
INFO   | jvm 1    | 2012/05/22 10:46:38 | WrapperManager Debug: Backend not connected, not sending packet STOPPED : 1
INFO   | jvm 1    | 2012/05/22 10:46:38 | WrapperManager Debug: calling System.exit(1)
DEBUG  | wrapper  | 2012/05/22 10:46:38 | JVM process exited with a code of 1, setting the wrapper exit code to 1.

But given my very simple implementation, i cannot guess what is going wrong.

Answer

Naytzyrhc picture Naytzyrhc · May 22, 2012

rather than extending WrapperSimpleApp, your main class should implement the org.tanukisoftware.wrapper.WrapperListener interface.

You can find a very detailed description about implementing the interface on our website: http://wrapper.tanukisoftware.com/doc/english/integrate-listener.html

Please let me know if you have any further questions about the implementation and/or the configuration properties in your conf file.

Another advise which comes in handy for me sometimes, is rather than running your application immediately as service, I find it easier to do the integration by running first as console application, because you can see the output directly on your console. Once everything seems working, I go ahead and install/run as service. To run as console application, you would run for example:

C:\servicetest\bin>wrapper -c ../conf/wrapper.conf

You can also turn on debug output for the Wrapper by setting

wrapper.debug=true

in your conf file.

Edit due to the comment:

if your application is actually as simple as you describe, then just write your application as normal Java application without any Wrapper API parts.

You can use the WrapperSimpleApp to run your application out of the box with codeless integration into the Wrapper.

All you need to do is set the following properties in your conf file:

wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
wrapper.app.parameter.1=textappender.Main
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../MyProject.jar

With this configuration, the Wrapper will be able to run your application as windows service.

UPDATE2

I'm not sure what your code exactly looks like, but it seems you are calling WrapperManager.stop() in your main class...

Following the initial example class, I have modified the class, so it is not using any Wrapper-API (which for an simple application is not necessary):

public class Main {    
public static void main(String[] args) {
    while(true) {
        Logger.getLogger(Main.class.getName()).log(Level.INFO, "I'm alive");
        try 
        { Thread.sleep(2000); } 
        catch (java.lang.InterruptedException ex) 
        { return; }
    }
}
}

After compile and creating the jar, the necessary properties in the conf file are:

wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
wrapper.app.parameter.1=textappender.Main
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../MyProject.jar

cheers,