Search code examples
javajbosswildflywildfly-8

Can a deployment stop itself?


I'm currently searching for a way to stop a deployment on wildfly programmatically.

Background:

  • The application does some health checks in its startup phase using an MBean.
  • If the app determines that the environment is not valid because some resources are missing, it needs to stop its own deployment.

The way it was:

  • The application was formerly running on JBoss 4 and simply stopped the whole app server calling the shutdown command using JMX.
  • In case this command failed, it simply terminated the whole JVM using System.exit(1).

Current problems:

  • Calling shutdown() via JMX does not work on wildfly since the whole server hangs when trying to stop it from within a deployed application.
  • System.exit() will also not work since wildly must be catching the command in any way.

So does anyone know how to stop the server from within the deployment or stop the deployment process or undeploy the app?

Thanks a lot!


Solution

  • Ok, I did not yet manage to undeploy the app but I've been able to shutdown the server in case of an error. This is not perfect but matches the behavior of the app on the older version of JBoss, so I think it's not too bad at all.

    I'm now calling the CLI interface like so

    try {
      String jbossBinDir = System.getProperty("jboss.server.base.dir").replace("standalone", "bin");
      Runtime.getRuntime().exec("sh " + jbossBinDir + "/jboss-cli.sh -c command=:shutdown");
    } catch(IOException e) {
      ...
    }
    

    This works reliable for us.

    In my comment above I stated that the execution returns with an error code but this was probably the case because I must have had a typo in the command call.