com.opencredo.concourse.domain.commands.dispatching.PartitioningCommandExecutor Maven / Gradle / Ivy
package com.opencredo.concourse.domain.commands.dispatching;
import com.opencredo.concourse.domain.commands.Command;
import com.opencredo.concourse.domain.commands.CommandResult;
import com.opencredo.concourse.domain.common.AggregateId;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class PartitioningCommandExecutor implements CommandExecutor {
public static PartitioningCommandExecutor processingWith(CommandProcessor commandProcessor, int partitionCount) {
return partitioningBetween(IntStream.range(0, partitionCount)
.mapToObj(i -> ThreadpoolCommandExecutor.singleThreaded(commandProcessor))
.collect(Collectors.toList()));
}
public static PartitioningCommandExecutor partitioningBetween(List executors) {
return new PartitioningCommandExecutor(
pickPartition(executors.size()).andThen(executors::get));
}
private static Function pickPartition(int partitionCount) {
return aggregateId -> Math.abs(aggregateId.hashCode() % partitionCount);
}
private final Function executorPicker;
public PartitioningCommandExecutor(Function executorPicker) {
this.executorPicker = executorPicker;
}
@Override
public void accept(Command command, CompletableFuture future) {
executorPicker.apply(command.getAggregateId()).accept(command, future);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy