/** * Removes given {@code server} from given {@code host} * * @return {@code true} if the server was actually removed, {@code false} otherwise */ public boolean removeServer(String host, String server) throws IOException, OperationException { return ops.removeIfExists(Address.host(host).and(Constants.SERVER_CONFIG, server)); } }
void shutdown(String host, int timeoutInSeconds) throws IOException { if (timeoutInSeconds == 0) { // older versions don't understand the "timeout" parameter ops.invoke(Constants.SHUTDOWN, Address.host(host)); } else { Batch batch = new Batch(); for (String server : allRunningServers(host)) { batch.invoke(Constants.STOP, Address.host(host).and(Constants.SERVER_CONFIG, server), Values.of(Constants.TIMEOUT, timeoutInSeconds)); } batch.invoke(Constants.SHUTDOWN, Address.host(host)); ops.batch(batch); } }
List<String> allRunningServers(String host) throws IOException { ModelNodeResult result = ops.readChildrenNames(Address.host(host), Constants.SERVER); result.assertDefinedValue(); List<String> servers = result.stringListValue(); List<String> startedServers = new ArrayList<String>(); for (String server : servers) { ModelNodeResult serverStatus = ops.readAttribute(Address.host(host).and(Constants.SERVER_CONFIG, server), Constants.STATUS); serverStatus.assertDefinedValue(); if ("STARTED".equals(serverStatus.stringValue())) { startedServers.add(server); } } return startedServers; }
void performRestartOperation(String host, RestartOperation restartOperation) throws IOException, TimeoutException, InterruptedException { List<String> allServers = allRunningServers(host); boolean needsToReconnect = false; try { restartOperation.perform(ops, Address.host(host)); } catch (Throwable e) { // server went down before we received the response, this can happen needsToReconnect = true; } waitUntilServersAreRunning(host, allServers, needsToReconnect); }
/** Retrieves all configured servers on given {@code host}. */ public List<String> allServers(String host) throws IOException { ModelNodeResult result = ops.readChildrenNames(Address.host(host), Constants.SERVER_CONFIG); result.assertDefinedValue(); return result.stringListValue(); }
void shutdownServers(String host, List<String> servers) throws IOException, InterruptedException, TimeoutException { Batch batch = new Batch(); for (String server : servers) { batch.invoke(Constants.STOP, Address.host(host).and(Constants.SERVER_CONFIG, server)); } ops.batch(batch); }
/** * Stops given {@code server} on given {@code host}. * * @return {@code true} when server was successfully stopped, {@code false} otherwise */ public boolean stopServer(String host, String server) throws IOException { ModelNodeResult result = ops.invoke("stop", Address.host(host).and(Constants.SERVER_CONFIG, server), Values.of("blocking", true)); result.assertDefinedValue(); return result.isSuccess(); }
private boolean areServersRunning(String host, List<String> servers) throws IOException { Address hostAddress = Address.host(host); if (servers == null || servers.isEmpty()) { ModelNodeResult result = ops.readAttribute(Address.host(host), Constants.HOST_STATE); result.assertDefinedValue(); return ServerState.isRunning(result.stringValue()); } else { for (String server : servers) { Address serverAddress = hostAddress.and(Constants.SERVER, server); ModelNodeResult result = ops.readAttribute(serverAddress, Constants.SERVER_STATE); if (!result.hasDefinedValue() || !ServerState.isRunning(result.stringValue())) { return false; } } return true; } } }
/** * Returns the Java command the server / host controller was started with. It's read from the platform MBean * server via the management interface. * * @param host should be {@code null} for standalone server or for default host in managed domain; * should only be non-{@code null} if a specific host in managed domain is checked */ private static String commandUsedToStartTheServer(OnlineManagementClient client, String host) throws IOException { Address baseAddress = Address.root(); if (host != null) { baseAddress = Address.host(host); } ModelNodeResult systemProperties = new Operations(client).readAttribute( baseAddress.and(Constants.CORE_SERVICE, Constants.PLATFORM_MBEAN).and("type", "runtime"), "system-properties" ); systemProperties.assertDefinedValue(); // the "sun.java.command" system property exists at least on OpenJDK, Oracle Java and IBM Java return systemProperties.value().get("sun.java.command").asString(); }
/** * Starts given {@code server} on given {@code host}. * * @return {@code true} when server was successfully started, {@code false} otherwise */ public boolean startServer(String host, String server) throws IOException { ModelNodeResult result = ops.invoke(Constants.START, Address.host(host).and(Constants.SERVER_CONFIG, server), Values.of(Constants.BLOCKING, true)); result.assertDefinedValue(); return result.isSuccess(); }
void restartServers(String host, List<String> servers) throws IOException, InterruptedException, TimeoutException { Batch batch = new Batch(); for (String server : servers) { batch.invoke(Constants.RESTART, Address.host(host).and(Constants.SERVER_CONFIG, server)); } boolean needsToReconnect = false; try { ops.batch(batch); } catch (Throwable e) { // server went down before we received the response, this can happen needsToReconnect = true; } domainOps.waitUntilServersAreRunning(host, servers, needsToReconnect); }
boolean isRestartOperationRequired(String host, CommonRestartOperation restartOperation) throws IOException { List<String> allServers = allRunningServers(host); Batch batch = new Batch(); batch.readAttribute(Address.host(host), Constants.HOST_STATE); for (String server : allServers) { batch.readAttribute(Address.host(host).and(Constants.SERVER, server), Constants.SERVER_STATE); } ModelNodeResult result = ops.batch(batch); result.assertDefinedValue(); int counter = 0; // 0 == host, > 0 == server for (ModelNodeResult stepResult : result.forAllBatchSteps()) { if (restartOperation.isRequired(stepResult, counter > 0)) { return true; } counter++; } return false; }
void shutdownServersGracefully(String host, List<String> servers, int timeoutInSeconds) throws IOException, InterruptedException, TimeoutException { client.version().assertAtLeast(ServerVersion.VERSION_3_0_0, "Graceful shutdown is only supported since WildFly 9"); Batch batch = new Batch(); for (String server : servers) { batch.invoke(Constants.STOP, Address.host(host).and(Constants.SERVER_CONFIG, server), Values.of(Constants.TIMEOUT, timeoutInSeconds)); } ops.batch(batch); }