use of io.micronaut.gcp.pubsub.configuration.PublisherConfigurationProperties in project micronaut-gcp by micronaut-projects.
the class DefaultPublisherFactory method createPublisher.
/**
* Creates a publisher for a given topic.
*
* @param config {@link PublisherFactoryConfig} object containing all required properties.
* @return An instance of {@link Publisher} configured using the config and environment properties from `gcp.pubsub.publisher.>config_name<`
*/
@Override
public Publisher createPublisher(@NonNull PublisherFactoryConfig config) {
try {
Publisher.Builder publisherBuilder = Publisher.newBuilder(config.getTopicState().getProjectTopicName());
Optional<PublisherConfigurationProperties> publisherConfiguration = beanContext.findBean(PublisherConfigurationProperties.class, Qualifiers.byName(config.getTopicState().getConfigurationName()));
String executor = publisherConfiguration.map(p -> p.getExecutor()).orElse(config.getDefaultExecutor());
ExecutorService executorService = beanContext.getBean(ExecutorService.class, Qualifiers.byName(executor));
publisherBuilder.setEnableMessageOrdering(config.getTopicState().getOrdered());
if (!StringUtils.isEmpty(config.getTopicState().getEndpoint())) {
publisherBuilder.setEndpoint(config.getTopicState().getEndpoint());
}
if (publisherConfiguration.isPresent()) {
publisherBuilder.setRetrySettings(publisherConfiguration.get().getRetrySettings().build());
// FlowControl had to be flatten in order to be parsed
BatchingSettings batchSettings = publisherConfiguration.get().getBatchingSettings().build();
publisherBuilder.setBatchingSettings(BatchingSettings.newBuilder().setDelayThreshold(batchSettings.getDelayThreshold()).setElementCountThreshold(batchSettings.getElementCountThreshold()).setIsEnabled(batchSettings.getIsEnabled()).setRequestByteThreshold(batchSettings.getRequestByteThreshold()).setFlowControlSettings(publisherConfiguration.get().getFlowControlSettings().build()).build());
}
if (!(executorService instanceof ScheduledExecutorService)) {
throw new IllegalStateException("Invalid Executor type provided, please make sure you have a ScheduledExecutorService configured for Publisher: " + config.getTopicState().getProjectTopicName().getTopic());
}
publisherBuilder.setExecutorProvider(FixedExecutorProvider.create((ScheduledExecutorService) executorService));
publisherBuilder.setChannelProvider(this.transportChannelProvider);
publisherBuilder.setCredentialsProvider(this.credentialsProvider);
return publisherBuilder.build();
} catch (Exception ex) {
throw new PubSubClientException("Failed to create subscriber", ex);
}
}
Aggregations