
io.fluxcapacitor.javaclient.publishing.DefaultGenericGateway 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.publishing;
import io.fluxcapacitor.common.Guarantee;
import io.fluxcapacitor.common.MessageType;
import io.fluxcapacitor.common.api.SerializedMessage;
import io.fluxcapacitor.javaclient.common.Message;
import io.fluxcapacitor.javaclient.common.serialization.DeserializingMessage;
import io.fluxcapacitor.javaclient.common.serialization.Serializer;
import io.fluxcapacitor.javaclient.publishing.client.GatewayClient;
import io.fluxcapacitor.javaclient.tracking.handling.HandlerRegistry;
import io.fluxcapacitor.javaclient.tracking.handling.ResponseMapper;
import io.fluxcapacitor.javaclient.web.WebResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static io.fluxcapacitor.common.Guarantee.SENT;
import static io.fluxcapacitor.javaclient.common.ClientUtils.waitForResults;
import static java.lang.String.format;
@AllArgsConstructor
@Slf4j
public class DefaultGenericGateway implements GenericGateway {
private final GatewayClient gatewayClient;
private final RequestHandler requestHandler;
private final Serializer serializer;
private final DispatchInterceptor dispatchInterceptor;
private final MessageType messageType;
@Delegate
private final HandlerRegistry localHandlerRegistry;
private final ResponseMapper responseMapper;
private final Map> callbacks = new ConcurrentHashMap<>();
@Override
@SneakyThrows
public CompletableFuture sendAndForget(Guarantee guarantee, Message... messages) {
List serializedMessages = new ArrayList<>();
for (Message message : messages) {
message = dispatchInterceptor.interceptDispatch(message, messageType);
if (message == null) {
continue;
}
dispatchInterceptor.monitorDispatch(message, messageType);
Optional> localResult
= localHandlerRegistry.handle(new DeserializingMessage(message, messageType, serializer));
if (localResult.isEmpty()) {
SerializedMessage serializedMessage = dispatchInterceptor.modifySerializedMessage(
message.serialize(serializer), message, messageType);
if (serializedMessage == null) {
continue;
}
serializedMessages.add(serializedMessage);
} else {
if (localResult.get().isCompletedExceptionally()) {
try {
localResult.get().getNow(null);
} catch (CompletionException e) {
log.error("Handler failed to handle a {}",
message.getPayloadClass().getSimpleName(), e.getCause());
}
}
}
}
if (!serializedMessages.isEmpty()) {
try {
return gatewayClient.append(guarantee, serializedMessages.toArray(new SerializedMessage[0]));
} catch (Exception e) {
throw new GatewayException(format("Failed to send and forget %s messages", messages.length), e);
}
}
return CompletableFuture.completedFuture(null);
}
@SuppressWarnings("unchecked")
@Override
public List> sendForMessages(Message... messages) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy