public static void main(String[] args) throws Exception {
final ModelControllerClient client = ModelControllerClient.Factory.create("localhost", 9999);
try {
final ModelNode hostNamesOp = new ModelNode();
hostNamesOp.get(OP).set(READ_CHILDREN_NAMES_OPERATION);
hostNamesOp.get(OP_ADDR).setEmptyList();
hostNamesOp.get(CHILD_TYPE).set(HOST);
final ModelNode hostNames = client.execute(hostNamesOp);
final Map<String, Set<String>> hosts = new HashMap<String, Set<String>>();
for(final ModelNode host : hostNames.get(RESULT).asList()) {
final String hostName = host.asString();
final Set<String> serverNames = new HashSet<String>();
hosts.put(host.asString(), serverNames);
final ModelNode serverOp = new ModelNode();
serverOp.get(OP).set(READ_CHILDREN_NAMES_OPERATION);
serverOp.get(OP_ADDR).setEmptyList().add(HOST, host.asString());
serverOp.get(CHILD_TYPE).set(SERVER_CONFIG);
final ModelNode serversResult = client.execute(serverOp);
for(final ModelNode server : serversResult.get(RESULT).asList()) {
final String serverName = server.asString();
serverNames.add(serverName);
final ModelNode serverAddress = new ModelNode();
serverAddress.add(HOST, hostName);
serverAddress.add(SERVER_CONFIG, serverName);
final ModelNode serverConfigOp = new ModelNode();
serverConfigOp.get(OP).set(READ_RESOURCE_OPERATION);
serverConfigOp.get(OP_ADDR).set(serverAddress);
serverConfigOp.get(INCLUDE_RUNTIME).set(true);
final ModelNode serverConfig = client.execute(serverConfigOp);
System.out.println("\nServer:\n");
System.out.println("server name: " + serverName);
System.out.println("host controller name: " + hostName);
System.out.println("server group name: " + serverConfig.get(RESULT, GROUP).asString());
System.out.println("status: " + serverConfig.get(RESULT, "status"));
if("STARTED".equals(serverConfig.get(RESULT, "status").asString())) {
serverNames.add(serverName);
}
}
}
for(final Entry<String, Set<String>> entry : hosts.entrySet()) {
final String hostName = entry.getKey();
for(final String serverName : entry.getValue()) {
final ModelNode address = new ModelNode();
address.add(HOST, hostName);
address.add(SERVER_CONFIG, serverName);
runServerOperation(client, address, "stop");
}
}
Thread.sleep(2000); //
for(final Entry<String, Set<String>> entry : hosts.entrySet()) {
final String hostName = entry.getKey();
for(final String serverName : entry.getValue()) {
final ModelNode address = new ModelNode();
address.add(HOST, hostName);
address.add(SERVER_CONFIG, serverName);
runServerOperation(client, address, "start");
}
}
Thread.sleep(10000); //
for(final Entry<String, Set<String>> entry : hosts.entrySet()) {
final String hostName = entry.getKey();
for(final String serverName : entry.getValue()) {
final ModelNode address = new ModelNode();
address.add(HOST, hostName);
address.add(SERVER_CONFIG, serverName);
runServerOperation(client, address, "restart");
}
}