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

org.entur.pubsub.base.EnturGooglePubSubAdmin Maven / Gradle / Ivy

Go to download

Base configuration for Google PubSub clients, to be used for non-Camel clients. Camel-based clients should use camel-entur-google-pubsub instead.

There is a newer version: 3.0
Show newest version
package org.entur.pubsub.base;

import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.cloud.spring.pubsub.PubSubAdmin;
import com.google.pubsub.v1.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EnturGooglePubSubAdmin {

    private static final Logger LOGGER = LoggerFactory.getLogger(EnturGooglePubSubAdmin.class);

    private final PubSubAdmin pubSubAdmin;
    private final boolean autocreate;

    public EnturGooglePubSubAdmin(PubSubAdmin pubSubAdmin, @Value("${entur.pubsub.subscriber.autocreate:true}") boolean autocreate) {
        this.pubSubAdmin = pubSubAdmin;
        this.autocreate = autocreate;
    }

    public void createSubscriptionIfMissing(String destinationName) {

        if (autocreate) {
            try {
                pubSubAdmin.createTopic(destinationName);
                LOGGER.debug("Created topic: {}", destinationName);
            } catch (AlreadyExistsException e) {
                LOGGER.trace("Did not create topic: {}, as it already exists", destinationName);
            }

            try {
                pubSubAdmin.createSubscription(destinationName, destinationName);
                LOGGER.debug("Created subscription: {}", destinationName);
            } catch (AlreadyExistsException e) {
                LOGGER.trace("Did not create subscription: {}, as it already exists", destinationName);
            }
        }
    }

    public void deleteAllSubscriptions() {
        pubSubAdmin.listSubscriptions().stream()
                .map(Subscription::getName)
                .forEach(pubSubAdmin::deleteSubscription);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy