PDA

View Full Version : How to create daemon thread?




stevejobbers
Jul 13, 2009, 07:49 PM
I wrote a simple Java program that I'd like to run when I login and have it continue to run in the background (it's a server for a simple client/server program).

Currently I have a 'command' file that I'm using to start the process, but I have to keep doing it manually and it opens a Terminal window on the dock. I'm hoping there is a way to configure it to run in the background automatically when I log in.

Thanks guys!!!



Cromulent
Jul 14, 2009, 04:02 AM
I wrote a simple Java program that I'd like to run when I login and have it continue to run in the background (it's a server for a simple client/server program).

Currently I have a 'command' file that I'm using to start the process, but I have to keep doing it manually and it opens a Terminal window on the dock. I'm hoping there is a way to configure it to run in the background automatically when I log in.

Thanks guys!!!

You might want to look into Launchd. Although I am not sure if it supports Java, I see no reason why it wouldn't but do not know for sure.

larkost
Jul 14, 2009, 11:51 AM
Cromulent is correct, using a Launchd agent is the right way of going.

And just a note: you want a daemon process, not thread. Threads aren't really daemons (python has "daemon threads", but that is a different concept).

stevejobbers
Jul 14, 2009, 08:31 PM
thanks, i'll give it a shot.

is there a good resource online or tutorial? i tried some basic google searches but got kinda lost...

rrpalma
Jul 14, 2009, 11:19 PM
You might want to check Bruce Eckel's free on-line (and downloadable) book Thinking in Java. If I remember correctly, there's a couple of examples.

What you should basically consider doing thin infinite loop:

done = false;
while (!done) {
.....
//update done if necessary
}

then inside the loop, use a socket to hear for requests on a specific port, and when you get a request, create a new thread to service that request. Main thread keeps listening for new incoming requests.

Then use launchd to invoke this program via java "YourProgram"

Hope that helps.

Cromulent
Jul 15, 2009, 05:08 AM
You might want to check Bruce Eckel's free on-line (and downloadable) book Thinking in Java. If I remember correctly, there's a couple of examples.

What you should basically consider doing thin infinite loop:

done = false;
while (!done) {
.....
//update done if necessary
}

then inside the loop, use a socket to hear for requests on a specific port, and when you get a request, create a new thread to service that request. Main thread keeps listening for new incoming requests.

Then use launchd to invoke this program via java "YourProgram"

Hope that helps.

A launchd server process should quit when not in use so that infinite loop breaks best practice policy. Launchd handles the loading and unloading of server processes as and when they are required to conserve memory and CPU resources.

stevejobbers
Jul 15, 2009, 06:11 AM
thanks for all the responses. the java program already runs in a loop, so that's not an issue. the only issue is trying to get the program to automatically start when i log in, and run in the background. everytime i google for launchd it says that i'll have to create plist files and other stuff.... i thought all i would have to do is add a command to some login script somewhere.

regardless, i found an application, Lingon, that seems to have worked for me. the process started up at login and continues to run in the background.

it's a free app, in case anyone else wants/needs to use it.

rrpalma
Jul 15, 2009, 09:06 AM
A launchd server process should quit when not in use so that infinite loop breaks best practice policy. Launchd handles the loading and unloading of server processes as and when they are required to conserve memory and CPU resources.

Very true. Just providing a quick and dirty solution :o:o