3

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!

shillner
  • 1,806
  • 15
  • 24

4 Answers4

2

I assume the core question is stopping the deployment process if some health checks fail. Throwing a run-time exception during app startup is enough to do the job.

@Startup
@Singleton
public class StartupBean {


    @PostConstruct
    public void start() {
        //your checks
        boolean check = doHealthCheck();
        if(!check){
           throw new RuntimeException("Your error message");
        }

    }
}

or

@Startup
@Singleton
public class StartupBean {


    @PostConstruct
    public void start() {
        //your checks
        boolean check = doHealthCheck();
        if(!check){
           throw new Error("Your error message");
        }

    }
}
1

I suggest you to try WildFly CLI: Running the CLI or use Marker Files.

But in any case, I'm not sure how the server will behave. For example what will happen when You add file myWarName.dodeploy when there is myWarName.isdeploying. So let us know when You will earn some experience in this topic (it is quite interesting).

pWoz
  • 1,649
  • 3
  • 20
  • 30
  • Thanks but I have already tried to invoke the cli command for shutting down the server using Runtime.exec but without success. And deleting the dodeploy or deployed Marker files could work but is only possible for the deployment-Scanner approach. This way you won't ve able to undeploy the app if it has been deployed using the management Interface. Nevertheless, thanks for your Suggestion! – shillner May 15 '14 at 20:54
0

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.

shillner
  • 1,806
  • 15
  • 24
0

We're using a CDI Extension to abort the deployment if our DB schema doesn't match the application's expectation:

class MyValidatingExtension implements javax.enterprise.inject.spi.Extension {

    void deploymentValidationFinished(@Observes AfterDeploymentValidation afterDeploymentValidation) {
        if (!stateExpected) {
            afterDeploymentValidation.addDeploymentProblem(new IDontLikeThisException());
        }
    }
}

The deployment of the WAR will fail with the stacktrace of the exception listed as DeploymentProblem, leaving your WAR in an undeployed state. This solution is independent of your container implementation, it uses a CDI standard mechanism only. Note that this will not stop/shutdown the server!

blubb
  • 9,510
  • 3
  • 40
  • 82