All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.zulia.server.index.federator.NodeRequestFederator Maven / Gradle / Ivy

There is a newer version: 1.6.4
Show newest version
package io.zulia.server.index.federator;

import io.zulia.message.ZuliaBase.Node;
import io.zulia.server.index.NodeRequestBase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public abstract class NodeRequestFederator extends NodeRequestBase {

	private final ExecutorService pool;

	protected final Set nodes;

	public NodeRequestFederator(Node thisNode, Collection otherNodesActive, ExecutorService pool) {
		super(thisNode, otherNodesActive);
		this.pool = pool;
		this.nodes = new HashSet<>();
	}

	public List send(final I request) throws Exception {

		List> futureResponses = new ArrayList<>();

		for (final Node node : nodes) {

			Future futureResponse = pool.submit(() -> {
				if (nodeIsLocal(node)) {
					return processInternal(node, request);
				}
				return processExternal(node, request);

			});

			futureResponses.add(futureResponse);
		}

		ArrayList results = new ArrayList<>();
		for (Future response : futureResponses) {
			try {
				O result = response.get();
				results.add(result);
			}
			catch (InterruptedException e) {
				throw e;
			}
			catch (Exception e) {
				Throwable cause = e.getCause();
				if (cause instanceof Exception) {
					throw (Exception) cause;
				}

				throw e;
			}
		}

		return results;

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy