Tuesday, January 06, 2009

Java and daemons

I have to say for Java is pretty much OK for daemons, if you want same daemon to be cross-platform out of the box and you do not mind a thing is eating like 30M of RAM or bit more. It is possible to achieve it, once some certain rules are kept strict. You have to remember: you will get entire Java VM instance for your daemon. Therefore before you go to this area, think twice: maybe C or C++ or even Digital Mars D are much better language choices for you?

You might want Java daemons in sort of cases, like:
  • You want to connect to RDBMS, but you can not use ODBC for some reasons or it is just impossible to install client on current OS. For example, you have OpenBSD but need to connect to Oracle. JDBC would work here just perfect. You will not get HotSpot on OpenBSD, but yet you can use Kaffe VM or Cacao VM or JamVM and GNU/Classpath. I tried them all, works almost great, but JamVM is just really meant to be small for embedded devices, hence performance is not that really great.
  • You have something very special inside and do not want to waste time porting your daemon separately to Solaris, to Mac OS X, to Linux, to IRIX, to AIX, to HP UX, to FreeBSD etc.
  • You do not want to waste your time banging your head into the wall, coding with C++, because you realized that library you need either does not exists yet and you have to DIY it or just very bad and you need make it good yourself, before you write your desired daemon.
  • etc.

Your daemon must be really small, simple and clean at implementation. To get that, you definitely have to throw away mostly all Java crappy design patterns. They sucks anyway. Forget about those stupid adapters, bridges, factories and other layered crap, but just do directly what you have to and think like you're coding almost in plain C.

Since Java sucks hard at system programming, because it's VM is almost like an OS itself, there is no such things as .fork(), unfortunately. Therefore, for plain Unix you might use init.d, using shell script with regular running in background task technique would do the trick. For Mac OS X you definitely want to use launchd and it is opposite: your script should not detach, otherwise launchd will think your daemon is dead. For Windows... well, there is daemon stuff from Apache Commons. They also have wrapper for Java on Linux, but as much as I tried, it works horrible and unstable, in my opinion. Additionally, you want to read this article.

The Windows thing... Well, honestly I dunno much what else you can use for Windows, since I am not big fan of this strange OS and have no one around: my servers are all Unix and for desktop I still prefer Mac. But there are must be some wrappers, registry keys for automatically booting it up on start-up or so and other things though...

Happy coding! :-)

No comments: