John
John

Reputation: 887

Writing a long lived Java class that can be stopped from a different JVM

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.

Upvotes: 0

Views: 162

Answers (2)

Will Hartung
Will Hartung

Reputation: 118691

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...

Upvotes: 2

Tahir Akhtar
Tahir Akhtar

Reputation: 11625

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.

Upvotes: 0

Related Questions