You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Steve Cohen <sc...@javactivity.org> on 2008/06/12 18:39:34 UTC

[daemon] "daemonize" existing application - samples?

I have used commons-daemon and jsvc to make Tomcat a service and found 
it worked well following the directions on the Tomcat site.

Now I have a standalone java application with a main() that I would also 
like to "daemonize" and I'm finding the directions a little daunting in 
that they don't explicitly explain how to convert the functionality now 
existing in main() into the format required by daemon.

Would the path forward be to reparcel the existing code of main into the 
appropriate start/stop/init/destroy methods and make my main class 
implement the Daemon interface, or would it be to write a new class that 
wraps the existing main class.

A sample, if it were available, would be a tremendous help here.

Thank you.





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [daemon] "daemonize" existing application - samples?

Posted by Steve Cohen <sc...@javactivity.org>.
Johnny Luong wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> It's probably because people have different thoughts on what the static
> method main() does in a given class.  You will need to identify how the
> lifecycle of your application corresponds to the explicit operations of
> init(), start(), stop(), destroy()... and it may help to look at the
> concrete implementation (assuming unix)
> daemon-1.0.1/src/native/unix/native/jsvc-unix.c:457
>
> So something more concrete might be:
>
> import org.apache.commons.daemon.*
> public class Test
> {
> ~  public void init(DaemonContext context) throws Exception
> ~  {
> ~     // use command line arguments
> ~     // you may want to do configuration stuff here or at the beginning
> of start depending on needs
> ~  }
> ~  Thread t;
> ~  public void start()
> ~  {
> ~    myClass = new ...
> ~    t = new Thread(myClass);
> ~    // this assumes the class implements Runnable...
> ~    t.start();
> ~  }
>
> ~  public void stop()
> ~  {
> ~    // in this example, stop simply sends it an interrupt
> ~    t.interrupt();
> ~  }
>
> ~  public void destroy()
> ~  {
> ~    // wait for the thread to complete after 15 seconds...
> ~    try { t.join(15000); } catch (Exception e) {}
> ~  }
> }
>
> Best,
> Johnny
>
> Steve Cohen wrote:
> | I have used commons-daemon and jsvc to make Tomcat a service and found
> | it worked well following the directions on the Tomcat site.
> |
> | Now I have a standalone java application with a main() that I would 
> also
> | like to "daemonize" and I'm finding the directions a little daunting in
> | that they don't explicitly explain how to convert the functionality now
> | existing in main() into the format required by daemon.
> |
> | Would the path forward be to reparcel the existing code of main into 
> the
> | appropriate start/stop/init/destroy methods and make my main class
> | implement the Daemon interface, or would it be to write a new class 
> that
> | wraps the existing main class.
> |
> | A sample, if it were available, would be a tremendous help here.
> |
> | Thank you.
> |
> |
> |
> |
> |
> | ---------------------------------------------------------------------
> | To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> | For additional commands, e-mail: user-help@commons.apache.org
> |
> |
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iQIcBAEBAgAGBQJIUWpGAAoJEJdI1jIpgaUDkDAP/RXf1P7UwBAn2vUlRXBIkS1Z
> 4stf3aSfoNKpiIRb10tuK3kPus/vl6InmFfB7EvuAN6oxGLxKPF68uUqHAoOyR0m
> 6cV/3OVfH3d+Ndjerko71ERTLqOMNJHCVZw2TlIntdaw8xhXhvFEc93CkKyBsN4L
> w/y1vs3yNATdShvwCyi59Wp/cK5iD6lfk05XPaYgAYqw3EErnU+3E6FhyU397FPU
> A2IGRCvZKG5cCaHKDJe2sEkCtIcZf4OGn11qMOmRRE67shIJfj14/H7NGThhf3e6
> dJPEzsat/zOHvA0W30ViwpwRdOhWlrXev/ezkidGppdwXbvg43kR0k6Hie2h5DdJ
> 1ntYblCZ0NVk/H09lO4Aw25Tld08fsj18kLXtR6uCIw3oUklL4kClY3JzDbHMP7Y
> 9KV2dcsQZz9FPzWmXEtv4K8rqakVY0ACE9cbTt2RYtrjXVZZwg+V4ymkYNGT3l8N
> KgFxHIkd8gwP5Yx9aGxIKmIDzMIiXyEpogsBNCVRIKuZxsugRdglkqo9j51ueHH2
> +JiP+jWTyjWgfRyk08KQgDu6xU38Ay8+9MSgg52MP+/lszUlM95DE/03/E5GVuZW
> leANZhVBULgHwwY/fuGbfDcbnx0jskXWe/nBcEgw9BXc1PuBFd0ATVlYc/G3N74W
> 4nJHrzP5kstnqKedq9BA
> =YHfi
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>
Thanks.  I more or less came up with the same idea and it worked like a 
charm.  You're right, most people give very little thought to what 
happens in main() and it's probably a good thing to be forced to think 
more about it.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [daemon] "daemonize" existing application - samples?

Posted by Johnny Luong <jo...@trustcommerce.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

It's probably because people have different thoughts on what the static
method main() does in a given class.  You will need to identify how the
lifecycle of your application corresponds to the explicit operations of
init(), start(), stop(), destroy()... and it may help to look at the
concrete implementation (assuming unix)
daemon-1.0.1/src/native/unix/native/jsvc-unix.c:457

So something more concrete might be:

import org.apache.commons.daemon.*
public class Test
{
~  public void init(DaemonContext context) throws Exception
~  {
~     // use command line arguments
~     // you may want to do configuration stuff here or at the beginning
of start depending on needs
~  }
~  Thread t;
~  public void start()
~  {
~    myClass = new ...
~    t = new Thread(myClass);
~    // this assumes the class implements Runnable...
~    t.start();
~  }

~  public void stop()
~  {
~    // in this example, stop simply sends it an interrupt
~    t.interrupt();
~  }

~  public void destroy()
~  {
~    // wait for the thread to complete after 15 seconds...
~    try { t.join(15000); } catch (Exception e) {}
~  }
}

Best,
Johnny

Steve Cohen wrote:
| I have used commons-daemon and jsvc to make Tomcat a service and found
| it worked well following the directions on the Tomcat site.
|
| Now I have a standalone java application with a main() that I would also
| like to "daemonize" and I'm finding the directions a little daunting in
| that they don't explicitly explain how to convert the functionality now
| existing in main() into the format required by daemon.
|
| Would the path forward be to reparcel the existing code of main into the
| appropriate start/stop/init/destroy methods and make my main class
| implement the Daemon interface, or would it be to write a new class that
| wraps the existing main class.
|
| A sample, if it were available, would be a tremendous help here.
|
| Thank you.
|
|
|
|
|
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
| For additional commands, e-mail: user-help@commons.apache.org
|
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJIUWpGAAoJEJdI1jIpgaUDkDAP/RXf1P7UwBAn2vUlRXBIkS1Z
4stf3aSfoNKpiIRb10tuK3kPus/vl6InmFfB7EvuAN6oxGLxKPF68uUqHAoOyR0m
6cV/3OVfH3d+Ndjerko71ERTLqOMNJHCVZw2TlIntdaw8xhXhvFEc93CkKyBsN4L
w/y1vs3yNATdShvwCyi59Wp/cK5iD6lfk05XPaYgAYqw3EErnU+3E6FhyU397FPU
A2IGRCvZKG5cCaHKDJe2sEkCtIcZf4OGn11qMOmRRE67shIJfj14/H7NGThhf3e6
dJPEzsat/zOHvA0W30ViwpwRdOhWlrXev/ezkidGppdwXbvg43kR0k6Hie2h5DdJ
1ntYblCZ0NVk/H09lO4Aw25Tld08fsj18kLXtR6uCIw3oUklL4kClY3JzDbHMP7Y
9KV2dcsQZz9FPzWmXEtv4K8rqakVY0ACE9cbTt2RYtrjXVZZwg+V4ymkYNGT3l8N
KgFxHIkd8gwP5Yx9aGxIKmIDzMIiXyEpogsBNCVRIKuZxsugRdglkqo9j51ueHH2
+JiP+jWTyjWgfRyk08KQgDu6xU38Ay8+9MSgg52MP+/lszUlM95DE/03/E5GVuZW
leANZhVBULgHwwY/fuGbfDcbnx0jskXWe/nBcEgw9BXc1PuBFd0ATVlYc/G3N74W
4nJHrzP5kstnqKedq9BA
=YHfi
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org