0

I like to write a little server application being controlled by a little console app (start, pause, stop, etc). For the server spring should be used (part of it already exist). So what I do to start the server is something like this:

  1. start a server thread and exit main method

and then, in the thread:

  1. load application context from xml
  2. start some worker threads connecting to beans doing stuff

Now I want the server to be stopped by another command. How can I connect to the already running context and shut it down?

Thanks for your help, Alexander

avh
  • 178
  • 2
  • 11

2 Answers2

1

The classical way to manage running Java code is through JMX.
Spring lets you export beans as MBeans.

And for accessing JMX via the command line, look at the answers to this recent question:

Calling JMX MBean method from a shell script

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    I tried it with MBeans and got it working. I also refactored the server code itself into a beanm which calls the close method on its own context when its close method is called. Works for now. Thanks for your answer! – avh Dec 10 '10 at 07:31
0

You could create a pid file, when the server starts, it should log the pid to a file, server.pid. When you do a stop, you can read the process and kill it. Or even simpler, have a ever running thread in your main class which keeps looking for a file in some folder. As soon as the file becomes available or gets modified, it will stop the server.

Jinesh Parekh
  • 2,131
  • 14
  • 18