From e9888a664951a470bfdceaa64d280fedcf6076d1 Mon Sep 17 00:00:00 2001 From: Shin Fan Date: Mon, 11 Apr 2016 15:20:52 -0700 Subject: [PATCH] Update pubsub client with latest surface changes. (#885) Update pubsub client with latest surface changes, add throws in javadoc and update gax dependency to 0.0.9 --- .../gcloud/pubsub/spi/v1/PublisherApi.java | 70 +-- .../pubsub/spi/v1/PublisherSettings.java | 429 +++++++++------- .../gcloud/pubsub/spi/v1/SubscriberApi.java | 70 ++- .../pubsub/spi/v1/SubscriberSettings.java | 463 ++++++++++-------- gcloud-java-pubsub/pom.xml | 2 +- .../gcloud/pubsub/spi/v1/PublisherApi.java | 70 +-- .../pubsub/spi/v1/PublisherSettings.java | 429 +++++++++------- .../gcloud/pubsub/spi/v1/SubscriberApi.java | 70 ++- .../pubsub/spi/v1/SubscriberSettings.java | 463 ++++++++++-------- .../pubsub/spi/v1/PublisherApiTest.java | 26 +- 10 files changed, 1211 insertions(+), 881 deletions(-) diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java index 8394e5ae74ed..1d8c00c4e910 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java @@ -34,8 +34,6 @@ package com.google.gcloud.pubsub.spi.v1; import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.ApiCallable.BundlableApiCallableInfo; -import com.google.api.gax.grpc.BundlerFactory; import com.google.api.gax.protobuf.PathTemplate; import com.google.protobuf.Empty; import com.google.pubsub.v1.DeleteTopicRequest; @@ -80,23 +78,9 @@ public class PublisherApi implements AutoCloseable { listTopicSubscriptionsIterableCallable; private final ApiCallable deleteTopicCallable; - /** - * A PathTemplate representing the fully-qualified path to represent - * a project resource. - * - * - * - */ private static final PathTemplate PROJECT_PATH_TEMPLATE = PathTemplate.create("projects/{project}"); - /** - * A PathTemplate representing the fully-qualified path to represent - * a topic resource. - * - * - * - */ private static final PathTemplate TOPIC_PATH_TEMPLATE = PathTemplate.create("projects/{project}/topics/{topic}"); @@ -161,8 +145,8 @@ public static final String parseTopicFromTopicName(String topicName) { * * */ - public static final PublisherApi create() throws IOException { - return create(PublisherSettings.create()); + public static final PublisherApi defaultInstance() throws IOException { + return create(PublisherSettings.defaultInstance()); } /** @@ -188,22 +172,20 @@ public static final PublisherApi create(PublisherSettings settings) throws IOExc protected PublisherApi(PublisherSettings settings) throws IOException { this.channel = settings.getChannel(); - this.createTopicCallable = settings.createTopicMethod().build(settings); - BundlableApiCallableInfo bundlablePublish = - settings.publishMethod().buildBundlable(settings); - this.publishCallable = bundlablePublish.getApiCallable(); - BundlerFactory publishBundlerFactory = - bundlablePublish.getBundlerFactory(); - if (publishBundlerFactory != null) { - this.closeables.add(publishBundlerFactory); + this.createTopicCallable = ApiCallable.create(settings.createTopicSettings(), settings); + this.publishCallable = ApiCallable.create(settings.publishSettings(), settings); + if (settings.publishSettings().getBundlerFactory() != null) { + closeables.add(settings.publishSettings().getBundlerFactory()); } - this.getTopicCallable = settings.getTopicMethod().build(settings); - this.listTopicsCallable = settings.listTopicsMethod().build(settings); - this.listTopicsIterableCallable = settings.listTopicsMethod().buildPageStreaming(settings); - this.listTopicSubscriptionsCallable = settings.listTopicSubscriptionsMethod().build(settings); + this.getTopicCallable = ApiCallable.create(settings.getTopicSettings(), settings); + this.listTopicsCallable = ApiCallable.create(settings.listTopicsSettings(), settings); + this.listTopicsIterableCallable = + ApiCallable.createIterable(settings.listTopicsSettings(), settings); + this.listTopicSubscriptionsCallable = + ApiCallable.create(settings.listTopicSubscriptionsSettings(), settings); this.listTopicSubscriptionsIterableCallable = - settings.listTopicSubscriptionsMethod().buildPageStreaming(settings); - this.deleteTopicCallable = settings.deleteTopicMethod().build(settings); + ApiCallable.createIterable(settings.listTopicSubscriptionsSettings(), settings); + this.deleteTopicCallable = ApiCallable.create(settings.deleteTopicSettings(), settings); if (settings.shouldAutoCloseChannel()) { closeables.add( @@ -231,6 +213,7 @@ public void close() throws IOException { * underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent * signs (`%`). It must be between 3 and 255 characters in length, and it * must not start with `"goog"`. + * @throws ApiException if the remote call fails */ public final Topic createTopic(String name) { Topic request = Topic.newBuilder().setName(name).build(); @@ -246,6 +229,7 @@ public final Topic createTopic(String name) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private Topic createTopic(Topic request) { return createTopicCallable().call(request); @@ -257,6 +241,7 @@ private Topic createTopic(Topic request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable createTopicCallable() { return createTopicCallable; @@ -275,6 +260,7 @@ public final ApiCallable createTopicCallable() { * * @param topic The messages in the request will be published on this topic. * @param messages The messages to publish. + * @throws ApiException if the remote call fails */ public final PublishResponse publish(String topic, List messages) { PublishRequest request = @@ -293,6 +279,7 @@ public final PublishResponse publish(String topic, List messages) * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public PublishResponse publish(PublishRequest request) { return publishCallable().call(request); @@ -306,6 +293,7 @@ public PublishResponse publish(PublishRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable publishCallable() { return publishCallable; @@ -321,6 +309,7 @@ public final ApiCallable publishCallable() { * * * @param topic The name of the topic to get. + * @throws ApiException if the remote call fails */ public final Topic getTopic(String topic) { GetTopicRequest request = GetTopicRequest.newBuilder().setTopic(topic).build(); @@ -336,6 +325,7 @@ public final Topic getTopic(String topic) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private Topic getTopic(GetTopicRequest request) { return getTopicCallable().call(request); @@ -347,6 +337,7 @@ private Topic getTopic(GetTopicRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable getTopicCallable() { return getTopicCallable; @@ -360,6 +351,9 @@ public final ApiCallable getTopicCallable() { * * * + * + * @param project The name of the cloud project that topics belong to. + * @throws ApiException if the remote call fails */ public final Iterable listTopics(String project) { ListTopicsRequest request = ListTopicsRequest.newBuilder().setProject(project).build(); @@ -374,6 +368,7 @@ public final Iterable listTopics(String project) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public final Iterable listTopics(ListTopicsRequest request) { return listTopicsIterableCallable().call(request); @@ -385,6 +380,7 @@ public final Iterable listTopics(ListTopicsRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable> listTopicsIterableCallable() { return listTopicsIterableCallable; @@ -396,6 +392,7 @@ public final ApiCallable> listTopicsIterableC * * * + * @throws ApiException if the remote call fails */ public final ApiCallable listTopicsCallable() { return listTopicsCallable; @@ -409,6 +406,9 @@ public final ApiCallable listTopicsCallab * * * + * + * @param topic The name of the topic that subscriptions are attached to. + * @throws ApiException if the remote call fails */ public final Iterable listTopicSubscriptions(String topic) { ListTopicSubscriptionsRequest request = @@ -424,6 +424,7 @@ public final Iterable listTopicSubscriptions(String topic) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsRequest request) { return listTopicSubscriptionsIterableCallable().call(request); @@ -435,6 +436,7 @@ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsReque * * * + * @throws ApiException if the remote call fails */ public final ApiCallable> listTopicSubscriptionsIterableCallable() { @@ -447,6 +449,7 @@ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsReque * * * + * @throws ApiException if the remote call fails */ public final ApiCallable listTopicSubscriptionsCallable() { @@ -467,6 +470,7 @@ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsReque * * * @param topic Name of the topic to delete. + * @throws ApiException if the remote call fails */ public final void deleteTopic(String topic) { DeleteTopicRequest request = DeleteTopicRequest.newBuilder().setTopic(topic).build(); @@ -486,6 +490,7 @@ public final void deleteTopic(String topic) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private void deleteTopic(DeleteTopicRequest request) { deleteTopicCallable().call(request); @@ -501,6 +506,7 @@ private void deleteTopic(DeleteTopicRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable deleteTopicCallable() { return deleteTopicCallable; diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java index 8b3d434b8e49..11c709024144 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java @@ -33,19 +33,17 @@ package com.google.gcloud.pubsub.spi.v1; -import com.google.api.gax.core.BackoffParams; import com.google.api.gax.core.ConnectionSettings; -import com.google.api.gax.core.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.api.gax.grpc.ApiCallSettings; -import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.ApiCallable.Builder; -import com.google.api.gax.grpc.ApiCallable.BundlableBuilder; -import com.google.api.gax.grpc.ApiCallable.PageStreamingBuilder; +import com.google.api.gax.grpc.BundlingCallSettings; import com.google.api.gax.grpc.BundlingDescriptor; import com.google.api.gax.grpc.BundlingSettings; +import com.google.api.gax.grpc.PageStreamingCallSettings; import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.RequestIssuer; import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -63,10 +61,13 @@ import com.google.pubsub.v1.PublisherGrpc; import com.google.pubsub.v1.PubsubMessage; import com.google.pubsub.v1.Topic; +import io.grpc.ManagedChannel; import io.grpc.Status; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. @@ -100,202 +101,74 @@ public class PublisherSettings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); - private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; - - static { - ImmutableMap.Builder> definitions = ImmutableMap.builder(); - definitions.put( - "idempotent", - Sets.immutableEnumSet( - Lists.newArrayList( - Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); - definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); - RETRYABLE_CODE_DEFINITIONS = definitions.build(); - } + private final SimpleCallSettings createTopicSettings; + private final BundlingCallSettings publishSettings; + private final SimpleCallSettings getTopicSettings; + private final PageStreamingCallSettings + listTopicsSettings; - private static final ImmutableMap RETRY_PARAM_DEFINITIONS; - - static { - ImmutableMap.Builder definitions = ImmutableMap.builder(); - RetryParams params = null; - params = - RetryParams.newBuilder() - .setRetryBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(100L)) - .setDelayMultiplier(1.2) - .setMaxDelay(Duration.millis(1000L)) - .build()) - .setTimeoutBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(2000L)) - .setDelayMultiplier(1.5) - .setMaxDelay(Duration.millis(30000L)) - .build()) - .setTotalTimeout(Duration.millis(45000L)) - .build(); - definitions.put("default", params); - RETRY_PARAM_DEFINITIONS = definitions.build(); - } + private final PageStreamingCallSettings< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings; - private final MethodBuilders methods; + private final SimpleCallSettings deleteTopicSettings; - private static class MethodBuilders { - private final ApiCallable.Builder createTopicMethod; - private final ApiCallable.BundlableBuilder publishMethod; - private final ApiCallable.Builder getTopicMethod; - private final ApiCallable.PageStreamingBuilder - listTopicsMethod; - private final ApiCallable.PageStreamingBuilder< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - listTopicSubscriptionsMethod; - private final ApiCallable.Builder deleteTopicMethod; - private final ImmutableList allMethods; - - public MethodBuilders() { - createTopicMethod = new Builder<>(PublisherGrpc.METHOD_CREATE_TOPIC); - createTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - createTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + public SimpleCallSettings createTopicSettings() { + return createTopicSettings; + } - BundlingSettings publishBundlingSettings = - BundlingSettings.newBuilder() - .setElementCountThreshold(800) - .setElementCountLimit(1000) - .setRequestByteThreshold(8388608) - .setRequestByteLimit(10485760) - .setDelayThreshold(Duration.millis(100)) - .setBlockingCallCountThreshold(1) - .build(); - publishMethod = - new BundlableBuilder<>( - PublisherGrpc.METHOD_PUBLISH, PUBLISH_BUNDLING_DESC, publishBundlingSettings); - publishMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - publishMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - getTopicMethod = new Builder<>(PublisherGrpc.METHOD_GET_TOPIC); - getTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - getTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicsMethod = - new PageStreamingBuilder<>(PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC); - listTopicsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listTopicsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicSubscriptionsMethod = - new PageStreamingBuilder<>( - PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, - LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); - listTopicSubscriptionsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listTopicSubscriptionsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - deleteTopicMethod = new Builder<>(PublisherGrpc.METHOD_DELETE_TOPIC); - deleteTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - deleteTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createTopicMethod, - publishMethod, - getTopicMethod, - listTopicsMethod, - listTopicSubscriptionsMethod, - deleteTopicMethod) - .build(); - } + public BundlingCallSettings publishSettings() { + return publishSettings; } - /** - * Constructs an instance of PublisherSettings with default settings. - * - * - * - */ - public static PublisherSettings create() { - PublisherSettings settings = new PublisherSettings(new MethodBuilders()); - settings.provideChannelWith( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); - return settings; + public SimpleCallSettings getTopicSettings() { + return getTopicSettings; } - /** - * Constructs an instance of PublisherSettings with default settings. This is protected - * so that it easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - * - * - * - */ - protected PublisherSettings(MethodBuilders methods) { - super(methods.allMethods); - this.methods = methods; + public PageStreamingCallSettings + listTopicsSettings() { + return listTopicsSettings; } - /** - * Returns the builder for the API method createTopic. - * - * - * - */ - public final ApiCallable.Builder createTopicMethod() { - return methods.createTopicMethod; + public PageStreamingCallSettings< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings() { + return listTopicSubscriptionsSettings; } - /** - * Returns the builder for the API method publish. - * - * - * - */ - public final ApiCallable.BundlableBuilder publishMethod() { - return methods.publishMethod; + public SimpleCallSettings deleteTopicSettings() { + return deleteTopicSettings; } - /** - * Returns the builder for the API method getTopic. - * - * - * - */ - public final ApiCallable.Builder getTopicMethod() { - return methods.getTopicMethod; + public static PublisherSettings defaultInstance() throws IOException { + return newBuilder().build(); } - /** - * Returns the builder for the API method listTopics. - * - * - * - */ - public final ApiCallable.PageStreamingBuilder - listTopicsMethod() { - return methods.listTopicsMethod; + public static Builder newBuilder() { + return new Builder(); } - /** - * Returns the builder for the API method listTopicSubscriptions. - * - * - * - */ - public final ApiCallable.PageStreamingBuilder< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - listTopicSubscriptionsMethod() { - return methods.listTopicSubscriptionsMethod; + public Builder toBuilder() { + return new Builder(this); } - /** - * Returns the builder for the API method deleteTopic. - * - * - * - */ - public final ApiCallable.Builder deleteTopicMethod() { - return methods.deleteTopicMethod; + private PublisherSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getOrBuildChannel(), + settingsBuilder.shouldAutoCloseChannel(), + settingsBuilder.getOrBuildExecutor(), + settingsBuilder.getConnectionSettings(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + createTopicSettings = settingsBuilder.createTopicSettings().build(); + publishSettings = settingsBuilder.publishSettings().build(); + getTopicSettings = settingsBuilder.getTopicSettings().build(); + listTopicsSettings = settingsBuilder.listTopicsSettings().build(); + listTopicSubscriptionsSettings = settingsBuilder.listTopicSubscriptionsSettings().build(); + deleteTopicSettings = settingsBuilder.deleteTopicSettings().build(); } private static PageStreamingDescriptor @@ -412,4 +285,196 @@ public long countBytes(PublishRequest request) { return request.getSerializedSize(); } }; + + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder createTopicSettings; + private BundlingCallSettings.Builder publishSettings; + private SimpleCallSettings.Builder getTopicSettings; + private PageStreamingCallSettings.Builder + listTopicsSettings; + private PageStreamingCallSettings.Builder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings; + private SimpleCallSettings.Builder deleteTopicSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.2) + .setMaxRetryDelay(Duration.millis(1000L)) + .setInitialRpcTimeout(Duration.millis(2000L)) + .setRpcTimeoutMultiplier(1.5) + .setMaxRpcTimeout(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(45000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + + createTopicSettings = + SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_CREATE_TOPIC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + BundlingSettings.Builder publishBundlingSettingsBuilder = + BundlingSettings.newBuilder() + .setElementCountThreshold(800) + .setElementCountLimit(1000) + .setRequestByteThreshold(8388608) + .setRequestByteLimit(10485760) + .setDelayThreshold(Duration.millis(100)) + .setBlockingCallCountThreshold(1); + publishSettings = + BundlingCallSettings.newBuilder(PublisherGrpc.METHOD_PUBLISH, PUBLISH_BUNDLING_DESC) + .setBundlingSettingsBuilder(publishBundlingSettingsBuilder) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + getTopicSettings = + SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_GET_TOPIC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicsSettings = + PageStreamingCallSettings.newBuilder( + PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicSubscriptionsSettings = + PageStreamingCallSettings.newBuilder( + PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, + LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + deleteTopicSettings = + SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_DELETE_TOPIC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + methodSettingsBuilders = + ImmutableList.of( + createTopicSettings, + publishSettings, + getTopicSettings, + listTopicsSettings, + listTopicSubscriptionsSettings, + deleteTopicSettings); + } + + private Builder(PublisherSettings settings) { + super(settings); + + createTopicSettings = settings.createTopicSettings.toBuilder(); + publishSettings = settings.publishSettings.toBuilder(); + getTopicSettings = settings.getTopicSettings.toBuilder(); + listTopicsSettings = settings.listTopicsSettings.toBuilder(); + listTopicSubscriptionsSettings = settings.listTopicSubscriptionsSettings.toBuilder(); + deleteTopicSettings = settings.deleteTopicSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + createTopicSettings, + publishSettings, + getTopicSettings, + listTopicsSettings, + listTopicSubscriptionsSettings, + deleteTopicSettings); + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder setExecutor(ScheduledExecutorService executor) { + super.setExecutor(executor); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + public SimpleCallSettings.Builder createTopicSettings() { + return createTopicSettings; + } + + public BundlingCallSettings.Builder publishSettings() { + return publishSettings; + } + + public SimpleCallSettings.Builder getTopicSettings() { + return getTopicSettings; + } + + public PageStreamingCallSettings.Builder + listTopicsSettings() { + return listTopicsSettings; + } + + public PageStreamingCallSettings.Builder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings() { + return listTopicSubscriptionsSettings; + } + + public SimpleCallSettings.Builder deleteTopicSettings() { + return deleteTopicSettings; + } + + @Override + public PublisherSettings build() throws IOException { + return new PublisherSettings(this); + } + } } diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java index 3040af41efd8..17c1dfdf0c37 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java @@ -80,23 +80,9 @@ public class SubscriberApi implements AutoCloseable { private final ApiCallable pullCallable; private final ApiCallable modifyPushConfigCallable; - /** - * A PathTemplate representing the fully-qualified path to represent - * a project resource. - * - * - * - */ private static final PathTemplate PROJECT_PATH_TEMPLATE = PathTemplate.create("projects/{project}"); - /** - * A PathTemplate representing the fully-qualified path to represent - * a subscription resource. - * - * - * - */ private static final PathTemplate SUBSCRIPTION_PATH_TEMPLATE = PathTemplate.create("projects/{project}/subscriptions/{subscription}"); @@ -161,8 +147,8 @@ public static final String parseSubscriptionFromSubscriptionName(String subscrip * * */ - public static final SubscriberApi create() throws IOException { - return create(SubscriberSettings.create()); + public static final SubscriberApi defaultInstance() throws IOException { + return create(SubscriberSettings.defaultInstance()); } /** @@ -188,16 +174,21 @@ public static final SubscriberApi create(SubscriberSettings settings) throws IOE protected SubscriberApi(SubscriberSettings settings) throws IOException { this.channel = settings.getChannel(); - this.createSubscriptionCallable = settings.createSubscriptionMethod().build(settings); - this.getSubscriptionCallable = settings.getSubscriptionMethod().build(settings); - this.listSubscriptionsCallable = settings.listSubscriptionsMethod().build(settings); + this.createSubscriptionCallable = + ApiCallable.create(settings.createSubscriptionSettings(), settings); + this.getSubscriptionCallable = ApiCallable.create(settings.getSubscriptionSettings(), settings); + this.listSubscriptionsCallable = + ApiCallable.create(settings.listSubscriptionsSettings(), settings); this.listSubscriptionsIterableCallable = - settings.listSubscriptionsMethod().buildPageStreaming(settings); - this.deleteSubscriptionCallable = settings.deleteSubscriptionMethod().build(settings); - this.modifyAckDeadlineCallable = settings.modifyAckDeadlineMethod().build(settings); - this.acknowledgeCallable = settings.acknowledgeMethod().build(settings); - this.pullCallable = settings.pullMethod().build(settings); - this.modifyPushConfigCallable = settings.modifyPushConfigMethod().build(settings); + ApiCallable.createIterable(settings.listSubscriptionsSettings(), settings); + this.deleteSubscriptionCallable = + ApiCallable.create(settings.deleteSubscriptionSettings(), settings); + this.modifyAckDeadlineCallable = + ApiCallable.create(settings.modifyAckDeadlineSettings(), settings); + this.acknowledgeCallable = ApiCallable.create(settings.acknowledgeSettings(), settings); + this.pullCallable = ApiCallable.create(settings.pullSettings(), settings); + this.modifyPushConfigCallable = + ApiCallable.create(settings.modifyPushConfigSettings(), settings); if (settings.shouldAutoCloseChannel()) { closeables.add( @@ -252,6 +243,7 @@ public void close() throws IOException { * system will eventually redeliver the message. * * If this parameter is not set, the default value of 10 seconds is used. + * @throws ApiException if the remote call fails */ public final Subscription createSubscription( String name, String topic, PushConfig pushConfig, int ackDeadlineSeconds) { @@ -279,6 +271,7 @@ public final Subscription createSubscription( * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public Subscription createSubscription(Subscription request) { return createSubscriptionCallable().call(request); @@ -295,6 +288,7 @@ public Subscription createSubscription(Subscription request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable createSubscriptionCallable() { return createSubscriptionCallable; @@ -313,6 +307,7 @@ public final ApiCallable createSubscriptionCallable( * * * @param subscription The name of the subscription to get. + * @throws ApiException if the remote call fails */ public final Subscription getSubscription(String subscription) { GetSubscriptionRequest request = @@ -332,6 +327,7 @@ public final Subscription getSubscription(String subscription) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private Subscription getSubscription(GetSubscriptionRequest request) { return getSubscriptionCallable().call(request); @@ -346,6 +342,7 @@ private Subscription getSubscription(GetSubscriptionRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable getSubscriptionCallable() { return getSubscriptionCallable; @@ -362,6 +359,9 @@ public final ApiCallable getSubscriptionCa * * * + * + * @param project The name of the cloud project that subscriptions belong to. + * @throws ApiException if the remote call fails */ public final Iterable listSubscriptions(String project) { ListSubscriptionsRequest request = @@ -380,6 +380,7 @@ public final Iterable listSubscriptions(String project) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public final Iterable listSubscriptions(ListSubscriptionsRequest request) { return listSubscriptionsIterableCallable().call(request); @@ -394,6 +395,7 @@ public final Iterable listSubscriptions(ListSubscriptionsRequest r * * * + * @throws ApiException if the remote call fails */ public final ApiCallable> listSubscriptionsIterableCallable() { @@ -409,6 +411,7 @@ public final Iterable listSubscriptions(ListSubscriptionsRequest r * * * + * @throws ApiException if the remote call fails */ public final ApiCallable listSubscriptionsCallable() { @@ -429,6 +432,7 @@ public final Iterable listSubscriptions(ListSubscriptionsRequest r * * * @param subscription The subscription to delete. + * @throws ApiException if the remote call fails */ public final void deleteSubscription(String subscription) { DeleteSubscriptionRequest request = @@ -449,6 +453,7 @@ public final void deleteSubscription(String subscription) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private void deleteSubscription(DeleteSubscriptionRequest request) { deleteSubscriptionCallable().call(request); @@ -464,6 +469,7 @@ private void deleteSubscription(DeleteSubscriptionRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable deleteSubscriptionCallable() { return deleteSubscriptionCallable; @@ -484,10 +490,11 @@ public final ApiCallable deleteSubscriptionCal * @param subscription The name of the subscription. * @param ackIds List of acknowledgment IDs. * @param ackDeadlineSeconds The new ack deadline with respect to the time this request was sent to - * the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new + * the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new * ack deadline will expire 10 seconds after the `ModifyAckDeadline` call * was made. Specifying zero may immediately make the message available for * another pull request. + * @throws ApiException if the remote call fails */ public final void modifyAckDeadline( String subscription, List ackIds, int ackDeadlineSeconds) { @@ -512,6 +519,7 @@ public final void modifyAckDeadline( * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public void modifyAckDeadline(ModifyAckDeadlineRequest request) { modifyAckDeadlineCallable().call(request); @@ -526,6 +534,7 @@ public void modifyAckDeadline(ModifyAckDeadlineRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable modifyAckDeadlineCallable() { return modifyAckDeadlineCallable; @@ -549,6 +558,7 @@ public final ApiCallable modifyAckDeadlineCalla * @param subscription The subscription whose message is being acknowledged. * @param ackIds The acknowledgment ID for the messages being acknowledged that was returned * by the Pub/Sub system in the `Pull` response. Must not be empty. + * @throws ApiException if the remote call fails */ public final void acknowledge(String subscription, List ackIds) { AcknowledgeRequest request = @@ -571,6 +581,7 @@ public final void acknowledge(String subscription, List ackIds) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public void acknowledge(AcknowledgeRequest request) { acknowledgeCallable().call(request); @@ -588,6 +599,7 @@ public void acknowledge(AcknowledgeRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable acknowledgeCallable() { return acknowledgeCallable; @@ -612,6 +624,7 @@ public final ApiCallable acknowledgeCallable() { * than returning no messages. * @param maxMessages The maximum number of messages returned for this request. The Pub/Sub * system may return fewer than the number specified. + * @throws ApiException if the remote call fails */ public final PullResponse pull(String subscription, boolean returnImmediately, int maxMessages) { PullRequest request = @@ -635,6 +648,7 @@ public final PullResponse pull(String subscription, boolean returnImmediately, i * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public PullResponse pull(PullRequest request) { return pullCallable().call(request); @@ -649,6 +663,7 @@ public PullResponse pull(PullRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable pullCallable() { return pullCallable; @@ -675,6 +690,7 @@ public final ApiCallable pullCallable() { * stop pushing messages from the given subscription and allow * messages to be pulled and acknowledged - effectively pausing * the subscription if `Pull` is not called. + * @throws ApiException if the remote call fails */ public final void modifyPushConfig(String subscription, PushConfig pushConfig) { ModifyPushConfigRequest request = @@ -699,6 +715,7 @@ public final void modifyPushConfig(String subscription, PushConfig pushConfig) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public void modifyPushConfig(ModifyPushConfigRequest request) { modifyPushConfigCallable().call(request); @@ -715,6 +732,7 @@ public void modifyPushConfig(ModifyPushConfigRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable modifyPushConfigCallable() { return modifyPushConfigCallable; diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java index e0204a4171f8..918ec77e9f04 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java @@ -33,15 +33,13 @@ package com.google.gcloud.pubsub.spi.v1; -import com.google.api.gax.core.BackoffParams; import com.google.api.gax.core.ConnectionSettings; -import com.google.api.gax.core.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.api.gax.grpc.ApiCallSettings; -import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.ApiCallable.Builder; -import com.google.api.gax.grpc.ApiCallable.PageStreamingBuilder; +import com.google.api.gax.grpc.PageStreamingCallSettings; import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -59,7 +57,10 @@ import com.google.pubsub.v1.PullResponse; import com.google.pubsub.v1.SubscriberGrpc; import com.google.pubsub.v1.Subscription; +import io.grpc.ManagedChannel; import io.grpc.Status; +import java.io.IOException; +import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. @@ -93,219 +94,83 @@ public class SubscriberSettings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); - private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; - - static { - ImmutableMap.Builder> definitions = ImmutableMap.builder(); - definitions.put( - "idempotent", - Sets.immutableEnumSet( - Lists.newArrayList( - Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); - definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); - RETRYABLE_CODE_DEFINITIONS = definitions.build(); - } + private final SimpleCallSettings createSubscriptionSettings; + private final SimpleCallSettings getSubscriptionSettings; + private final PageStreamingCallSettings< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings; - private static final ImmutableMap RETRY_PARAM_DEFINITIONS; - - static { - ImmutableMap.Builder definitions = ImmutableMap.builder(); - RetryParams params = null; - params = - RetryParams.newBuilder() - .setRetryBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(100L)) - .setDelayMultiplier(1.2) - .setMaxDelay(Duration.millis(1000L)) - .build()) - .setTimeoutBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(2000L)) - .setDelayMultiplier(1.5) - .setMaxDelay(Duration.millis(30000L)) - .build()) - .setTotalTimeout(Duration.millis(45000L)) - .build(); - definitions.put("default", params); - RETRY_PARAM_DEFINITIONS = definitions.build(); - } + private final SimpleCallSettings deleteSubscriptionSettings; + private final SimpleCallSettings modifyAckDeadlineSettings; + private final SimpleCallSettings acknowledgeSettings; + private final SimpleCallSettings pullSettings; + private final SimpleCallSettings modifyPushConfigSettings; - private final MethodBuilders methods; + public SimpleCallSettings createSubscriptionSettings() { + return createSubscriptionSettings; + } - private static class MethodBuilders { - private final ApiCallable.Builder createSubscriptionMethod; - private final ApiCallable.Builder getSubscriptionMethod; - private final ApiCallable.PageStreamingBuilder< - ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> - listSubscriptionsMethod; - private final ApiCallable.Builder deleteSubscriptionMethod; - private final ApiCallable.Builder modifyAckDeadlineMethod; - private final ApiCallable.Builder acknowledgeMethod; - private final ApiCallable.Builder pullMethod; - private final ApiCallable.Builder modifyPushConfigMethod; - private final ImmutableList allMethods; - - public MethodBuilders() { - createSubscriptionMethod = new Builder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); - createSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - createSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - getSubscriptionMethod = new Builder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); - getSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - getSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - listSubscriptionsMethod = - new PageStreamingBuilder<>( - SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC); - listSubscriptionsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listSubscriptionsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - deleteSubscriptionMethod = new Builder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); - deleteSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - deleteSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - modifyAckDeadlineMethod = new Builder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); - modifyAckDeadlineMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - modifyAckDeadlineMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - acknowledgeMethod = new Builder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); - acknowledgeMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - acknowledgeMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - pullMethod = new Builder<>(SubscriberGrpc.METHOD_PULL); - pullMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - pullMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - modifyPushConfigMethod = new Builder<>(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); - modifyPushConfigMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - modifyPushConfigMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createSubscriptionMethod, - getSubscriptionMethod, - listSubscriptionsMethod, - deleteSubscriptionMethod, - modifyAckDeadlineMethod, - acknowledgeMethod, - pullMethod, - modifyPushConfigMethod) - .build(); - } + public SimpleCallSettings getSubscriptionSettings() { + return getSubscriptionSettings; } - /** - * Constructs an instance of SubscriberSettings with default settings. - * - * - * - */ - public static SubscriberSettings create() { - SubscriberSettings settings = new SubscriberSettings(new MethodBuilders()); - settings.provideChannelWith( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); - return settings; + public PageStreamingCallSettings< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings() { + return listSubscriptionsSettings; } - /** - * Constructs an instance of SubscriberSettings with default settings. This is protected - * so that it easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - * - * - * - */ - protected SubscriberSettings(MethodBuilders methods) { - super(methods.allMethods); - this.methods = methods; + public SimpleCallSettings deleteSubscriptionSettings() { + return deleteSubscriptionSettings; } - /** - * Returns the builder for the API method createSubscription. - * - * - * - */ - public final ApiCallable.Builder createSubscriptionMethod() { - return methods.createSubscriptionMethod; + public SimpleCallSettings modifyAckDeadlineSettings() { + return modifyAckDeadlineSettings; } - /** - * Returns the builder for the API method getSubscription. - * - * - * - */ - public final ApiCallable.Builder getSubscriptionMethod() { - return methods.getSubscriptionMethod; + public SimpleCallSettings acknowledgeSettings() { + return acknowledgeSettings; } - /** - * Returns the builder for the API method listSubscriptions. - * - * - * - */ - public final ApiCallable.PageStreamingBuilder< - ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> - listSubscriptionsMethod() { - return methods.listSubscriptionsMethod; + public SimpleCallSettings pullSettings() { + return pullSettings; } - /** - * Returns the builder for the API method deleteSubscription. - * - * - * - */ - public final ApiCallable.Builder deleteSubscriptionMethod() { - return methods.deleteSubscriptionMethod; + public SimpleCallSettings modifyPushConfigSettings() { + return modifyPushConfigSettings; } - /** - * Returns the builder for the API method modifyAckDeadline. - * - * - * - */ - public final ApiCallable.Builder modifyAckDeadlineMethod() { - return methods.modifyAckDeadlineMethod; + public static SubscriberSettings defaultInstance() throws IOException { + return newBuilder().build(); } - /** - * Returns the builder for the API method acknowledge. - * - * - * - */ - public final ApiCallable.Builder acknowledgeMethod() { - return methods.acknowledgeMethod; + public static Builder newBuilder() { + return new Builder(); } - /** - * Returns the builder for the API method pull. - * - * - * - */ - public final ApiCallable.Builder pullMethod() { - return methods.pullMethod; + public Builder toBuilder() { + return new Builder(this); } - /** - * Returns the builder for the API method modifyPushConfig. - * - * - * - */ - public final ApiCallable.Builder modifyPushConfigMethod() { - return methods.modifyPushConfigMethod; + private SubscriberSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getOrBuildChannel(), + settingsBuilder.shouldAutoCloseChannel(), + settingsBuilder.getOrBuildExecutor(), + settingsBuilder.getConnectionSettings(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + createSubscriptionSettings = settingsBuilder.createSubscriptionSettings().build(); + getSubscriptionSettings = settingsBuilder.getSubscriptionSettings().build(); + listSubscriptionsSettings = settingsBuilder.listSubscriptionsSettings().build(); + deleteSubscriptionSettings = settingsBuilder.deleteSubscriptionSettings().build(); + modifyAckDeadlineSettings = settingsBuilder.modifyAckDeadlineSettings().build(); + acknowledgeSettings = settingsBuilder.acknowledgeSettings().build(); + pullSettings = settingsBuilder.pullSettings().build(); + modifyPushConfigSettings = settingsBuilder.modifyPushConfigSettings().build(); } private static PageStreamingDescriptor< @@ -336,4 +201,212 @@ public Iterable extractResources(ListSubscriptionsResponse payload return payload.getSubscriptionsList(); } }; + + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder createSubscriptionSettings; + private SimpleCallSettings.Builder + getSubscriptionSettings; + private PageStreamingCallSettings.Builder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings; + private SimpleCallSettings.Builder deleteSubscriptionSettings; + private SimpleCallSettings.Builder modifyAckDeadlineSettings; + private SimpleCallSettings.Builder acknowledgeSettings; + private SimpleCallSettings.Builder pullSettings; + private SimpleCallSettings.Builder modifyPushConfigSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.2) + .setMaxRetryDelay(Duration.millis(1000L)) + .setInitialRpcTimeout(Duration.millis(2000L)) + .setRpcTimeoutMultiplier(1.5) + .setMaxRpcTimeout(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(45000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + + createSubscriptionSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + getSubscriptionSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_GET_SUBSCRIPTION) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + listSubscriptionsSettings = + PageStreamingCallSettings.newBuilder( + SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + deleteSubscriptionSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyAckDeadlineSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + acknowledgeSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_ACKNOWLEDGE) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + pullSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_PULL) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyPushConfigSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + methodSettingsBuilders = + ImmutableList.of( + createSubscriptionSettings, + getSubscriptionSettings, + listSubscriptionsSettings, + deleteSubscriptionSettings, + modifyAckDeadlineSettings, + acknowledgeSettings, + pullSettings, + modifyPushConfigSettings); + } + + private Builder(SubscriberSettings settings) { + super(settings); + + createSubscriptionSettings = settings.createSubscriptionSettings.toBuilder(); + getSubscriptionSettings = settings.getSubscriptionSettings.toBuilder(); + listSubscriptionsSettings = settings.listSubscriptionsSettings.toBuilder(); + deleteSubscriptionSettings = settings.deleteSubscriptionSettings.toBuilder(); + modifyAckDeadlineSettings = settings.modifyAckDeadlineSettings.toBuilder(); + acknowledgeSettings = settings.acknowledgeSettings.toBuilder(); + pullSettings = settings.pullSettings.toBuilder(); + modifyPushConfigSettings = settings.modifyPushConfigSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + createSubscriptionSettings, + getSubscriptionSettings, + listSubscriptionsSettings, + deleteSubscriptionSettings, + modifyAckDeadlineSettings, + acknowledgeSettings, + pullSettings, + modifyPushConfigSettings); + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder setExecutor(ScheduledExecutorService executor) { + super.setExecutor(executor); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + public SimpleCallSettings.Builder createSubscriptionSettings() { + return createSubscriptionSettings; + } + + public SimpleCallSettings.Builder + getSubscriptionSettings() { + return getSubscriptionSettings; + } + + public PageStreamingCallSettings.Builder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings() { + return listSubscriptionsSettings; + } + + public SimpleCallSettings.Builder + deleteSubscriptionSettings() { + return deleteSubscriptionSettings; + } + + public SimpleCallSettings.Builder modifyAckDeadlineSettings() { + return modifyAckDeadlineSettings; + } + + public SimpleCallSettings.Builder acknowledgeSettings() { + return acknowledgeSettings; + } + + public SimpleCallSettings.Builder pullSettings() { + return pullSettings; + } + + public SimpleCallSettings.Builder modifyPushConfigSettings() { + return modifyPushConfigSettings; + } + + @Override + public SubscriberSettings build() throws IOException { + return new SubscriberSettings(this); + } + } } diff --git a/gcloud-java-pubsub/pom.xml b/gcloud-java-pubsub/pom.xml index 18738ac989cd..16a82879b9f4 100644 --- a/gcloud-java-pubsub/pom.xml +++ b/gcloud-java-pubsub/pom.xml @@ -21,7 +21,7 @@ com.google.api gax - 0.0.6 + 0.0.9 com.google.api.grpc diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java index 8394e5ae74ed..1d8c00c4e910 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherApi.java @@ -34,8 +34,6 @@ package com.google.gcloud.pubsub.spi.v1; import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.ApiCallable.BundlableApiCallableInfo; -import com.google.api.gax.grpc.BundlerFactory; import com.google.api.gax.protobuf.PathTemplate; import com.google.protobuf.Empty; import com.google.pubsub.v1.DeleteTopicRequest; @@ -80,23 +78,9 @@ public class PublisherApi implements AutoCloseable { listTopicSubscriptionsIterableCallable; private final ApiCallable deleteTopicCallable; - /** - * A PathTemplate representing the fully-qualified path to represent - * a project resource. - * - * - * - */ private static final PathTemplate PROJECT_PATH_TEMPLATE = PathTemplate.create("projects/{project}"); - /** - * A PathTemplate representing the fully-qualified path to represent - * a topic resource. - * - * - * - */ private static final PathTemplate TOPIC_PATH_TEMPLATE = PathTemplate.create("projects/{project}/topics/{topic}"); @@ -161,8 +145,8 @@ public static final String parseTopicFromTopicName(String topicName) { * * */ - public static final PublisherApi create() throws IOException { - return create(PublisherSettings.create()); + public static final PublisherApi defaultInstance() throws IOException { + return create(PublisherSettings.defaultInstance()); } /** @@ -188,22 +172,20 @@ public static final PublisherApi create(PublisherSettings settings) throws IOExc protected PublisherApi(PublisherSettings settings) throws IOException { this.channel = settings.getChannel(); - this.createTopicCallable = settings.createTopicMethod().build(settings); - BundlableApiCallableInfo bundlablePublish = - settings.publishMethod().buildBundlable(settings); - this.publishCallable = bundlablePublish.getApiCallable(); - BundlerFactory publishBundlerFactory = - bundlablePublish.getBundlerFactory(); - if (publishBundlerFactory != null) { - this.closeables.add(publishBundlerFactory); + this.createTopicCallable = ApiCallable.create(settings.createTopicSettings(), settings); + this.publishCallable = ApiCallable.create(settings.publishSettings(), settings); + if (settings.publishSettings().getBundlerFactory() != null) { + closeables.add(settings.publishSettings().getBundlerFactory()); } - this.getTopicCallable = settings.getTopicMethod().build(settings); - this.listTopicsCallable = settings.listTopicsMethod().build(settings); - this.listTopicsIterableCallable = settings.listTopicsMethod().buildPageStreaming(settings); - this.listTopicSubscriptionsCallable = settings.listTopicSubscriptionsMethod().build(settings); + this.getTopicCallable = ApiCallable.create(settings.getTopicSettings(), settings); + this.listTopicsCallable = ApiCallable.create(settings.listTopicsSettings(), settings); + this.listTopicsIterableCallable = + ApiCallable.createIterable(settings.listTopicsSettings(), settings); + this.listTopicSubscriptionsCallable = + ApiCallable.create(settings.listTopicSubscriptionsSettings(), settings); this.listTopicSubscriptionsIterableCallable = - settings.listTopicSubscriptionsMethod().buildPageStreaming(settings); - this.deleteTopicCallable = settings.deleteTopicMethod().build(settings); + ApiCallable.createIterable(settings.listTopicSubscriptionsSettings(), settings); + this.deleteTopicCallable = ApiCallable.create(settings.deleteTopicSettings(), settings); if (settings.shouldAutoCloseChannel()) { closeables.add( @@ -231,6 +213,7 @@ public void close() throws IOException { * underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent * signs (`%`). It must be between 3 and 255 characters in length, and it * must not start with `"goog"`. + * @throws ApiException if the remote call fails */ public final Topic createTopic(String name) { Topic request = Topic.newBuilder().setName(name).build(); @@ -246,6 +229,7 @@ public final Topic createTopic(String name) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private Topic createTopic(Topic request) { return createTopicCallable().call(request); @@ -257,6 +241,7 @@ private Topic createTopic(Topic request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable createTopicCallable() { return createTopicCallable; @@ -275,6 +260,7 @@ public final ApiCallable createTopicCallable() { * * @param topic The messages in the request will be published on this topic. * @param messages The messages to publish. + * @throws ApiException if the remote call fails */ public final PublishResponse publish(String topic, List messages) { PublishRequest request = @@ -293,6 +279,7 @@ public final PublishResponse publish(String topic, List messages) * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public PublishResponse publish(PublishRequest request) { return publishCallable().call(request); @@ -306,6 +293,7 @@ public PublishResponse publish(PublishRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable publishCallable() { return publishCallable; @@ -321,6 +309,7 @@ public final ApiCallable publishCallable() { * * * @param topic The name of the topic to get. + * @throws ApiException if the remote call fails */ public final Topic getTopic(String topic) { GetTopicRequest request = GetTopicRequest.newBuilder().setTopic(topic).build(); @@ -336,6 +325,7 @@ public final Topic getTopic(String topic) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private Topic getTopic(GetTopicRequest request) { return getTopicCallable().call(request); @@ -347,6 +337,7 @@ private Topic getTopic(GetTopicRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable getTopicCallable() { return getTopicCallable; @@ -360,6 +351,9 @@ public final ApiCallable getTopicCallable() { * * * + * + * @param project The name of the cloud project that topics belong to. + * @throws ApiException if the remote call fails */ public final Iterable listTopics(String project) { ListTopicsRequest request = ListTopicsRequest.newBuilder().setProject(project).build(); @@ -374,6 +368,7 @@ public final Iterable listTopics(String project) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public final Iterable listTopics(ListTopicsRequest request) { return listTopicsIterableCallable().call(request); @@ -385,6 +380,7 @@ public final Iterable listTopics(ListTopicsRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable> listTopicsIterableCallable() { return listTopicsIterableCallable; @@ -396,6 +392,7 @@ public final ApiCallable> listTopicsIterableC * * * + * @throws ApiException if the remote call fails */ public final ApiCallable listTopicsCallable() { return listTopicsCallable; @@ -409,6 +406,9 @@ public final ApiCallable listTopicsCallab * * * + * + * @param topic The name of the topic that subscriptions are attached to. + * @throws ApiException if the remote call fails */ public final Iterable listTopicSubscriptions(String topic) { ListTopicSubscriptionsRequest request = @@ -424,6 +424,7 @@ public final Iterable listTopicSubscriptions(String topic) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsRequest request) { return listTopicSubscriptionsIterableCallable().call(request); @@ -435,6 +436,7 @@ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsReque * * * + * @throws ApiException if the remote call fails */ public final ApiCallable> listTopicSubscriptionsIterableCallable() { @@ -447,6 +449,7 @@ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsReque * * * + * @throws ApiException if the remote call fails */ public final ApiCallable listTopicSubscriptionsCallable() { @@ -467,6 +470,7 @@ public final Iterable listTopicSubscriptions(ListTopicSubscriptionsReque * * * @param topic Name of the topic to delete. + * @throws ApiException if the remote call fails */ public final void deleteTopic(String topic) { DeleteTopicRequest request = DeleteTopicRequest.newBuilder().setTopic(topic).build(); @@ -486,6 +490,7 @@ public final void deleteTopic(String topic) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private void deleteTopic(DeleteTopicRequest request) { deleteTopicCallable().call(request); @@ -501,6 +506,7 @@ private void deleteTopic(DeleteTopicRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable deleteTopicCallable() { return deleteTopicCallable; diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java index 8b3d434b8e49..11c709024144 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/PublisherSettings.java @@ -33,19 +33,17 @@ package com.google.gcloud.pubsub.spi.v1; -import com.google.api.gax.core.BackoffParams; import com.google.api.gax.core.ConnectionSettings; -import com.google.api.gax.core.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.api.gax.grpc.ApiCallSettings; -import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.ApiCallable.Builder; -import com.google.api.gax.grpc.ApiCallable.BundlableBuilder; -import com.google.api.gax.grpc.ApiCallable.PageStreamingBuilder; +import com.google.api.gax.grpc.BundlingCallSettings; import com.google.api.gax.grpc.BundlingDescriptor; import com.google.api.gax.grpc.BundlingSettings; +import com.google.api.gax.grpc.PageStreamingCallSettings; import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.RequestIssuer; import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -63,10 +61,13 @@ import com.google.pubsub.v1.PublisherGrpc; import com.google.pubsub.v1.PubsubMessage; import com.google.pubsub.v1.Topic; +import io.grpc.ManagedChannel; import io.grpc.Status; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. @@ -100,202 +101,74 @@ public class PublisherSettings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); - private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; - - static { - ImmutableMap.Builder> definitions = ImmutableMap.builder(); - definitions.put( - "idempotent", - Sets.immutableEnumSet( - Lists.newArrayList( - Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); - definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); - RETRYABLE_CODE_DEFINITIONS = definitions.build(); - } + private final SimpleCallSettings createTopicSettings; + private final BundlingCallSettings publishSettings; + private final SimpleCallSettings getTopicSettings; + private final PageStreamingCallSettings + listTopicsSettings; - private static final ImmutableMap RETRY_PARAM_DEFINITIONS; - - static { - ImmutableMap.Builder definitions = ImmutableMap.builder(); - RetryParams params = null; - params = - RetryParams.newBuilder() - .setRetryBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(100L)) - .setDelayMultiplier(1.2) - .setMaxDelay(Duration.millis(1000L)) - .build()) - .setTimeoutBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(2000L)) - .setDelayMultiplier(1.5) - .setMaxDelay(Duration.millis(30000L)) - .build()) - .setTotalTimeout(Duration.millis(45000L)) - .build(); - definitions.put("default", params); - RETRY_PARAM_DEFINITIONS = definitions.build(); - } + private final PageStreamingCallSettings< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings; - private final MethodBuilders methods; + private final SimpleCallSettings deleteTopicSettings; - private static class MethodBuilders { - private final ApiCallable.Builder createTopicMethod; - private final ApiCallable.BundlableBuilder publishMethod; - private final ApiCallable.Builder getTopicMethod; - private final ApiCallable.PageStreamingBuilder - listTopicsMethod; - private final ApiCallable.PageStreamingBuilder< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - listTopicSubscriptionsMethod; - private final ApiCallable.Builder deleteTopicMethod; - private final ImmutableList allMethods; - - public MethodBuilders() { - createTopicMethod = new Builder<>(PublisherGrpc.METHOD_CREATE_TOPIC); - createTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - createTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + public SimpleCallSettings createTopicSettings() { + return createTopicSettings; + } - BundlingSettings publishBundlingSettings = - BundlingSettings.newBuilder() - .setElementCountThreshold(800) - .setElementCountLimit(1000) - .setRequestByteThreshold(8388608) - .setRequestByteLimit(10485760) - .setDelayThreshold(Duration.millis(100)) - .setBlockingCallCountThreshold(1) - .build(); - publishMethod = - new BundlableBuilder<>( - PublisherGrpc.METHOD_PUBLISH, PUBLISH_BUNDLING_DESC, publishBundlingSettings); - publishMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - publishMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - getTopicMethod = new Builder<>(PublisherGrpc.METHOD_GET_TOPIC); - getTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - getTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicsMethod = - new PageStreamingBuilder<>(PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC); - listTopicsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listTopicsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicSubscriptionsMethod = - new PageStreamingBuilder<>( - PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, - LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); - listTopicSubscriptionsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listTopicSubscriptionsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - deleteTopicMethod = new Builder<>(PublisherGrpc.METHOD_DELETE_TOPIC); - deleteTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - deleteTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createTopicMethod, - publishMethod, - getTopicMethod, - listTopicsMethod, - listTopicSubscriptionsMethod, - deleteTopicMethod) - .build(); - } + public BundlingCallSettings publishSettings() { + return publishSettings; } - /** - * Constructs an instance of PublisherSettings with default settings. - * - * - * - */ - public static PublisherSettings create() { - PublisherSettings settings = new PublisherSettings(new MethodBuilders()); - settings.provideChannelWith( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); - return settings; + public SimpleCallSettings getTopicSettings() { + return getTopicSettings; } - /** - * Constructs an instance of PublisherSettings with default settings. This is protected - * so that it easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - * - * - * - */ - protected PublisherSettings(MethodBuilders methods) { - super(methods.allMethods); - this.methods = methods; + public PageStreamingCallSettings + listTopicsSettings() { + return listTopicsSettings; } - /** - * Returns the builder for the API method createTopic. - * - * - * - */ - public final ApiCallable.Builder createTopicMethod() { - return methods.createTopicMethod; + public PageStreamingCallSettings< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings() { + return listTopicSubscriptionsSettings; } - /** - * Returns the builder for the API method publish. - * - * - * - */ - public final ApiCallable.BundlableBuilder publishMethod() { - return methods.publishMethod; + public SimpleCallSettings deleteTopicSettings() { + return deleteTopicSettings; } - /** - * Returns the builder for the API method getTopic. - * - * - * - */ - public final ApiCallable.Builder getTopicMethod() { - return methods.getTopicMethod; + public static PublisherSettings defaultInstance() throws IOException { + return newBuilder().build(); } - /** - * Returns the builder for the API method listTopics. - * - * - * - */ - public final ApiCallable.PageStreamingBuilder - listTopicsMethod() { - return methods.listTopicsMethod; + public static Builder newBuilder() { + return new Builder(); } - /** - * Returns the builder for the API method listTopicSubscriptions. - * - * - * - */ - public final ApiCallable.PageStreamingBuilder< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - listTopicSubscriptionsMethod() { - return methods.listTopicSubscriptionsMethod; + public Builder toBuilder() { + return new Builder(this); } - /** - * Returns the builder for the API method deleteTopic. - * - * - * - */ - public final ApiCallable.Builder deleteTopicMethod() { - return methods.deleteTopicMethod; + private PublisherSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getOrBuildChannel(), + settingsBuilder.shouldAutoCloseChannel(), + settingsBuilder.getOrBuildExecutor(), + settingsBuilder.getConnectionSettings(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + createTopicSettings = settingsBuilder.createTopicSettings().build(); + publishSettings = settingsBuilder.publishSettings().build(); + getTopicSettings = settingsBuilder.getTopicSettings().build(); + listTopicsSettings = settingsBuilder.listTopicsSettings().build(); + listTopicSubscriptionsSettings = settingsBuilder.listTopicSubscriptionsSettings().build(); + deleteTopicSettings = settingsBuilder.deleteTopicSettings().build(); } private static PageStreamingDescriptor @@ -412,4 +285,196 @@ public long countBytes(PublishRequest request) { return request.getSerializedSize(); } }; + + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder createTopicSettings; + private BundlingCallSettings.Builder publishSettings; + private SimpleCallSettings.Builder getTopicSettings; + private PageStreamingCallSettings.Builder + listTopicsSettings; + private PageStreamingCallSettings.Builder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings; + private SimpleCallSettings.Builder deleteTopicSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.2) + .setMaxRetryDelay(Duration.millis(1000L)) + .setInitialRpcTimeout(Duration.millis(2000L)) + .setRpcTimeoutMultiplier(1.5) + .setMaxRpcTimeout(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(45000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + + createTopicSettings = + SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_CREATE_TOPIC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + BundlingSettings.Builder publishBundlingSettingsBuilder = + BundlingSettings.newBuilder() + .setElementCountThreshold(800) + .setElementCountLimit(1000) + .setRequestByteThreshold(8388608) + .setRequestByteLimit(10485760) + .setDelayThreshold(Duration.millis(100)) + .setBlockingCallCountThreshold(1); + publishSettings = + BundlingCallSettings.newBuilder(PublisherGrpc.METHOD_PUBLISH, PUBLISH_BUNDLING_DESC) + .setBundlingSettingsBuilder(publishBundlingSettingsBuilder) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + getTopicSettings = + SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_GET_TOPIC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicsSettings = + PageStreamingCallSettings.newBuilder( + PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicSubscriptionsSettings = + PageStreamingCallSettings.newBuilder( + PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, + LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + deleteTopicSettings = + SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_DELETE_TOPIC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + methodSettingsBuilders = + ImmutableList.of( + createTopicSettings, + publishSettings, + getTopicSettings, + listTopicsSettings, + listTopicSubscriptionsSettings, + deleteTopicSettings); + } + + private Builder(PublisherSettings settings) { + super(settings); + + createTopicSettings = settings.createTopicSettings.toBuilder(); + publishSettings = settings.publishSettings.toBuilder(); + getTopicSettings = settings.getTopicSettings.toBuilder(); + listTopicsSettings = settings.listTopicsSettings.toBuilder(); + listTopicSubscriptionsSettings = settings.listTopicSubscriptionsSettings.toBuilder(); + deleteTopicSettings = settings.deleteTopicSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + createTopicSettings, + publishSettings, + getTopicSettings, + listTopicsSettings, + listTopicSubscriptionsSettings, + deleteTopicSettings); + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder setExecutor(ScheduledExecutorService executor) { + super.setExecutor(executor); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + public SimpleCallSettings.Builder createTopicSettings() { + return createTopicSettings; + } + + public BundlingCallSettings.Builder publishSettings() { + return publishSettings; + } + + public SimpleCallSettings.Builder getTopicSettings() { + return getTopicSettings; + } + + public PageStreamingCallSettings.Builder + listTopicsSettings() { + return listTopicsSettings; + } + + public PageStreamingCallSettings.Builder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsSettings() { + return listTopicSubscriptionsSettings; + } + + public SimpleCallSettings.Builder deleteTopicSettings() { + return deleteTopicSettings; + } + + @Override + public PublisherSettings build() throws IOException { + return new PublisherSettings(this); + } + } } diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java index 3040af41efd8..17c1dfdf0c37 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberApi.java @@ -80,23 +80,9 @@ public class SubscriberApi implements AutoCloseable { private final ApiCallable pullCallable; private final ApiCallable modifyPushConfigCallable; - /** - * A PathTemplate representing the fully-qualified path to represent - * a project resource. - * - * - * - */ private static final PathTemplate PROJECT_PATH_TEMPLATE = PathTemplate.create("projects/{project}"); - /** - * A PathTemplate representing the fully-qualified path to represent - * a subscription resource. - * - * - * - */ private static final PathTemplate SUBSCRIPTION_PATH_TEMPLATE = PathTemplate.create("projects/{project}/subscriptions/{subscription}"); @@ -161,8 +147,8 @@ public static final String parseSubscriptionFromSubscriptionName(String subscrip * * */ - public static final SubscriberApi create() throws IOException { - return create(SubscriberSettings.create()); + public static final SubscriberApi defaultInstance() throws IOException { + return create(SubscriberSettings.defaultInstance()); } /** @@ -188,16 +174,21 @@ public static final SubscriberApi create(SubscriberSettings settings) throws IOE protected SubscriberApi(SubscriberSettings settings) throws IOException { this.channel = settings.getChannel(); - this.createSubscriptionCallable = settings.createSubscriptionMethod().build(settings); - this.getSubscriptionCallable = settings.getSubscriptionMethod().build(settings); - this.listSubscriptionsCallable = settings.listSubscriptionsMethod().build(settings); + this.createSubscriptionCallable = + ApiCallable.create(settings.createSubscriptionSettings(), settings); + this.getSubscriptionCallable = ApiCallable.create(settings.getSubscriptionSettings(), settings); + this.listSubscriptionsCallable = + ApiCallable.create(settings.listSubscriptionsSettings(), settings); this.listSubscriptionsIterableCallable = - settings.listSubscriptionsMethod().buildPageStreaming(settings); - this.deleteSubscriptionCallable = settings.deleteSubscriptionMethod().build(settings); - this.modifyAckDeadlineCallable = settings.modifyAckDeadlineMethod().build(settings); - this.acknowledgeCallable = settings.acknowledgeMethod().build(settings); - this.pullCallable = settings.pullMethod().build(settings); - this.modifyPushConfigCallable = settings.modifyPushConfigMethod().build(settings); + ApiCallable.createIterable(settings.listSubscriptionsSettings(), settings); + this.deleteSubscriptionCallable = + ApiCallable.create(settings.deleteSubscriptionSettings(), settings); + this.modifyAckDeadlineCallable = + ApiCallable.create(settings.modifyAckDeadlineSettings(), settings); + this.acknowledgeCallable = ApiCallable.create(settings.acknowledgeSettings(), settings); + this.pullCallable = ApiCallable.create(settings.pullSettings(), settings); + this.modifyPushConfigCallable = + ApiCallable.create(settings.modifyPushConfigSettings(), settings); if (settings.shouldAutoCloseChannel()) { closeables.add( @@ -252,6 +243,7 @@ public void close() throws IOException { * system will eventually redeliver the message. * * If this parameter is not set, the default value of 10 seconds is used. + * @throws ApiException if the remote call fails */ public final Subscription createSubscription( String name, String topic, PushConfig pushConfig, int ackDeadlineSeconds) { @@ -279,6 +271,7 @@ public final Subscription createSubscription( * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public Subscription createSubscription(Subscription request) { return createSubscriptionCallable().call(request); @@ -295,6 +288,7 @@ public Subscription createSubscription(Subscription request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable createSubscriptionCallable() { return createSubscriptionCallable; @@ -313,6 +307,7 @@ public final ApiCallable createSubscriptionCallable( * * * @param subscription The name of the subscription to get. + * @throws ApiException if the remote call fails */ public final Subscription getSubscription(String subscription) { GetSubscriptionRequest request = @@ -332,6 +327,7 @@ public final Subscription getSubscription(String subscription) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private Subscription getSubscription(GetSubscriptionRequest request) { return getSubscriptionCallable().call(request); @@ -346,6 +342,7 @@ private Subscription getSubscription(GetSubscriptionRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable getSubscriptionCallable() { return getSubscriptionCallable; @@ -362,6 +359,9 @@ public final ApiCallable getSubscriptionCa * * * + * + * @param project The name of the cloud project that subscriptions belong to. + * @throws ApiException if the remote call fails */ public final Iterable listSubscriptions(String project) { ListSubscriptionsRequest request = @@ -380,6 +380,7 @@ public final Iterable listSubscriptions(String project) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public final Iterable listSubscriptions(ListSubscriptionsRequest request) { return listSubscriptionsIterableCallable().call(request); @@ -394,6 +395,7 @@ public final Iterable listSubscriptions(ListSubscriptionsRequest r * * * + * @throws ApiException if the remote call fails */ public final ApiCallable> listSubscriptionsIterableCallable() { @@ -409,6 +411,7 @@ public final Iterable listSubscriptions(ListSubscriptionsRequest r * * * + * @throws ApiException if the remote call fails */ public final ApiCallable listSubscriptionsCallable() { @@ -429,6 +432,7 @@ public final Iterable listSubscriptions(ListSubscriptionsRequest r * * * @param subscription The subscription to delete. + * @throws ApiException if the remote call fails */ public final void deleteSubscription(String subscription) { DeleteSubscriptionRequest request = @@ -449,6 +453,7 @@ public final void deleteSubscription(String subscription) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ private void deleteSubscription(DeleteSubscriptionRequest request) { deleteSubscriptionCallable().call(request); @@ -464,6 +469,7 @@ private void deleteSubscription(DeleteSubscriptionRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable deleteSubscriptionCallable() { return deleteSubscriptionCallable; @@ -484,10 +490,11 @@ public final ApiCallable deleteSubscriptionCal * @param subscription The name of the subscription. * @param ackIds List of acknowledgment IDs. * @param ackDeadlineSeconds The new ack deadline with respect to the time this request was sent to - * the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new + * the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new * ack deadline will expire 10 seconds after the `ModifyAckDeadline` call * was made. Specifying zero may immediately make the message available for * another pull request. + * @throws ApiException if the remote call fails */ public final void modifyAckDeadline( String subscription, List ackIds, int ackDeadlineSeconds) { @@ -512,6 +519,7 @@ public final void modifyAckDeadline( * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public void modifyAckDeadline(ModifyAckDeadlineRequest request) { modifyAckDeadlineCallable().call(request); @@ -526,6 +534,7 @@ public void modifyAckDeadline(ModifyAckDeadlineRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable modifyAckDeadlineCallable() { return modifyAckDeadlineCallable; @@ -549,6 +558,7 @@ public final ApiCallable modifyAckDeadlineCalla * @param subscription The subscription whose message is being acknowledged. * @param ackIds The acknowledgment ID for the messages being acknowledged that was returned * by the Pub/Sub system in the `Pull` response. Must not be empty. + * @throws ApiException if the remote call fails */ public final void acknowledge(String subscription, List ackIds) { AcknowledgeRequest request = @@ -571,6 +581,7 @@ public final void acknowledge(String subscription, List ackIds) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public void acknowledge(AcknowledgeRequest request) { acknowledgeCallable().call(request); @@ -588,6 +599,7 @@ public void acknowledge(AcknowledgeRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable acknowledgeCallable() { return acknowledgeCallable; @@ -612,6 +624,7 @@ public final ApiCallable acknowledgeCallable() { * than returning no messages. * @param maxMessages The maximum number of messages returned for this request. The Pub/Sub * system may return fewer than the number specified. + * @throws ApiException if the remote call fails */ public final PullResponse pull(String subscription, boolean returnImmediately, int maxMessages) { PullRequest request = @@ -635,6 +648,7 @@ public final PullResponse pull(String subscription, boolean returnImmediately, i * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public PullResponse pull(PullRequest request) { return pullCallable().call(request); @@ -649,6 +663,7 @@ public PullResponse pull(PullRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable pullCallable() { return pullCallable; @@ -675,6 +690,7 @@ public final ApiCallable pullCallable() { * stop pushing messages from the given subscription and allow * messages to be pulled and acknowledged - effectively pausing * the subscription if `Pull` is not called. + * @throws ApiException if the remote call fails */ public final void modifyPushConfig(String subscription, PushConfig pushConfig) { ModifyPushConfigRequest request = @@ -699,6 +715,7 @@ public final void modifyPushConfig(String subscription, PushConfig pushConfig) { * * * @param request The request object containing all of the parameters for the API call. + * @throws ApiException if the remote call fails */ public void modifyPushConfig(ModifyPushConfigRequest request) { modifyPushConfigCallable().call(request); @@ -715,6 +732,7 @@ public void modifyPushConfig(ModifyPushConfigRequest request) { * * * + * @throws ApiException if the remote call fails */ public final ApiCallable modifyPushConfigCallable() { return modifyPushConfigCallable; diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java index e0204a4171f8..918ec77e9f04 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/v1/SubscriberSettings.java @@ -33,15 +33,13 @@ package com.google.gcloud.pubsub.spi.v1; -import com.google.api.gax.core.BackoffParams; import com.google.api.gax.core.ConnectionSettings; -import com.google.api.gax.core.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.api.gax.grpc.ApiCallSettings; -import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.ApiCallable.Builder; -import com.google.api.gax.grpc.ApiCallable.PageStreamingBuilder; +import com.google.api.gax.grpc.PageStreamingCallSettings; import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -59,7 +57,10 @@ import com.google.pubsub.v1.PullResponse; import com.google.pubsub.v1.SubscriberGrpc; import com.google.pubsub.v1.Subscription; +import io.grpc.ManagedChannel; import io.grpc.Status; +import java.io.IOException; +import java.util.concurrent.ScheduledExecutorService; import org.joda.time.Duration; // Manually-added imports: add custom (non-generated) imports after this point. @@ -93,219 +94,83 @@ public class SubscriberSettings extends ServiceApiSettings { .add("https://www.googleapis.com/auth/cloud-platform") .build(); - private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; - - static { - ImmutableMap.Builder> definitions = ImmutableMap.builder(); - definitions.put( - "idempotent", - Sets.immutableEnumSet( - Lists.newArrayList( - Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); - definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); - RETRYABLE_CODE_DEFINITIONS = definitions.build(); - } + private final SimpleCallSettings createSubscriptionSettings; + private final SimpleCallSettings getSubscriptionSettings; + private final PageStreamingCallSettings< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings; - private static final ImmutableMap RETRY_PARAM_DEFINITIONS; - - static { - ImmutableMap.Builder definitions = ImmutableMap.builder(); - RetryParams params = null; - params = - RetryParams.newBuilder() - .setRetryBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(100L)) - .setDelayMultiplier(1.2) - .setMaxDelay(Duration.millis(1000L)) - .build()) - .setTimeoutBackoff( - BackoffParams.newBuilder() - .setInitialDelay(Duration.millis(2000L)) - .setDelayMultiplier(1.5) - .setMaxDelay(Duration.millis(30000L)) - .build()) - .setTotalTimeout(Duration.millis(45000L)) - .build(); - definitions.put("default", params); - RETRY_PARAM_DEFINITIONS = definitions.build(); - } + private final SimpleCallSettings deleteSubscriptionSettings; + private final SimpleCallSettings modifyAckDeadlineSettings; + private final SimpleCallSettings acknowledgeSettings; + private final SimpleCallSettings pullSettings; + private final SimpleCallSettings modifyPushConfigSettings; - private final MethodBuilders methods; + public SimpleCallSettings createSubscriptionSettings() { + return createSubscriptionSettings; + } - private static class MethodBuilders { - private final ApiCallable.Builder createSubscriptionMethod; - private final ApiCallable.Builder getSubscriptionMethod; - private final ApiCallable.PageStreamingBuilder< - ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> - listSubscriptionsMethod; - private final ApiCallable.Builder deleteSubscriptionMethod; - private final ApiCallable.Builder modifyAckDeadlineMethod; - private final ApiCallable.Builder acknowledgeMethod; - private final ApiCallable.Builder pullMethod; - private final ApiCallable.Builder modifyPushConfigMethod; - private final ImmutableList allMethods; - - public MethodBuilders() { - createSubscriptionMethod = new Builder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); - createSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - createSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - getSubscriptionMethod = new Builder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); - getSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - getSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - listSubscriptionsMethod = - new PageStreamingBuilder<>( - SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC); - listSubscriptionsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listSubscriptionsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - deleteSubscriptionMethod = new Builder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); - deleteSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - deleteSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - modifyAckDeadlineMethod = new Builder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); - modifyAckDeadlineMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - modifyAckDeadlineMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - acknowledgeMethod = new Builder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); - acknowledgeMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - acknowledgeMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - pullMethod = new Builder<>(SubscriberGrpc.METHOD_PULL); - pullMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - pullMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - modifyPushConfigMethod = new Builder<>(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); - modifyPushConfigMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); - modifyPushConfigMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createSubscriptionMethod, - getSubscriptionMethod, - listSubscriptionsMethod, - deleteSubscriptionMethod, - modifyAckDeadlineMethod, - acknowledgeMethod, - pullMethod, - modifyPushConfigMethod) - .build(); - } + public SimpleCallSettings getSubscriptionSettings() { + return getSubscriptionSettings; } - /** - * Constructs an instance of SubscriberSettings with default settings. - * - * - * - */ - public static SubscriberSettings create() { - SubscriberSettings settings = new SubscriberSettings(new MethodBuilders()); - settings.provideChannelWith( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()); - return settings; + public PageStreamingCallSettings< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings() { + return listSubscriptionsSettings; } - /** - * Constructs an instance of SubscriberSettings with default settings. This is protected - * so that it easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - * - * - * - */ - protected SubscriberSettings(MethodBuilders methods) { - super(methods.allMethods); - this.methods = methods; + public SimpleCallSettings deleteSubscriptionSettings() { + return deleteSubscriptionSettings; } - /** - * Returns the builder for the API method createSubscription. - * - * - * - */ - public final ApiCallable.Builder createSubscriptionMethod() { - return methods.createSubscriptionMethod; + public SimpleCallSettings modifyAckDeadlineSettings() { + return modifyAckDeadlineSettings; } - /** - * Returns the builder for the API method getSubscription. - * - * - * - */ - public final ApiCallable.Builder getSubscriptionMethod() { - return methods.getSubscriptionMethod; + public SimpleCallSettings acknowledgeSettings() { + return acknowledgeSettings; } - /** - * Returns the builder for the API method listSubscriptions. - * - * - * - */ - public final ApiCallable.PageStreamingBuilder< - ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> - listSubscriptionsMethod() { - return methods.listSubscriptionsMethod; + public SimpleCallSettings pullSettings() { + return pullSettings; } - /** - * Returns the builder for the API method deleteSubscription. - * - * - * - */ - public final ApiCallable.Builder deleteSubscriptionMethod() { - return methods.deleteSubscriptionMethod; + public SimpleCallSettings modifyPushConfigSettings() { + return modifyPushConfigSettings; } - /** - * Returns the builder for the API method modifyAckDeadline. - * - * - * - */ - public final ApiCallable.Builder modifyAckDeadlineMethod() { - return methods.modifyAckDeadlineMethod; + public static SubscriberSettings defaultInstance() throws IOException { + return newBuilder().build(); } - /** - * Returns the builder for the API method acknowledge. - * - * - * - */ - public final ApiCallable.Builder acknowledgeMethod() { - return methods.acknowledgeMethod; + public static Builder newBuilder() { + return new Builder(); } - /** - * Returns the builder for the API method pull. - * - * - * - */ - public final ApiCallable.Builder pullMethod() { - return methods.pullMethod; + public Builder toBuilder() { + return new Builder(this); } - /** - * Returns the builder for the API method modifyPushConfig. - * - * - * - */ - public final ApiCallable.Builder modifyPushConfigMethod() { - return methods.modifyPushConfigMethod; + private SubscriberSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getOrBuildChannel(), + settingsBuilder.shouldAutoCloseChannel(), + settingsBuilder.getOrBuildExecutor(), + settingsBuilder.getConnectionSettings(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + createSubscriptionSettings = settingsBuilder.createSubscriptionSettings().build(); + getSubscriptionSettings = settingsBuilder.getSubscriptionSettings().build(); + listSubscriptionsSettings = settingsBuilder.listSubscriptionsSettings().build(); + deleteSubscriptionSettings = settingsBuilder.deleteSubscriptionSettings().build(); + modifyAckDeadlineSettings = settingsBuilder.modifyAckDeadlineSettings().build(); + acknowledgeSettings = settingsBuilder.acknowledgeSettings().build(); + pullSettings = settingsBuilder.pullSettings().build(); + modifyPushConfigSettings = settingsBuilder.modifyPushConfigSettings().build(); } private static PageStreamingDescriptor< @@ -336,4 +201,212 @@ public Iterable extractResources(ListSubscriptionsResponse payload return payload.getSubscriptionsList(); } }; + + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder createSubscriptionSettings; + private SimpleCallSettings.Builder + getSubscriptionSettings; + private PageStreamingCallSettings.Builder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings; + private SimpleCallSettings.Builder deleteSubscriptionSettings; + private SimpleCallSettings.Builder modifyAckDeadlineSettings; + private SimpleCallSettings.Builder acknowledgeSettings; + private SimpleCallSettings.Builder pullSettings; + private SimpleCallSettings.Builder modifyPushConfigSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.2) + .setMaxRetryDelay(Duration.millis(1000L)) + .setInitialRpcTimeout(Duration.millis(2000L)) + .setRpcTimeoutMultiplier(1.5) + .setMaxRpcTimeout(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(45000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + + createSubscriptionSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + getSubscriptionSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_GET_SUBSCRIPTION) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + listSubscriptionsSettings = + PageStreamingCallSettings.newBuilder( + SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + deleteSubscriptionSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyAckDeadlineSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + acknowledgeSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_ACKNOWLEDGE) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + pullSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_PULL) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyPushConfigSettings = + SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG) + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + methodSettingsBuilders = + ImmutableList.of( + createSubscriptionSettings, + getSubscriptionSettings, + listSubscriptionsSettings, + deleteSubscriptionSettings, + modifyAckDeadlineSettings, + acknowledgeSettings, + pullSettings, + modifyPushConfigSettings); + } + + private Builder(SubscriberSettings settings) { + super(settings); + + createSubscriptionSettings = settings.createSubscriptionSettings.toBuilder(); + getSubscriptionSettings = settings.getSubscriptionSettings.toBuilder(); + listSubscriptionsSettings = settings.listSubscriptionsSettings.toBuilder(); + deleteSubscriptionSettings = settings.deleteSubscriptionSettings.toBuilder(); + modifyAckDeadlineSettings = settings.modifyAckDeadlineSettings.toBuilder(); + acknowledgeSettings = settings.acknowledgeSettings.toBuilder(); + pullSettings = settings.pullSettings.toBuilder(); + modifyPushConfigSettings = settings.modifyPushConfigSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + createSubscriptionSettings, + getSubscriptionSettings, + listSubscriptionsSettings, + deleteSubscriptionSettings, + modifyAckDeadlineSettings, + acknowledgeSettings, + pullSettings, + modifyPushConfigSettings); + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder setExecutor(ScheduledExecutorService executor) { + super.setExecutor(executor); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + public SimpleCallSettings.Builder createSubscriptionSettings() { + return createSubscriptionSettings; + } + + public SimpleCallSettings.Builder + getSubscriptionSettings() { + return getSubscriptionSettings; + } + + public PageStreamingCallSettings.Builder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsSettings() { + return listSubscriptionsSettings; + } + + public SimpleCallSettings.Builder + deleteSubscriptionSettings() { + return deleteSubscriptionSettings; + } + + public SimpleCallSettings.Builder modifyAckDeadlineSettings() { + return modifyAckDeadlineSettings; + } + + public SimpleCallSettings.Builder acknowledgeSettings() { + return acknowledgeSettings; + } + + public SimpleCallSettings.Builder pullSettings() { + return pullSettings; + } + + public SimpleCallSettings.Builder modifyPushConfigSettings() { + return modifyPushConfigSettings; + } + + @Override + public SubscriberSettings build() throws IOException { + return new SubscriberSettings(this); + } + } } diff --git a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/v1/PublisherApiTest.java b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/v1/PublisherApiTest.java index dac4be9ae8fc..109c537c3f4a 100644 --- a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/v1/PublisherApiTest.java +++ b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/v1/PublisherApiTest.java @@ -58,23 +58,29 @@ public static void stopServer() throws IOException, InterruptedException { public void setUp() throws Exception { ManagedChannel channel = pubsubHelper.createChannel(); - PublisherSettings publisherSettings = PublisherSettings.create(); - publisherSettings.provideChannelWith(channel, true); + PublisherSettings publisherSettings = + PublisherSettings.newBuilder() + .provideChannelWith(channel, true) + .build(); publisherApi = PublisherApi.create(publisherSettings); - BundlingSettings bundlingSettings = + BundlingSettings.Builder bundlingSettings = BundlingSettings.newBuilder() .setElementCountThreshold(10) - .setDelayThreshold(Duration.standardSeconds(30)) - .build(); + .setDelayThreshold(Duration.standardSeconds(30)); + + PublisherSettings.Builder bundledPublisherSettingsBuilder = PublisherSettings.newBuilder(); + bundledPublisherSettingsBuilder + .provideChannelWith(channel, true) + .publishSettings() + .setBundlingSettingsBuilder(bundlingSettings); - PublisherSettings bundledPublisherSettings = PublisherSettings.create(); - bundledPublisherSettings.provideChannelWith(channel, true); - bundledPublisherSettings.publishMethod().setBundlingSettings(bundlingSettings); + PublisherSettings bundledPublisherSettings = bundledPublisherSettingsBuilder.build(); bundledPublisherApi = PublisherApi.create(bundledPublisherSettings); - SubscriberSettings subscriberSettings = SubscriberSettings.create(); - subscriberSettings.provideChannelWith(channel, true); + SubscriberSettings subscriberSettings = SubscriberSettings.newBuilder() + .provideChannelWith(channel, true) + .build(); subscriberApi = SubscriberApi.create(subscriberSettings); }