Can a deployment stop itself?

大憨熊 提交于 2019-12-05 18:40:59

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");
        }

    }
}

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

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!