Skip to main content

Shutting down GlassFish remotely

Posted by kohsuke on October 25, 2007 at 2:16 PM PDT

We run a lot of tests of Metro on Hudson with GlassFish, but there's one common problem we had.

Namely, often test jobs abort in the middle, leaving an application server running behind. This not only wastes memory, but it also wrecks havoc to successive builds that attempt to start the server on the same port.
Obviously you can shut down a server if you have access to asadmin script, but for programs like Hudson having that sort of dependencies is problematic.

I know Tomcat has a very easy way to shut down as long as you know the port number — you just need to send a magic word. So I started wonder if I can do something similar for GlassFish.

I did a bit of research, and I found that doing this is fairly easy with JMX. The following is the entire program:

public class Main {
    public static void main(String[] args) throws Exception {
        // shutdown("localhost:8686", "admin", "adminadmin");
        if(args.length!=3) {
            System.err.println("Usage: java -jar shutdown-gf.jar ");
            System.err.println("  e.g., java -jar shutdown-gf.jar localhost:8686 admin adminadmin");
            System.exit(-1);
        }
       
        shutdown(args[0],args[1],args[2]);
        System.exit(0);
    }

    private static void shutdown(String hostAndPort, String username, String password) throws Exception {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+hostAndPort+"/jmxrmi");

        Map envs = new HashMap();
        envs.put(JMXConnector.CREDENTIALS,new String[]{username, password});

        MBeanServerConnection con = JMXConnectorFactory.connect(url,envs).getMBeanServerConnection();
        try {
            con.invoke(new ObjectName("amx:j2eeType=J2EEServer,name=server"),"stop",new Object[0],new String[0]);
        } catch (UnmarshalException e) {
            if(e.getCause() instanceof EOFException) {
                // to be expected, as the above would shut down the server.
            } else {
                throw e;
            }
        }
    }
}

For this to work you need to know three things. The hostname/IP where GlassFish runs, TCP port of JMX (normally 8686 but asadmin start-domain will tell you), and user name and password of the domain administrator.

I hope to integrate this into a Hudson plugin that Rama and I have been writing, but that will be a topic for another day.

Related Topics >>

Comments

<p>Kohsuke</p> <p>Thanks for this, it really helped ...

Kohsuke
Thanks for this, it really helped alot!
Just want to know if you could help. After if shut the server down, with the "stop" command, the J2EEServer's mbean state goes to 3. But i have no idea that 3 means? Where or how could i get a discription on this?
Searching the web i found that -1 is failed, 0 is stopped, and 1 is running, but could not find anything on what 3 is.
Any help would be greatly appreciated.
Winston