All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fluxcapacitor.javaclient.publishing.QueryGateway Maven / Gradle / Ivy

There is a newer version: 0.1015.0
Show newest version
package io.fluxcapacitor.javaclient.publishing;

import io.fluxcapacitor.common.api.Metadata;
import io.fluxcapacitor.javaclient.common.Message;
import lombok.SneakyThrows;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static java.lang.String.format;

public interface QueryGateway {

    default  CompletableFuture query(Object query) {
        if (query instanceof Message) {
            return query(((Message) query).getPayload(), ((Message) query).getMetadata());
        } else {
            return query(query, Metadata.empty());
        }
    }

    default  CompletableFuture query(Object payload, Metadata metadata) {
        return queryForMessage(payload, metadata).thenApply(Message::getPayload);
    }

    CompletableFuture queryForMessage(Object payload, Metadata metadata);

    default  R queryAndWait(Object query) {
        if (query instanceof Message) {
            return queryAndWait(((Message) query).getPayload(), ((Message) query).getMetadata());
        } else {
            return queryAndWait(query, Metadata.empty());
        }
    }

    @SneakyThrows
    default  R queryAndWait(Object payload, Metadata metadata) {
        CompletableFuture future = query(payload, metadata);
        try {
            Timeout timeout = payload.getClass().getAnnotation(Timeout.class);
            if (timeout != null) {
                return future.get(timeout.millis(), TimeUnit.MILLISECONDS);
            }
            return future.get();
        } catch (TimeoutException e) {
            throw new io.fluxcapacitor.javaclient.publishing.TimeoutException(
                    format("Query %s has timed out", payload), e);
        } catch (InterruptedException e) {
            Thread.interrupted();
            throw new GatewayException(format("Thread interrupted while waiting for result of query %s", payload), e);
        } catch (ExecutionException e) {
            throw e.getCause();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy