org.lumongo.server.connection.SocketRequestFederator Maven / Gradle / Ivy
package org.lumongo.server.connection;
import com.hazelcast.core.Member;
import org.lumongo.server.hazelcast.HazelcastManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
public abstract class SocketRequestFederator {
private HazelcastManager hazelcastManager;
private ExecutorService pool;
public SocketRequestFederator(HazelcastManager hazelcastManager, ExecutorService pool) {
this.hazelcastManager = hazelcastManager;
this.pool = pool;
}
public List send(final I request) throws Exception {
Set members = hazelcastManager.getMembers();
List> futureResponses = new ArrayList>();
final Member self = hazelcastManager.getSelf();
for (final Member m : members) {
Future futureResponse = pool.submit(new Callable() {
@Override
public O call() throws Exception {
if (!self.equals(m)) {
return processExternal(m, request);
}
return processInternal(request);
}
});
futureResponses.add(futureResponse);
}
ArrayList results = new ArrayList();
for (Future response : futureResponses) {
try {
O result = response.get();
results.add(result);
}
catch (InterruptedException e) {
throw new Exception("Interrupted while waiting for results");
}
catch (Exception e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
}
throw e;
}
}
return results;
}
public abstract O processExternal(Member m, I request) throws Exception;
public abstract O processInternal(I request) throws Exception;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy