Search in sources :

Example 1 with SubscriberConfigurationProperties

use of io.micronaut.gcp.pubsub.configuration.SubscriberConfigurationProperties in project micronaut-gcp by micronaut-projects.

the class DefaultSubscriberFactory method createSubscriber.

@Override
public Subscriber createSubscriber(SubscriberFactoryConfig config) {
    Subscriber subscriber = subscribers.compute(config.getSubscriptionName(), (k, v) -> {
        if (v == null) {
            Subscriber.Builder builder = Subscriber.newBuilder(config.getSubscriptionName(), config.getReceiver()).setChannelProvider(this.transportChannelProvider).setCredentialsProvider(this.credentialsProvider);
            Optional<SubscriberConfigurationProperties> subscriberConfiguration = beanContext.findBean(SubscriberConfigurationProperties.class, Qualifiers.byName(config.getSubscriberConfiguration()));
            String executor = subscriberConfiguration.map(s -> s.getExecutor()).orElse(config.getDefaultExecutor());
            ExecutorService executorService = beanContext.getBean(ExecutorService.class, Qualifiers.byName(executor));
            if (!(executorService instanceof ScheduledExecutorService)) {
                throw new IllegalStateException("Invalid Executor type provided, please make sure you have a ScheduledExecutorService configured for Subscriber: " + config.getSubscriptionName().getSubscription());
            }
            builder.setExecutorProvider(FixedExecutorProvider.create((ScheduledExecutorService) executorService));
            if (subscriberConfiguration.isPresent()) {
                SubscriberConfigurationProperties properties = subscriberConfiguration.get();
                builder.setMaxAckExtensionPeriod(properties.getMaxAckExtensionPeriod());
                builder.setParallelPullCount(properties.getParallelPullCount());
                builder.setMaxDurationPerAckExtension(properties.getMaxDurationPerAckExtension());
                builder.setFlowControlSettings(properties.getFlowControlSettings().build());
            }
            return builder.build();
        }
        throw new PubSubListenerException(String.format("Subscription %s is already registered for another" + " method", config.getSubscriptionName().toString()));
    });
    subscriber.startAsync();
    return subscriber;
}
Also used : Logger(org.slf4j.Logger) Iterator(java.util.Iterator) BeanContext(io.micronaut.context.BeanContext) SubscriberConfigurationProperties(io.micronaut.gcp.pubsub.configuration.SubscriberConfigurationProperties) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) Singleton(jakarta.inject.Singleton) Subscriber(com.google.cloud.pubsub.v1.Subscriber) PreDestroy(javax.annotation.PreDestroy) SubscriberInterface(com.google.cloud.pubsub.v1.SubscriberInterface) Modules(io.micronaut.gcp.Modules) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) Map(java.util.Map) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) FixedExecutorProvider(com.google.api.gax.core.FixedExecutorProvider) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Optional(java.util.Optional) PubSubListenerException(io.micronaut.gcp.pubsub.exception.PubSubListenerException) ExecutorService(java.util.concurrent.ExecutorService) Named(jakarta.inject.Named) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Subscriber(com.google.cloud.pubsub.v1.Subscriber) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SubscriberConfigurationProperties(io.micronaut.gcp.pubsub.configuration.SubscriberConfigurationProperties) PubSubListenerException(io.micronaut.gcp.pubsub.exception.PubSubListenerException)

Aggregations

CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 SubscriberInterface (com.google.cloud.pubsub.v1.SubscriberInterface)1 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)1 BeanContext (io.micronaut.context.BeanContext)1 Modules (io.micronaut.gcp.Modules)1 SubscriberConfigurationProperties (io.micronaut.gcp.pubsub.configuration.SubscriberConfigurationProperties)1 PubSubListenerException (io.micronaut.gcp.pubsub.exception.PubSubListenerException)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 Named (jakarta.inject.Named)1 Singleton (jakarta.inject.Singleton)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 PreDestroy (javax.annotation.PreDestroy)1