com.googlecode.openbox.server.ConcurrentServerGroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ssh Show documentation
Show all versions of ssh Show documentation
This is remote linux SSH module
The newest version!
package com.googlecode.openbox.server;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ConcurrentServerGroup extends AbstractServerGroup {
private static final Logger logger = LogManager.getLogger();
public ConcurrentServerGroup() {
super();
}
public static ConcurrentServerGroup newInstance() {
return new ConcurrentServerGroup();
}
@Override
public Map visit(final ServerAction action) {
precheckServerGroup();
Server[] servers = listServers();
int num = servers.length;
ExecutorService executorService = Executors.newFixedThreadPool(num);
try {
@SuppressWarnings("unchecked")
final Future[] results = new Future[num];
for (int i = 0; i < num; i++) {
final Server server = servers[i];
Callable task = new Callable() {
@Override
public T call() throws Exception {
try {
return action.access(server);
} catch (Exception e) {
throw new RuntimeException("action.access on server: "
+ server, e);
}
}
};
results[i] = executorService.submit(task);
}
Map responses = new HashMap(num);
for (int i = 0; i < num; i++) {
Future result = results[i];
try {
responses.put(servers[i], result.get());
} catch (Exception e) {
throw new RuntimeException(
"wait and get future result failed !", e);
}
}
return responses;
} finally {
executorService.shutdown();
}
}
}