io.fluxcapacitor.javaclient.publishing.RequestGateway 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) 2016-2018 Flux Capacitor.
*
* 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.Registration;
import io.fluxcapacitor.common.api.Metadata;
import io.fluxcapacitor.common.handling.HandlerConfiguration;
import io.fluxcapacitor.javaclient.common.Message;
import io.fluxcapacitor.javaclient.common.serialization.DeserializingMessage;
import io.fluxcapacitor.javaclient.tracking.handling.HasLocalHandlers;
import lombok.SneakyThrows;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static java.lang.String.format;
import static java.lang.Thread.currentThread;
public interface RequestGateway extends HasLocalHandlers {
default void sendAndForget(Object payload) {
sendAndForget(payload instanceof Message ? (Message) payload : new Message(payload));
}
default void sendAndForget(Object payload, Metadata metadata) {
sendAndForget(new Message(payload, metadata));
}
void sendAndForget(Message message);
default CompletableFuture send(Message message) {
return sendForMessage(message).thenApply(Message::getPayload);
}
default CompletableFuture send(Object payload) {
return send(payload instanceof Message ? (Message) payload : new Message(payload));
}
default CompletableFuture send(Object payload, Metadata metadata) {
return send(new Message(payload, metadata));
}
CompletableFuture sendForMessage(Message message);
default R sendAndWait(Object payload) {
return sendAndWait(payload instanceof Message ? (Message) payload : new Message(payload));
}
@SneakyThrows
default R sendAndWait(Object payload, Metadata metadata) {
return sendAndWait(new Message(payload, metadata));
}
@SneakyThrows
default R sendAndWait(Message message) {
CompletableFuture future = send(message);
try {
Timeout timeout = message.getPayload().getClass().getAnnotation(Timeout.class);
if (timeout != null) {
return future.get(timeout.millis(), TimeUnit.MILLISECONDS);
}
return future.get(1, TimeUnit.MINUTES);
} catch (java.util.concurrent.TimeoutException e) {
throw new TimeoutException(
format("%s has timed out", message.getPayload().toString()), e);
} catch (InterruptedException e) {
currentThread().interrupt();
throw new GatewayException(format("Thread interrupted while waiting for result of %s", message.getPayload().toString()), e);
} catch (ExecutionException e) {
throw e.getCause();
}
}
}