Search in sources :

Example 1 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfiguration method defaultSubscriberFactory.

@Bean
@ConditionalOnMissingBean
public SubscriberFactory defaultSubscriberFactory(@Qualifier("subscriberExecutorProvider") Optional<ExecutorProvider> executorProvider, @Qualifier("subscriberSystemExecutorProvider") ObjectProvider<ExecutorProvider> systemExecutorProvider, @Qualifier("subscriberFlowControlSettings") ObjectProvider<FlowControlSettings> flowControlSettings, @Qualifier("subscriberApiClock") ObjectProvider<ApiClock> apiClock, @Qualifier("subscriberRetrySettings") ObjectProvider<RetrySettings> retrySettings, @Qualifier("healthTrackerRegistry") ObjectProvider<HealthTrackerRegistry> healthTrackerRegistry, @Qualifier("subscriberTransportChannelProvider") TransportChannelProvider subscriberTransportChannelProvider) {
    DefaultSubscriberFactory factory = new DefaultSubscriberFactory(this.finalProjectIdProvider, this.gcpPubSubProperties);
    if (executorProvider.isPresent()) {
        logger.warn("The subscriberExecutorProvider bean is being deprecated. Please use" + " application.properties to configure properties");
        factory.setExecutorProvider(executorProvider.get());
    }
    factory.setExecutorProviderMap(this.executorProviderMap);
    factory.setGlobalExecutorProvider(this.globalExecutorProvider);
    factory.setCredentialsProvider(this.finalCredentialsProvider);
    factory.setHeaderProvider(this.headerProvider);
    factory.setChannelProvider(subscriberTransportChannelProvider);
    systemExecutorProvider.ifAvailable(factory::setSystemExecutorProvider);
    if (flowControlSettings.getIfAvailable() != null) {
        logger.warn("The subscriberFlowControlSettings bean is being deprecated. Please use" + " application.properties to configure properties");
        factory.setFlowControlSettings(flowControlSettings.getIfAvailable());
    }
    factory.setFlowControlSettingsMap(this.subscriberFlowControlSettingsMap);
    factory.setGlobalFlowControlSettings(this.globalFlowControlSettings);
    apiClock.ifAvailable(factory::setApiClock);
    if (retrySettings.getIfAvailable() != null) {
        logger.warn("The subscriberRetrySettings bean is being deprecated. Please use application.properties" + " to configure properties");
        factory.setSubscriberStubRetrySettings(retrySettings.getIfAvailable());
    }
    factory.setRetrySettingsMap(this.subscriberRetrySettingsMap);
    factory.setGlobalRetrySettings(this.globalRetrySettings);
    healthTrackerRegistry.ifAvailable(factory::setHealthTrackerRegistry);
    return factory;
}
Also used : DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method retryableCodes_selectiveConfigurationSet.

@Test
void retryableCodes_selectiveConfigurationSet() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withUserConfiguration(TestConfig.class).withPropertyValues("spring.cloud.gcp.pubsub.subscription.subscription-name.retryableCodes=UNKNOWN,ABORTED,UNAVAILABLE,INTERNAL");
    contextRunner.run(ctx -> {
        DefaultSubscriberFactory defaultSubscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        assertThat(defaultSubscriberFactory.getRetryableCodes("subscription-name")).isEqualTo(new Code[] { Code.UNKNOWN, Code.ABORTED, Code.UNAVAILABLE, Code.INTERNAL });
        assertThat(defaultSubscriberFactory.getRetryableCodes("other")).isNull();
    });
}
Also used : DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method retrySettings_globalConfigurationSet.

@Test
void retrySettings_globalConfigurationSet() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.retry.total-timeout-seconds=1", "spring.cloud.gcp.pubsub.subscriber.retry.initial-retry-delay-seconds=2", "spring.cloud.gcp.pubsub.subscriber.retry.retry-delay-multiplier=3", "spring.cloud.gcp.pubsub.subscriber.retry.max-retry-delay-seconds=4", "spring.cloud.gcp.pubsub.subscriber.retry.max-attempts=5", "spring.cloud.gcp.pubsub.subscriber.retry.jittered=true", "spring.cloud.gcp.pubsub.subscriber.retry.initial-rpc-timeout-seconds=6", "spring.cloud.gcp.pubsub.subscriber.retry.rpc-timeout-multiplier=7", "spring.cloud.gcp.pubsub.subscriber.retry.max-rpc-timeout-seconds=8").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        GcpPubSubProperties gcpPubSubProperties = ctx.getBean(GcpPubSubProperties.class);
        GcpProjectIdProvider projectIdProvider = ctx.getBean(GcpProjectIdProvider.class);
        PubSubConfiguration.Retry retrySettings = gcpPubSubProperties.computeSubscriberRetrySettings("subscription-name", projectIdProvider.getProjectId());
        assertThat(retrySettings.getTotalTimeoutSeconds()).isEqualTo(1L);
        assertThat(retrySettings.getInitialRetryDelaySeconds()).isEqualTo(2L);
        assertThat(retrySettings.getRetryDelayMultiplier()).isEqualTo(3);
        assertThat(retrySettings.getMaxRetryDelaySeconds()).isEqualTo(4);
        assertThat(retrySettings.getMaxAttempts()).isEqualTo(5);
        assertThat(retrySettings.getJittered()).isTrue();
        assertThat(retrySettings.getInitialRpcTimeoutSeconds()).isEqualTo(6);
        assertThat(retrySettings.getRpcTimeoutMultiplier()).isEqualTo(7);
        assertThat(retrySettings.getMaxRpcTimeoutSeconds()).isEqualTo(8);
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        RetrySettings expectedRetrySettings = RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1L)).setInitialRetryDelay(Duration.ofSeconds(2L)).setRetryDelayMultiplier(3).setMaxRetryDelay(Duration.ofSeconds(4L)).setMaxAttempts(5).setInitialRpcTimeout(Duration.ofSeconds(6L)).setRpcTimeoutMultiplier(7).setMaxRpcTimeout(Duration.ofSeconds(8)).build();
        assertThat(subscriberFactory.getRetrySettings("name")).isEqualTo(expectedRetrySettings);
        assertThat(ctx.getBean("globalSubscriberRetrySettings", RetrySettings.class)).isEqualTo(expectedRetrySettings);
    });
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) PubSubConfiguration(com.google.cloud.spring.pubsub.core.PubSubConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method subscriberExecutorProvider_globalConfigurationSet.

@Test
void subscriberExecutorProvider_globalConfigurationSet() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.executor-threads=7").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        DefaultSubscriberFactory factory = (DefaultSubscriberFactory) ctx.getBean("defaultSubscriberFactory");
        ExecutorProvider globalExecutorProvider = (ExecutorProvider) ctx.getBean("globalSubscriberExecutorProvider");
        assertThat(globalExecutorProvider).isNotNull();
        assertThat(factory.getExecutorProvider("other")).isSameAs(globalExecutorProvider);
    });
}
Also used : DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method flowControlSettings_selectiveConfigurationSet.

@Test
void flowControlSettings_selectiveConfigurationSet() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.max-outstanding-element-Count=11", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.max-outstanding-request-Bytes=12", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.limit-exceeded-behavior=Ignore").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        GcpPubSubProperties gcpPubSubProperties = ctx.getBean(GcpPubSubProperties.class);
        GcpProjectIdProvider projectIdProvider = ctx.getBean(GcpProjectIdProvider.class);
        PubSubConfiguration.FlowControl flowControl = gcpPubSubProperties.getSubscriber("subscription-name", projectIdProvider.getProjectId()).getFlowControl();
        assertThat(flowControl.getMaxOutstandingElementCount()).isEqualTo(11L);
        assertThat(flowControl.getMaxOutstandingRequestBytes()).isEqualTo(12L);
        assertThat(flowControl.getLimitExceededBehavior()).isEqualTo(FlowController.LimitExceededBehavior.Ignore);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).hasSize(1);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).containsKey(ProjectSubscriptionName.parse("projects/fake project/subscriptions/subscription-name"));
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        FlowControlSettings expectedFlowControlForSubscriptionName = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(11L).setMaxOutstandingRequestBytes(12L).setLimitExceededBehavior(FlowController.LimitExceededBehavior.Ignore).build();
        assertThat(subscriberFactory.getFlowControlSettings("subscription-name")).isEqualTo(expectedFlowControlForSubscriptionName);
        assertThat(ctx.getBean("subscriberFlowControlSettings-projects/fake project/subscriptions/subscription-name", FlowControlSettings.class)).isEqualTo(expectedFlowControlForSubscriptionName);
    });
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) PubSubConfiguration(com.google.cloud.spring.pubsub.core.PubSubConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultSubscriberFactory (com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory)26 Test (org.junit.jupiter.api.Test)25 ApplicationContextRunner (org.springframework.boot.test.context.runner.ApplicationContextRunner)25 GcpProjectIdProvider (com.google.cloud.spring.core.GcpProjectIdProvider)12 PubSubConfiguration (com.google.cloud.spring.pubsub.core.PubSubConfiguration)10 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)6 RetrySettings (com.google.api.gax.retrying.RetrySettings)6 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)5 Code (com.google.api.gax.rpc.StatusCode.Code)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1