Search This Blog
Friday, December 18, 2009
How can I control a headless Java program?
I ran into an interesting idea and will describe it here. The problem is that you have a headless, server type process running and you need a clean way to administer it. One idea is to create main() that starts a thread that actually runs the program. The main thread opens a socket and waits for commands. You can then send a "stop" command which halts the application thread and exits the main thread. I can see this tactic being a great way to start a non-web based Spring application. The application thread does the normal Spring context instantiation, making sure to have Spring register its shutdown hook, while the main thread just waits for a command. Since the command thread isn't going to be under load you don't have to pay any special attention to the listening code, such as using NIO or socket frameworks.