0

I am looking for any tutorial/example that shows the best practices for one to write a standalone Java class that will run like a server (w/o exiting) and can be stopped by issuing another command from a different invocation of the JVM (sort of like a Tomcat server). Is the best way to look for classes in java.util.concurrent as there are some interesting classes there such as CountDownLatch? An example would be really helpful. Thanks.

John
  • 877
  • 4
  • 11
  • 19

2 Answers2

2

Not sure what you're looking for.

Here is a trivial server that will happily count to MAXINT and back again until it's stopped.

You can use JConsole to stop it.

Server.java

import javax.management.*;
import java.lang.management.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class Server implements ServerMBean {
    AtomicBoolean running;

    public void register() throws Exception {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName serverBeanName = null;
        serverBeanName = new ObjectName("ServerBean:name=TestBean");
        mbs.registerMBean(this, serverBeanName);
    }

    public void stop() {
        running.set(false);
    }

    public void runServer() throws Exception {
        int cnt = 0;
        running = new AtomicBoolean(true);
        while(running.get()) {
            Thread.sleep(1000);
            System.out.println("tic tic " + cnt++);
        }
    }

    public static void main(String args[]) throws Exception {
        Server bean = new Server();
        bean.register();
        bean.runServer();
    }
}

ServerMBean.java

public interface ServerMBean {
    public void stop();
}

This registers a trivial JMX MBean that has a single method (stop), which sets the running variable to "false" and thus stops the loop. Note that 'running' is an AtomicBoolean, as that is important. If you used a normal boolean it is quite possible this would never stop. It would work also with a volatile boolean.

If you start the server (java Server) and then fire up JConsole, it will offer Server as a process to connect to. Then go to the MBeans tab, find ServerBean -> TestBean -> Operations -> stop in the tree view, and click the stop button, and the server will stop.

What you want your server to do, I dunno. But this gives you a taste of what can be done in, what, 40 lines of code...

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • Thanks for a good example. How would one go about having this type of logic w/o calling Thread.sleep() ? – John Jul 22 '11 at 12:00
  • Actually this gives me more ideas now. I can simply use CountdownLatch(1) instead of AtomicBoolean and in the stop method call the countDown() method. – John Jul 22 '11 at 12:17
  • The Thread.sleep was just there to pause the simple loop. You don't want to use a CountDownLatch. CountDownLatches are one time switches that are better used to hold things up, rather than stop things later. I have several threads waiting on a single CDL for the main system to initialize everything, then I set the latch to 0 and they're off to the races. Once its set to 0, you can't reset it. The AtomicBoolean is a good choice for this as its simply global state telling the main loop to run. – Will Hartung Jul 22 '11 at 15:02
0

JMX can be a solution for remotely controlling a Java application. This answer provides details of a JMX based solution for shutting down a JVM.

Note that JMX makes it possible to control your application over the network too. If you don't want to risk a malicious remote shutdown, you'll have to secure the access to the JMX interface. Though a firewall rule to block access to JMX port might be good enough in low risk environments.

Community
  • 1
  • 1
Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69