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;
}
Aggregations