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.
/*
* Copyright (c) Flux Capacitor IP B.V. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.HandlerFilter;
import io.fluxcapacitor.common.handling.HandlerInvoker;
import io.fluxcapacitor.javaclient.FluxCapacitor;
import io.fluxcapacitor.javaclient.common.ClientUtils;
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.publishing.ResultGateway;
import io.fluxcapacitor.javaclient.tracking.client.DefaultTracker;
import io.fluxcapacitor.javaclient.tracking.handling.HandlerFactory;
import io.fluxcapacitor.javaclient.tracking.handling.Invocation;
import io.fluxcapacitor.javaclient.web.WebRequest;
import lombok.AllArgsConstructor;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
import static io.fluxcapacitor.common.ObjectUtils.unwrapException;
import static io.fluxcapacitor.javaclient.common.ClientUtils.waitForResults;
import static io.fluxcapacitor.javaclient.common.serialization.DeserializingMessage.handleBatch;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
@AllArgsConstructor
@Slf4j
public class DefaultTracking implements Tracking {
private final HandlerFilter handlerFilter = ClientUtils::isTrackingHandler;
private final MessageType messageType;
private final ResultGateway resultGateway;
private final List configurations;
private final List extends BatchInterceptor> generalBatchInterceptors;
private final Serializer serializer;
private final HandlerFactory handlerFactory;
private final Set startedConfigurations = new HashSet<>();
private final Collection> outstandingRequests = new CopyOnWriteArrayList<>();
private final AtomicReference shutdownFunction = new AtomicReference<>(Registration.noOp());
@SuppressWarnings("unchecked")
@Override
@Synchronized
public Registration start(FluxCapacitor fluxCapacitor, List> handlers) {
return fluxCapacitor.apply(fc -> {
Map>> consumers =
assignHandlersToConsumers(handlers).entrySet().stream().flatMap(e -> {
List> converted = e.getValue().stream().flatMap(target -> {
if (target instanceof Handler>) {
return Stream.of((Handler) target);
}
return handlerFactory.createHandler(target, handlerFilter,
e.getKey().getHandlerInterceptors()).stream();
}).collect(toList());
return converted.isEmpty() ? Stream.empty() :
Stream.of(new SimpleEntry<>(e.getKey(), converted));
}).collect(toMap(Entry::getKey, Entry::getValue));
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());
Registration registration =
consumers.entrySet().stream().map(e -> startTracking(e.getKey(), e.getValue(), fc))
.reduce(Registration::merge).orElse(Registration.noOp());
shutdownFunction.updateAndGet(r -> r.merge(registration));
return registration;
});
}
private Map> assignHandlersToConsumers(List> handlers) {
var unassignedHandlers = new ArrayList