UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2005-04-28 01:51 PM

ineo
Member
From: Zion
Registered: 2004-11-28
Posts: 22

Re: Server shutdown

How do you shutdown a server in Java?
For that matter how do you write a daemon in Java?


The problem is choice!

Offline

#2 2005-04-29 02:26 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Server shutdown

That is one of the things lacking in Java...

Its missing for a number of reason... not the least of which being that different popular platforms, for example Windows and *NIX systems, use very different methods to achieve the same goal...

Don't despair though... instead check out...

http://jakarta.apache.org/commons/daemon/

Its a combo of C and Java source... also facilitates doing shutdowns...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2005-04-29 04:13 AM

ineo
Member
From: Zion
Registered: 2004-11-28
Posts: 22

Re: Server shutdown

So are there any Servers you know of that are written in Java completely?
If their Open Source that would be great. And not too greatly advanced. :)


The problem is choice!

Offline

#4 2005-04-29 08:13 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Server shutdown

Again, at least that I know of...

There are no pure Java solutions since every platform seems to handle things such as the terminal, users, groups and etc. differently...  some such as MS-Dos don't even have a concept of the latter items... which is rational behind just giving you the URL for the combo Java / native code previously...

Still don't know what platform you are working on... but just for putting something in the background on a *NIX type system... and note it still requires some shell commands... you could try:

nohup java -jar foo.jar </dev/null >>/dev/null 2>>/dev/null &

The nohup will disconnect the JVM from the console so that if the user logs out of the terminal it will continue running i.e. it will ignore the terminal hang up signal...

The center is of course the java command you want to run...

The three /dev/nulls represent standard input, output and error streams and can be changed to any file names... the given one assumes you want to just throw out / disconnect them...

The final & says to run it all in the background...


There is also the daemon command... but in my experience the nohup seems to be universally available...


I didn't ask before ... but on the shutdown side of things...

If you are just looking for "clean up" code for your application / server... the Runtime.getRuntime().addShutdownHook( Thread hook ) allows you to do that...



Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2005-04-29 08:27 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Server shutdown

Just a heads up in case the question arises... the java.lang.Process class / objects don't really work for this either...

A direct quote from the current API docs:

"The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts."

So creating a new Process using the Runtime or ProcessBuilder doesn't seem to be a cure... 


Also... the threadObj.setDaemon( true ) call cannot be used as a single step solution... since the JVM explicitly states that when all threads are daemons the JVM will exit...


Not trying to be the bearer of bad news... but I have gone through the same thing in the past myself... and its just one of the things you have to live with when writing this type of program in Java...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2005-04-29 12:54 PM

ineo
Member
From: Zion
Registered: 2004-11-28
Posts: 22

Re: Server shutdown

I want to be able to run the server on both Windows as well as Linux. After all isn't that where the power of Java is supposed to be?


The problem is choice!

Offline

#7 2005-04-29 01:35 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Server shutdown

Of course the cross platform useage was / is a big selling point for Java...

The problem is that some tasks are just done so differently by the various OSes that during the design of the language there wasn't enough of a common point to create a unified API...

Making daemons / background processes / services is just one of those things...

You also have to remember the genesis of the language...  first full release was in December 1995 when Unix was out there but Linux was still in its infancy... Mac OS was pre-version 10... and Windows was Win 95...

Thats also the reason things like getting and setting the user / group etc. of a process is not defined...  I mean, the Mac and Windows 95 didn't even have true concepts of security, users, groups or etc. at that time...

Again, take a look at the URL I provided earlier... Here it is again:

http://jakarta.apache.org/commons/daemon/

To make your Java application a "service" on Windows you just need to call your Java main with the binary helper...  for *NIX systems ( which should include Mac OS X now ) you just need to subclass off their helper and it's binary...
 

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#8 2005-04-29 01:42 PM

ineo
Member
From: Zion
Registered: 2004-11-28
Posts: 22

Re: Server shutdown

So you mean to say that I will need a binary helper for each platform?


The problem is choice!

Offline

#9 2005-04-29 01:42 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: Server shutdown

I want to be able to run the server on both Windows as well as Linux. After all isn't that where the power of Java is supposed to be?

Heh.  Only if you believe the marketing drones at Sun... ;-)  Yep, that's sure how
Java was sold: a universally-portable, write-once/run-anywhere utopia of a language,
which would solve all of mankind's problems!  Unfortunately, that's nothing like the
reality of the situation... ;-)

In truth, you're probably much better off portability-wise, if you write in ANSI C
(sticking only to standard ANSI/POSIX functions), and compile your code on each
target platform...  Or, if you need an interpreted/byte-coded Java-like approach,
use something like Perl or Python, which has implementations on pretty much every
system in existence, as well...  I really don't see the use of Java at all, personally...

Offline

#10 2005-04-29 02:03 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Server shutdown

>> Ignoring Rob <<

:-P


Yes, for this particular situation you will need a binary helper...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#11 2009-01-01 05:25 PM

lilokster
Guest

Re: Server shutdown

I don't think there are any servers written completely in Java.

#12 2009-12-16 04:37 PM

Naguellegange
Guest

Re: Server shutdown

I am trying to install the Flash chat using either PHP or Java Socket.

None of the instructions provided in this forum or in the documentation itself matches what I see in my newly downloaded demo.

For example, I do not have patServer folder. I do not have the a line:

enableSocketServer => true,

in inc/config.php

So since nothing matches the docs or the forum, I am not sure how to get the socket server running?

Also, does the Java socket server option need to be started with a shell command as well?

Can the PHP socket service be left on? Is there an option where the socket server is started and killed automaticaly?

Board footer

Powered by FluxBB