Search code examples
jakarta-eejbossjboss7.xwildflywildfly-8

Shutdown hook/event in Wildfly/JBoss


We've got a thread checking if our database is ready. The thread is exited when the database is available. However in some case the Wildfly server is shutting down before a database is ready. In that case, Wildfly won't shutdown since this thread is still alive.

We're looking for a method to notify this thread or a possiblity to check the state (running, shutting down, starting and so on) of the Wildfly server in order to stop that thread.

Any idea?


Solution

  • You should probably set the Thread.setDaemon(true).

    As far as detecting if WildFly is running you could try to connect a ModelControllerClient and check the state of the server. If it doesn't connect it's likely down. Or at least the management connection is down.

    Example:

    public static boolean isServerRunning() throws IOException {
        try (final ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getLocalHost(), 9990)) {
            final ModelNode address = new ModelNode().setEmptyList();
            final ModelNode op = Operations.createReadAttributeOperation(address, "server-state");
            final ModelNode result = client.execute(op);
            if (Operations.isSuccessfulOutcome(result)) {
                final String state = Operations.readResult(result).asString();
                switch (state) {
                    case "running":
                    case "reload-required":
                    case "restart-required":
                        return true;
                }
            }
            return false;
        }
    }