io.fluxcapacitor.javaclient.tracking.DefaultTracking Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
Default Java client library for interfacing with Flux Capacitor.
package io.fluxcapacitor.javaclient.tracking;
import io.fluxcapacitor.common.MessageType;
import io.fluxcapacitor.common.Registration;
import io.fluxcapacitor.common.api.SerializedMessage;
import io.fluxcapacitor.common.handling.Handler;
import io.fluxcapacitor.common.handling.HandlerInspector;
import io.fluxcapacitor.common.handling.ParameterResolver;
import io.fluxcapacitor.javaclient.FluxCapacitor;
import io.fluxcapacitor.javaclient.common.Message;
import io.fluxcapacitor.javaclient.common.exception.FunctionalException;
import io.fluxcapacitor.javaclient.common.exception.TechnicalException;
import io.fluxcapacitor.javaclient.common.serialization.DeserializingMessage;
import io.fluxcapacitor.javaclient.common.serialization.Serializer;
import io.fluxcapacitor.javaclient.eventsourcing.CacheInvalidatingInterceptor;
import io.fluxcapacitor.javaclient.publishing.ErrorGateway;
import io.fluxcapacitor.javaclient.publishing.ResultGateway;
import io.fluxcapacitor.javaclient.tracking.client.TrackingClient;
import io.fluxcapacitor.javaclient.tracking.client.TrackingUtils;
import io.fluxcapacitor.javaclient.tracking.handling.HandlerInterceptor;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import static io.fluxcapacitor.common.handling.HandlerInspector.createHandlers;
import static java.lang.String.format;
import static java.util.stream.Collectors.groupingBy;
@AllArgsConstructor
@Slf4j
public class DefaultTracking implements Tracking {
private final MessageType messageType;
private final Class extends Annotation> handlerAnnotation;
private final TrackingClient trackingClient;
private final ResultGateway resultGateway;
private final ErrorGateway errorGateway;
private final List configurations;
private final Serializer serializer;
private final HandlerInterceptor handlerInterceptor;
private final List> parameterResolvers;
private final Set startedConfigurations = new HashSet<>();
@Override
public Registration start(FluxCapacitor fluxCapacitor, List> handlers) {
synchronized (this) {
Map> consumers = handlers.stream()
.filter(h -> HandlerInspector.hasHandlerMethods(h.getClass(), handlerAnnotation))
.collect(groupingBy(h -> configurations.stream()
.filter(config -> config.getHandlerFilter().test(h)).findFirst()
.orElseThrow(() -> new TrackingException(format("Failed to find consumer for %s", h)))));
if (!Collections.disjoint(consumers.keySet(), startedConfigurations)) {
throw new TrackingException("Failed to start tracking. "
+ "Consumers for some handlers have already started tracking.");
}
startedConfigurations.addAll(consumers.keySet());
return consumers.entrySet().stream().map(e -> startTracking(e.getKey(), e.getValue(), fluxCapacitor))
.reduce(Registration::merge).orElse(Registration.noOp());
}
}
protected Registration startTracking(ConsumerConfiguration configuration, List