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

io.quarkiverse.reactive.messaging.nats.jetstream.client.api.JetStreamMessage Maven / Gradle / Ivy

There is a newer version: 3.17.0
Show newest version
package io.quarkiverse.reactive.messaging.nats.jetstream.client.api;

import static io.quarkiverse.reactive.messaging.nats.jetstream.mapper.HeaderMapper.toMessageHeaders;
import static io.smallrye.reactive.messaging.providers.locals.ContextAwareMessage.captureContextMetadata;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.function.Supplier;

import org.eclipse.microprofile.reactive.messaging.Metadata;
import org.jboss.logging.Logger;

import io.nats.client.api.MessageInfo;
import io.quarkiverse.reactive.messaging.nats.jetstream.JetStreamIncomingMessageMetadata;

public class JetStreamMessage implements io.quarkiverse.reactive.messaging.nats.jetstream.JetStreamMessage {

    private static final Logger logger = Logger.getLogger(JetStreamMessage.class);

    private final MessageInfo message;
    private Metadata metadata;
    private final JetStreamIncomingMessageMetadata incomingMetadata;
    private final T payload;

    public JetStreamMessage(final MessageInfo message, final T payload) {
        this.message = message;
        this.incomingMetadata = JetStreamIncomingMessageMetadata.of(message);
        this.metadata = captureContextMetadata(incomingMetadata);
        this.payload = payload;
    }

    @Override
    public Metadata getMetadata() {
        return metadata;
    }

    public String getMessageId() {
        return incomingMetadata.messageId();
    }

    public byte[] getData() {
        return message.getData();
    }

    public String getSubject() {
        return incomingMetadata.subject();
    }

    public String getStream() {
        return incomingMetadata.stream();
    }

    public Map> getHeaders() {
        return toMessageHeaders(message.getHeaders());
    }

    @Override
    public T getPayload() {
        return payload;
    }

    @Override
    public Supplier> getAck() {
        return this::ack;
    }

    @Override
    public CompletionStage ack() {
        return CompletableFuture.supplyAsync(() -> {
            logger.debugf("Message with id = %s acknowledged", getMessageId());
            return null;
        });
    }

    @Override
    public CompletionStage nack(Throwable reason, Metadata metadata) {
        return CompletableFuture.supplyAsync(() -> {
            logger.errorf(reason, "Message with id = %s not acknowledged", getMessageId());
            throw new RuntimeException(reason);
        });
    }

    @Override
    public Function> getNack() {
        return this::nack;
    }

    @Override
    public synchronized void injectMetadata(Object metadataObject) {
        this.metadata = metadata.with(metadataObject);
    }

    @Override
    public org.eclipse.microprofile.reactive.messaging.Message addMetadata(Object metadata) {
        injectMetadata(metadata);
        return this;
    }

    @Override
    public String toString() {
        return "JetStreamMessage{" +
                "message=" + message +
                ", metadata=" + metadata +
                ", incomingMetadata=" + incomingMetadata +
                ", payload=" + payload +
                '}';
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy