Search in sources :

Example 1 with PublisherConfigurationProperties

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);
    }
}
Also used : BeanContext(io.micronaut.context.BeanContext) BatchingSettings(com.google.api.gax.batching.BatchingSettings) PublisherConfigurationProperties(io.micronaut.gcp.pubsub.configuration.PublisherConfigurationProperties) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) StringUtils(io.micronaut.core.util.StringUtils) NonNull(io.micronaut.core.annotation.NonNull) Publisher(com.google.cloud.pubsub.v1.Publisher) PubSubClientException(io.micronaut.gcp.pubsub.exception.PubSubClientException) Modules(io.micronaut.gcp.Modules) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) FixedExecutorProvider(com.google.api.gax.core.FixedExecutorProvider) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Optional(java.util.Optional) ExecutorService(java.util.concurrent.ExecutorService) Named(jakarta.inject.Named) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PubSubClientException(io.micronaut.gcp.pubsub.exception.PubSubClientException) PublisherConfigurationProperties(io.micronaut.gcp.pubsub.configuration.PublisherConfigurationProperties) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Publisher(com.google.cloud.pubsub.v1.Publisher) BatchingSettings(com.google.api.gax.batching.BatchingSettings) PubSubClientException(io.micronaut.gcp.pubsub.exception.PubSubClientException)

Aggregations

BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 BeanContext (io.micronaut.context.BeanContext)1 NonNull (io.micronaut.core.annotation.NonNull)1 StringUtils (io.micronaut.core.util.StringUtils)1 Modules (io.micronaut.gcp.Modules)1 PublisherConfigurationProperties (io.micronaut.gcp.pubsub.configuration.PublisherConfigurationProperties)1 PubSubClientException (io.micronaut.gcp.pubsub.exception.PubSubClientException)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 Named (jakarta.inject.Named)1 Singleton (jakarta.inject.Singleton)1 Optional (java.util.Optional)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1