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

pl.allegro.tech.hermes.consumers.consumer.sender.googlepubsub.GooglePubSubMetadataAppender Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
package pl.allegro.tech.hermes.consumers.consumer.sender.googlepubsub;

import com.google.common.collect.ImmutableMap;
import com.google.pubsub.v1.PubsubMessage;
import org.apache.commons.lang3.tuple.Pair;
import pl.allegro.tech.hermes.consumers.consumer.Message;
import pl.allegro.tech.hermes.consumers.consumer.trace.MetadataAppender;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class GooglePubSubMetadataAppender implements MetadataAppender {

    public static final String HEADER_NAME_TOPIC_NAME = "tn";
    public static final String HEADER_NAME_MESSAGE_ID = "id";
    public static final String HEADER_NAME_SUBSCRIPTION_NAME = "sn";
    public static final String HEADER_NAME_TIMESTAMP = "ts";
    public static final String HEADER_NAME_SCHEMA_ID = "sid";
    public static final String HEADER_NAME_SCHEMA_VERSION = "sv";

    @Override
    public PubsubMessage append(PubsubMessage target, Message message) {
        return PubsubMessage.newBuilder(target)
                .putAllAttributes(createMessageAttributes(message))
                .build();
    }

    protected Map createMessageAttributes(Message message) {
        Optional> schemaIdAndVersion = message.getSchema().map(s ->
                Pair.of(String.valueOf(s.getId().value()), String.valueOf(s.getVersion().value())));

        final Map headers = new HashMap<>(ImmutableMap.of(
                HEADER_NAME_TOPIC_NAME, message.getTopic(),
                HEADER_NAME_MESSAGE_ID, message.getId(),
                HEADER_NAME_TIMESTAMP, String.valueOf(message.getPublishingTimestamp())));

        if (message.hasSubscriptionIdentityHeaders()) {
            headers.put(HEADER_NAME_SUBSCRIPTION_NAME, message.getSubscription());
        }

        schemaIdAndVersion.ifPresent(sv -> {
            headers.put(HEADER_NAME_SCHEMA_ID, sv.getLeft());
            headers.put(HEADER_NAME_SCHEMA_VERSION, sv.getRight());
        });

        return headers;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy