Search in sources :

Example 1 with PubSub

use of com.google.cloud.pubsub.PubSub in project helios by spotify.

the class EventSenderFactory method build.

public static List<EventSender> build(final Environment environment, final CommonConfiguration<?> config, final MetricRegistry metricRegistry, final String pubsubHealthcheckTopic) {
    final List<EventSender> senders = new ArrayList<>();
    final KafkaClientProvider kafkaClientProvider = new KafkaClientProvider(config.getKafkaBrokers());
    final Optional<KafkaProducer<String, byte[]>> kafkaProducer = kafkaClientProvider.getDefaultProducer();
    kafkaProducer.ifPresent(producer -> senders.add(new KafkaSender(producer)));
    final LifecycleEnvironment lifecycle = environment.lifecycle();
    if (!config.getPubsubPrefixes().isEmpty()) {
        final PubSub pubsub = PubSubOptions.getDefaultInstance().getService();
        final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("pubsub-healthchecker-%d").build());
        // choose an arbitrary prefix to use in the healthcheck. we assume if we can connect to
        // one we can connect to all
        final String topicToHealthcheck = config.getPubsubPrefixes().iterator().next() + pubsubHealthcheckTopic;
        final GooglePubSubSender.DefaultHealthChecker healthchecker = new GooglePubSubSender.DefaultHealthChecker(pubsub, topicToHealthcheck, executor, Duration.ofMinutes(5));
        metricRegistry.register("pubsub-health", (Gauge<Boolean>) healthchecker::isHealthy);
        for (final String prefix : config.getPubsubPrefixes()) {
            final GooglePubSubSender sender = GooglePubSubSender.create(pubsub, prefix, healthchecker);
            senders.add(sender);
        }
        lifecycle.manage(new ManagedPubSub(pubsub));
    }
    senders.forEach(lifecycle::manage);
    return senders;
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) PubSub(com.google.cloud.pubsub.PubSub) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArrayList(java.util.ArrayList) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder)

Aggregations

PubSub (com.google.cloud.pubsub.PubSub)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 LifecycleEnvironment (io.dropwizard.lifecycle.setup.LifecycleEnvironment)1 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1