From b2115f3ae4747eb2fd6083c4918ebe7afc6d3bd9 Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Fri, 4 Mar 2016 11:00:11 -0800 Subject: [PATCH 1/7] Regenerating code, new settings classes * Moving settings constants and data into new settings classes * Making use of ApiCallableBuilder in the new settings classes * Simplifying the Api classes --- .../gcloud/pubsub/spi/PublisherApi.java | 555 +++++++++++++ .../gcloud/pubsub/spi/PublisherSettings.java | 333 ++++++++ .../gcloud/pubsub/spi/SubscriberApi.java | 777 ++++++++++++++++++ .../gcloud/pubsub/spi/SubscriberSettings.java | 336 ++++++++ gcloud-java-pubsub/pom.xml | 2 +- .../gcloud/pubsub/spi/PublisherApi.java | 310 ++----- .../gcloud/pubsub/spi/PublisherSettings.java | 333 ++++++++ .../gcloud/pubsub/spi/SubscriberApi.java | 310 ++----- .../gcloud/pubsub/spi/SubscriberSettings.java | 336 ++++++++ .../gcloud/pubsub/spi/PublisherApiTest.java | 33 +- 10 files changed, 2811 insertions(+), 514 deletions(-) create mode 100644 gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java create mode 100644 gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java create mode 100644 gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java create mode 100644 gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java create mode 100644 gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java create mode 100644 gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java new file mode 100644 index 000000000000..f7011dcd0240 --- /dev/null +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -0,0 +1,555 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ + +package com.google.gcloud.pubsub.spi; + +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.protobuf.Empty; +import com.google.pubsub.v1.DeleteTopicRequest; +import com.google.pubsub.v1.GetTopicRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsResponse; +import com.google.pubsub.v1.ListTopicsRequest; +import com.google.pubsub.v1.ListTopicsResponse; +import com.google.pubsub.v1.PublishRequest; +import com.google.pubsub.v1.PublishResponse; +import com.google.pubsub.v1.PubsubMessage; +import com.google.pubsub.v1.Topic; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +// Manually-added imports: add custom (non-generated) imports after this point. + +// AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. +/** + * The service that an application uses to manipulate topics, and to send + * messages to a topic. + * + * + * + */ +@javax.annotation.Generated("by GAPIC") +public class PublisherApi implements AutoCloseable { + + // ========= + // Constants + // ========= + + /** + * 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}"); + + // ======== + // Members + // ======== + + private final ManagedChannel channel; + private final List closeables = new ArrayList<>(); + + private final ApiCallable createTopicCallable; + private final ApiCallable publishCallable; + private final ApiCallable getTopicCallable; + private final ApiCallable listTopicsCallable; + private final ApiCallable> listTopicsIterableCallable; + private final ApiCallable + listTopicSubscriptionsCallable; + private final ApiCallable> + listTopicSubscriptionsIterableCallable; + private final ApiCallable deleteTopicCallable; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of PublisherSettings with default settings. + */ + public static PublisherSettings newSettings() { + return PublisherSettings.create(); + } + + /** + * Constructs an instance of PublisherApi with default settings. + * + * + * + */ + public static PublisherApi create() throws IOException { + return create(newSettings()); + } + + /** + * Constructs an instance of PublisherApi, using the given settings. The channels are created based + * on the settings passed in, or defaults for any settings that are not set. + * + * + * + */ + public static PublisherApi create(PublisherSettings settings) throws IOException { + return new PublisherApi(settings); + } + + /** + * Constructs an instance of PublisherApi, using the given settings. This is protected so that it + * easy to make a subclass, but otherwise, the static factory methods should be preferred. + * + * + * + */ + protected PublisherApi(PublisherSettings settings) throws IOException { + this.channel = settings.getChannel(); + + for (ApiCallSettings method : settings.allMethods()) { + if (method.getExecutor() == null) { + method.setExecutor(settings.getExecutor()); + } + } + + this.createTopicCallable = settings.createTopicMethod().build(settings); + this.publishCallable = settings.publishMethod().build(settings); + 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.listTopicSubscriptionsIterableCallable = + settings.listTopicSubscriptionsMethod().buildPageStreaming(settings); + this.deleteTopicCallable = settings.deleteTopicMethod().build(settings); + + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Creates a string containing the fully-qualified path to represent + * a project resource. + * + * + * + */ + public static final String createProjectPath(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } + + /** + * Creates a string containing the fully-qualified path to represent + * a topic resource. + * + * + * + */ + public static final String createTopicPath(String project, String topic) { + return TOPIC_PATH_TEMPLATE.instantiate("project", project, "topic", topic); + } + + /** + * Extracts the project from the given fully-qualified path which + * represents a project resource. + * + * + * + */ + public static final String extractProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } + + /** + * Extracts the project from the given fully-qualified path which + * represents a topic resource. + * + * + * + */ + public static final String extractProjectFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project"); + } + + /** + * Extracts the topic from the given fully-qualified path which + * represents a topic resource. + * + * + * + */ + public static final String extractTopicFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic"); + } + + // ============= + // Service Calls + // ============= + + // ----- createTopic ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates the given topic with the given name. + * + * + * + * + * @param name The name of the topic. It must have the format + * `"projects/{project}/topics/{topic}"`. `{topic}` must start with a letter, + * and contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`), + * underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent + * signs (`%`). It must be between 3 and 255 characters in length, and it + * must not start with `"goog"`. + */ + public Topic createTopic(String name) { + Topic request = Topic.newBuilder().setName(name).build(); + + return createTopic(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates the given topic with the given name. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Topic createTopic(Topic request) { + return createTopicCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates the given topic with the given name. + * + * + * + */ + public ApiCallable createTopicCallable() { + return createTopicCallable; + } + + // ----- publish ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Adds one or more messages to the topic. Generates `NOT_FOUND` if the topic + * does not exist. The message payload must not be empty; it must contain + * either a non-empty data field, or at least one attribute. + * + * + * + * + * @param topic The messages in the request will be published on this topic. + * @param messages The messages to publish. + */ + public PublishResponse publish(String topic, List messages) { + PublishRequest request = + PublishRequest.newBuilder().setTopic(topic).addAllMessages(messages).build(); + + return publish(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Adds one or more messages to the topic. Generates `NOT_FOUND` if the topic + * does not exist. The message payload must not be empty; it must contain + * either a non-empty data field, or at least one attribute. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public PublishResponse publish(PublishRequest request) { + return publishCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Adds one or more messages to the topic. Generates `NOT_FOUND` if the topic + * does not exist. The message payload must not be empty; it must contain + * either a non-empty data field, or at least one attribute. + * + * + * + */ + public ApiCallable publishCallable() { + return publishCallable; + } + + // ----- getTopic ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets the configuration of a topic. + * + * + * + * + * @param topic The name of the topic to get. + */ + public Topic getTopic(String topic) { + GetTopicRequest request = GetTopicRequest.newBuilder().setTopic(topic).build(); + + return getTopic(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets the configuration of a topic. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Topic getTopic(GetTopicRequest request) { + return getTopicCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets the configuration of a topic. + * + * + * + */ + public ApiCallable getTopicCallable() { + return getTopicCallable; + } + + // ----- listTopics ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching topics. + * + * + * + */ + public Iterable listTopics(String project) { + ListTopicsRequest request = ListTopicsRequest.newBuilder().setProject(project).build(); + return listTopics(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching topics. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listTopics(ListTopicsRequest request) { + return listTopicsIterableCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching topics. + * + * + * + */ + public ApiCallable> listTopicsIterableCallable() { + return listTopicsIterableCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching topics. + * + * + * + */ + public ApiCallable listTopicsCallable() { + return listTopicsCallable; + } + + // ----- listTopicSubscriptions ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists the name of the subscriptions for this topic. + * + * + * + */ + public Iterable listTopicSubscriptions(String topic) { + ListTopicSubscriptionsRequest request = + ListTopicSubscriptionsRequest.newBuilder().setTopic(topic).build(); + return listTopicSubscriptions(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists the name of the subscriptions for this topic. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listTopicSubscriptions(ListTopicSubscriptionsRequest request) { + return listTopicSubscriptionsIterableCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists the name of the subscriptions for this topic. + * + * + * + */ + public ApiCallable> + listTopicSubscriptionsIterableCallable() { + return listTopicSubscriptionsIterableCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists the name of the subscriptions for this topic. + * + * + * + */ + public ApiCallable + listTopicSubscriptionsCallable() { + return listTopicSubscriptionsCallable; + } + + // ----- deleteTopic ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes the topic with the given name. Generates `NOT_FOUND` if the topic + * does not exist. After a topic is deleted, a new topic may be created with + * the same name; this is an entirely new topic with none of the old + * configuration or subscriptions. Existing subscriptions to this topic are + * not deleted, but their `topic` field is set to `_deleted-topic_`. + * + * + * + * + * @param topic Name of the topic to delete. + */ + public void deleteTopic(String topic) { + DeleteTopicRequest request = DeleteTopicRequest.newBuilder().setTopic(topic).build(); + + deleteTopic(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes the topic with the given name. Generates `NOT_FOUND` if the topic + * does not exist. After a topic is deleted, a new topic may be created with + * the same name; this is an entirely new topic with none of the old + * configuration or subscriptions. Existing subscriptions to this topic are + * not deleted, but their `topic` field is set to `_deleted-topic_`. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void deleteTopic(DeleteTopicRequest request) { + deleteTopicCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes the topic with the given name. Generates `NOT_FOUND` if the topic + * does not exist. After a topic is deleted, a new topic may be created with + * the same name; this is an entirely new topic with none of the old + * configuration or subscriptions. Existing subscriptions to this topic are + * not deleted, but their `topic` field is set to `_deleted-topic_`. + * + * + * + */ + public ApiCallable deleteTopicCallable() { + return deleteTopicCallable; + } + + // ======== + // Cleanup + // ======== + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + * + * + * + */ + @Override + public void close() { + // Manually-added shutdown code + + // Auto-generated shutdown code + channel.shutdown(); + + // Manually-added shutdown code + } + + // ======== + // Manually-added methods: add custom (non-generated) methods after this point. + // ======== + +} diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java new file mode 100644 index 000000000000..b1985fc40b53 --- /dev/null +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -0,0 +1,333 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ + +package com.google.gcloud.pubsub.spi; + +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.grpc.ApiCallSettings; +import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; +import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; +import com.google.api.gax.grpc.PageDescriptor; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.protobuf.Empty; +import com.google.pubsub.v1.DeleteTopicRequest; +import com.google.pubsub.v1.GetTopicRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsResponse; +import com.google.pubsub.v1.ListTopicsRequest; +import com.google.pubsub.v1.ListTopicsResponse; +import com.google.pubsub.v1.PublishRequest; +import com.google.pubsub.v1.PublishResponse; +import com.google.pubsub.v1.PublisherGrpc; +import com.google.pubsub.v1.Topic; +import io.grpc.Status; + +// Manually-added imports: add custom (non-generated) imports after this point. + +// AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +@javax.annotation.Generated("by GAPIC") +public class PublisherSettings extends ApiCallSettings { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + public static ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder() + .add("https://www.googleapis.com/auth/pubsub") + .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 static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetryParams params = null; + params = + RetryParams.newBuilder() + .setRetryBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(100L) + .setDelayMultiplier(1.2) + .setMaxDelayMillis(1000L) + .build()) + .setTimeoutBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(300L) + .setDelayMultiplier(1.3) + .setMaxDelayMillis(3000L) + .build()) + .setTotalTimeout(30000L) + .build(); + definitions.put("default", params); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private final ApiCallableBuilder createTopicMethod; + private final ApiCallableBuilder publishMethod; + private final ApiCallableBuilder getTopicMethod; + private final PageStreamingApiCallableBuilder + listTopicsMethod; + private final PageStreamingApiCallableBuilder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsMethod; + private final ApiCallableBuilder deleteTopicMethod; + private final ImmutableList allMethods; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of PublisherSettings with default settings. + * + * + * + */ + public static PublisherSettings create() { + PublisherSettings settings = new PublisherSettings(); + settings.provideChannelWith( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + return settings; + } + + /** + * 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() { + createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); + createTopicMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + publishMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_PUBLISH); + publishMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + getTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_GET_TOPIC); + getTopicMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicsMethod = + new PageStreamingApiCallableBuilder<>( + PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC); + listTopicsMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicSubscriptionsMethod = + new PageStreamingApiCallableBuilder<>( + PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); + listTopicSubscriptionsMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + deleteTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_DELETE_TOPIC); + deleteTopicMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + allMethods = + ImmutableList.builder() + .add( + createTopicMethod, + publishMethod, + getTopicMethod, + listTopicsMethod, + listTopicSubscriptionsMethod, + deleteTopicMethod) + .build(); + } + + /** + * Returns the ApiCallableBuilder for the API method createTopic. + * + * + * + */ + public ApiCallableBuilder createTopicMethod() { + return createTopicMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method publish. + * + * + * + */ + public ApiCallableBuilder publishMethod() { + return publishMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method getTopic. + * + * + * + */ + public ApiCallableBuilder getTopicMethod() { + return getTopicMethod; + } + + /** + * Returns the PageStreamingApiCallableBuilder for the API method listTopics. + * + * + * + */ + public PageStreamingApiCallableBuilder + listTopicsMethod() { + return listTopicsMethod; + } + + /** + * Returns the PageStreamingApiCallableBuilder for the API method listTopicSubscriptions. + * + * + * + */ + public PageStreamingApiCallableBuilder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsMethod() { + return listTopicSubscriptionsMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method deleteTopic. + * + * + * + */ + public ApiCallableBuilder deleteTopicMethod() { + return deleteTopicMethod; + } + + public ImmutableList allMethods() { + return allMethods; + } + + private static PageDescriptor + LIST_TOPICS_PAGE_STR_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListTopicsRequest injectToken(ListTopicsRequest payload, Object token) { + return ListTopicsRequest.newBuilder(payload).setPageToken((String) token).build(); + } + + @Override + public Object extractNextToken(ListTopicsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListTopicsResponse payload) { + return payload.getTopicsList(); + } + }; + + private static PageDescriptor< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC = + new PageDescriptor< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String>() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListTopicSubscriptionsRequest injectToken( + ListTopicSubscriptionsRequest payload, Object token) { + return ListTopicSubscriptionsRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListTopicSubscriptionsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListTopicSubscriptionsResponse payload) { + return payload.getSubscriptionsList(); + } + }; +} diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java new file mode 100644 index 000000000000..c50e6b6aaaed --- /dev/null +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -0,0 +1,777 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ + +package com.google.gcloud.pubsub.spi; + +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.protobuf.Empty; +import com.google.pubsub.v1.AcknowledgeRequest; +import com.google.pubsub.v1.DeleteSubscriptionRequest; +import com.google.pubsub.v1.GetSubscriptionRequest; +import com.google.pubsub.v1.ListSubscriptionsRequest; +import com.google.pubsub.v1.ListSubscriptionsResponse; +import com.google.pubsub.v1.ModifyAckDeadlineRequest; +import com.google.pubsub.v1.ModifyPushConfigRequest; +import com.google.pubsub.v1.PullRequest; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.Subscription; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +// Manually-added imports: add custom (non-generated) imports after this point. + +// AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. +/** + * The service that an application uses to manipulate subscriptions and to + * consume messages from a subscription via the `Pull` method. + * + * + * + */ +@javax.annotation.Generated("by GAPIC") +public class SubscriberApi implements AutoCloseable { + + // ========= + // Constants + // ========= + + /** + * 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}"); + + // ======== + // Members + // ======== + + private final ManagedChannel channel; + private final List closeables = new ArrayList<>(); + + private final ApiCallable createSubscriptionCallable; + private final ApiCallable getSubscriptionCallable; + private final ApiCallable + listSubscriptionsCallable; + private final ApiCallable> + listSubscriptionsIterableCallable; + private final ApiCallable deleteSubscriptionCallable; + private final ApiCallable modifyAckDeadlineCallable; + private final ApiCallable acknowledgeCallable; + private final ApiCallable pullCallable; + private final ApiCallable modifyPushConfigCallable; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of SubscriberSettings with default settings. + */ + public static SubscriberSettings newSettings() { + return SubscriberSettings.create(); + } + + /** + * Constructs an instance of SubscriberApi with default settings. + * + * + * + */ + public static SubscriberApi create() throws IOException { + return create(newSettings()); + } + + /** + * Constructs an instance of SubscriberApi, using the given settings. The channels are created based + * on the settings passed in, or defaults for any settings that are not set. + * + * + * + */ + public static SubscriberApi create(SubscriberSettings settings) throws IOException { + return new SubscriberApi(settings); + } + + /** + * Constructs an instance of SubscriberApi, using the given settings. This is protected so that it + * easy to make a subclass, but otherwise, the static factory methods should be preferred. + * + * + * + */ + protected SubscriberApi(SubscriberSettings settings) throws IOException { + this.channel = settings.getChannel(); + + for (ApiCallSettings method : settings.allMethods()) { + if (method.getExecutor() == null) { + method.setExecutor(settings.getExecutor()); + } + } + + this.createSubscriptionCallable = settings.createSubscriptionMethod().build(settings); + this.getSubscriptionCallable = settings.getSubscriptionMethod().build(settings); + this.listSubscriptionsCallable = settings.listSubscriptionsMethod().build(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); + + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Creates a string containing the fully-qualified path to represent + * a project resource. + * + * + * + */ + public static final String createProjectPath(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } + + /** + * Creates a string containing the fully-qualified path to represent + * a subscription resource. + * + * + * + */ + public static final String createSubscriptionPath(String project, String subscription) { + return SUBSCRIPTION_PATH_TEMPLATE.instantiate("project", project, "subscription", subscription); + } + + /** + * Extracts the project from the given fully-qualified path which + * represents a project resource. + * + * + * + */ + public static final String extractProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } + + /** + * Extracts the project from the given fully-qualified path which + * represents a subscription resource. + * + * + * + */ + public static final String extractProjectFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project"); + } + + /** + * Extracts the subscription from the given fully-qualified path which + * represents a subscription resource. + * + * + * + */ + public static final String extractSubscriptionFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription"); + } + + // ============= + // Service Calls + // ============= + + // ----- createSubscription ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a subscription to a given topic for a given subscriber. + * If the subscription already exists, generates `ALREADY_EXISTS`. + * If the corresponding topic doesn't exist, generates `NOT_FOUND`. + * + * If the name is not provided in the request, the server will assign a random + * name for this subscription on the same project as the topic. + * + * + * + * + * @param name The name of the subscription. It must have the format + * `"projects/{project}/subscriptions/{subscription}"`. `{subscription}` must + * start with a letter, and contain only letters (`[A-Za-z]`), numbers + * (`[0-9]`), dashes (`-`), underscores (`_`), periods (`.`), tildes (`~`), + * plus (`+`) or percent signs (`%`). It must be between 3 and 255 characters + * in length, and it must not start with `"goog"`. + * @param topic The name of the topic from which this subscription is receiving messages. + * @param pushConfig If push delivery is used with this subscription, this field is + * used to configure it. An empty `pushConfig` signifies that the subscriber + * will pull and ack messages using API methods. + * @param ackDeadlineSeconds This value is the maximum time after a subscriber receives a message + * before the subscriber should acknowledge the message. After message + * delivery but before the ack deadline expires and before the message is + * acknowledged, it is an outstanding message and will not be delivered + * again during that time (on a best-effort basis). + * + * For pull subscriptions, this value is used as the initial value for the ack + * deadline. To override this value for a given message, call + * `ModifyAckDeadline` with the corresponding `ack_id` if using + * pull. + * + * For push delivery, this value is also used to set the request timeout for + * the call to the push endpoint. + * + * If the subscriber never acknowledges the message, the Pub/Sub + * system will eventually redeliver the message. + * + * If this parameter is not set, the default value of 10 seconds is used. + */ + public Subscription createSubscription( + String name, String topic, PushConfig pushConfig, int ackDeadlineSeconds) { + Subscription request = + Subscription.newBuilder() + .setName(name) + .setTopic(topic) + .setPushConfig(pushConfig) + .setAckDeadlineSeconds(ackDeadlineSeconds) + .build(); + + return createSubscription(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a subscription to a given topic for a given subscriber. + * If the subscription already exists, generates `ALREADY_EXISTS`. + * If the corresponding topic doesn't exist, generates `NOT_FOUND`. + * + * If the name is not provided in the request, the server will assign a random + * name for this subscription on the same project as the topic. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Subscription createSubscription(Subscription request) { + return createSubscriptionCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Creates a subscription to a given topic for a given subscriber. + * If the subscription already exists, generates `ALREADY_EXISTS`. + * If the corresponding topic doesn't exist, generates `NOT_FOUND`. + * + * If the name is not provided in the request, the server will assign a random + * name for this subscription on the same project as the topic. + * + * + * + */ + public ApiCallable createSubscriptionCallable() { + return createSubscriptionCallable; + } + + // ----- getSubscription ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets the configuration details of a subscription. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + * + * @param subscription The name of the subscription to get. + */ + public Subscription getSubscription(String subscription) { + GetSubscriptionRequest request = + GetSubscriptionRequest.newBuilder().setSubscription(subscription).build(); + + return getSubscription(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets the configuration details of a subscription. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Subscription getSubscription(GetSubscriptionRequest request) { + return getSubscriptionCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Gets the configuration details of a subscription. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + */ + public ApiCallable getSubscriptionCallable() { + return getSubscriptionCallable; + } + + // ----- listSubscriptions ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching subscriptions. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + */ + public Iterable listSubscriptions(String project) { + ListSubscriptionsRequest request = + ListSubscriptionsRequest.newBuilder().setProject(project).build(); + return listSubscriptions(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching subscriptions. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public Iterable listSubscriptions(ListSubscriptionsRequest request) { + return listSubscriptionsIterableCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching subscriptions. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + */ + public ApiCallable> + listSubscriptionsIterableCallable() { + return listSubscriptionsIterableCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Lists matching subscriptions. + * + * If the topic of a subscription has been deleted, the subscription itself is + * not deleted, but the value of the `topic` field is set to `_deleted-topic_`. + * + * + * + */ + public ApiCallable + listSubscriptionsCallable() { + return listSubscriptionsCallable; + } + + // ----- deleteSubscription ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes an existing subscription. All pending messages in the subscription + * are immediately dropped. Calls to `Pull` after deletion will generate + * `NOT_FOUND`. After a subscription is deleted, a new one may be created with + * the same name, but the new one has no association with the old + * subscription, or its topic unless the same topic is specified. + * + * + * + * + * @param subscription The subscription to delete. + */ + public void deleteSubscription(String subscription) { + DeleteSubscriptionRequest request = + DeleteSubscriptionRequest.newBuilder().setSubscription(subscription).build(); + + deleteSubscription(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes an existing subscription. All pending messages in the subscription + * are immediately dropped. Calls to `Pull` after deletion will generate + * `NOT_FOUND`. After a subscription is deleted, a new one may be created with + * the same name, but the new one has no association with the old + * subscription, or its topic unless the same topic is specified. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void deleteSubscription(DeleteSubscriptionRequest request) { + deleteSubscriptionCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Deletes an existing subscription. All pending messages in the subscription + * are immediately dropped. Calls to `Pull` after deletion will generate + * `NOT_FOUND`. After a subscription is deleted, a new one may be created with + * the same name, but the new one has no association with the old + * subscription, or its topic unless the same topic is specified. + * + * + * + */ + public ApiCallable deleteSubscriptionCallable() { + return deleteSubscriptionCallable; + } + + // ----- modifyAckDeadline ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Modifies the ack deadline for a specific message. This method is useful + * to indicate that more time is needed to process a message by the + * subscriber, or to make the message available for redelivery if the + * processing was interrupted. + * + * + * + * + * @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 + * ack deadline will expire 10 seconds after the `ModifyAckDeadline` call + * was made. Specifying zero may immediately make the message available for + * another pull request. + */ + public void modifyAckDeadline(String subscription, List ackIds, int ackDeadlineSeconds) { + ModifyAckDeadlineRequest request = + ModifyAckDeadlineRequest.newBuilder() + .setSubscription(subscription) + .addAllAckIds(ackIds) + .setAckDeadlineSeconds(ackDeadlineSeconds) + .build(); + + modifyAckDeadline(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Modifies the ack deadline for a specific message. This method is useful + * to indicate that more time is needed to process a message by the + * subscriber, or to make the message available for redelivery if the + * processing was interrupted. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void modifyAckDeadline(ModifyAckDeadlineRequest request) { + modifyAckDeadlineCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Modifies the ack deadline for a specific message. This method is useful + * to indicate that more time is needed to process a message by the + * subscriber, or to make the message available for redelivery if the + * processing was interrupted. + * + * + * + */ + public ApiCallable modifyAckDeadlineCallable() { + return modifyAckDeadlineCallable; + } + + // ----- acknowledge ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Acknowledges the messages associated with the `ack_ids` in the + * `AcknowledgeRequest`. The Pub/Sub system can remove the relevant messages + * from the subscription. + * + * Acknowledging a message whose ack deadline has expired may succeed, + * but such a message may be redelivered later. Acknowledging a message more + * than once will not result in an error. + * + * + * + * + * @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. + */ + public void acknowledge(String subscription, List ackIds) { + AcknowledgeRequest request = + AcknowledgeRequest.newBuilder().setSubscription(subscription).addAllAckIds(ackIds).build(); + + acknowledge(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Acknowledges the messages associated with the `ack_ids` in the + * `AcknowledgeRequest`. The Pub/Sub system can remove the relevant messages + * from the subscription. + * + * Acknowledging a message whose ack deadline has expired may succeed, + * but such a message may be redelivered later. Acknowledging a message more + * than once will not result in an error. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void acknowledge(AcknowledgeRequest request) { + acknowledgeCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Acknowledges the messages associated with the `ack_ids` in the + * `AcknowledgeRequest`. The Pub/Sub system can remove the relevant messages + * from the subscription. + * + * Acknowledging a message whose ack deadline has expired may succeed, + * but such a message may be redelivered later. Acknowledging a message more + * than once will not result in an error. + * + * + * + */ + public ApiCallable acknowledgeCallable() { + return acknowledgeCallable; + } + + // ----- pull ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Pulls messages from the server. Returns an empty list if there are no + * messages available in the backlog. The server may generate `UNAVAILABLE` if + * there are too many concurrent pull requests pending for the given + * subscription. + * + * + * + * + * @param subscription The subscription from which messages should be pulled. + * @param returnImmediately If this is specified as true the system will respond immediately even if + * it is not able to return a message in the `Pull` response. Otherwise the + * system is allowed to wait until at least one message is available rather + * 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. + */ + public PullResponse pull(String subscription, boolean returnImmediately, int maxMessages) { + PullRequest request = + PullRequest.newBuilder() + .setSubscription(subscription) + .setReturnImmediately(returnImmediately) + .setMaxMessages(maxMessages) + .build(); + + return pull(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Pulls messages from the server. Returns an empty list if there are no + * messages available in the backlog. The server may generate `UNAVAILABLE` if + * there are too many concurrent pull requests pending for the given + * subscription. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public PullResponse pull(PullRequest request) { + return pullCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Pulls messages from the server. Returns an empty list if there are no + * messages available in the backlog. The server may generate `UNAVAILABLE` if + * there are too many concurrent pull requests pending for the given + * subscription. + * + * + * + */ + public ApiCallable pullCallable() { + return pullCallable; + } + + // ----- modifyPushConfig ----- + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Modifies the `PushConfig` for a specified subscription. + * + * This may be used to change a push subscription to a pull one (signified by + * an empty `PushConfig`) or vice versa, or change the endpoint URL and other + * attributes of a push subscription. Messages will accumulate for delivery + * continuously through the call regardless of changes to the `PushConfig`. + * + * + * + * + * @param subscription The name of the subscription. + * @param pushConfig The push configuration for future deliveries. + * + * An empty `pushConfig` indicates that the Pub/Sub system should + * stop pushing messages from the given subscription and allow + * messages to be pulled and acknowledged - effectively pausing + * the subscription if `Pull` is not called. + */ + public void modifyPushConfig(String subscription, PushConfig pushConfig) { + ModifyPushConfigRequest request = + ModifyPushConfigRequest.newBuilder() + .setSubscription(subscription) + .setPushConfig(pushConfig) + .build(); + + modifyPushConfig(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Modifies the `PushConfig` for a specified subscription. + * + * This may be used to change a push subscription to a pull one (signified by + * an empty `PushConfig`) or vice versa, or change the endpoint URL and other + * attributes of a push subscription. Messages will accumulate for delivery + * continuously through the call regardless of changes to the `PushConfig`. + * + * + * + * + * @param request The request object containing all of the parameters for the API call. + */ + public void modifyPushConfig(ModifyPushConfigRequest request) { + modifyPushConfigCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. + /** + * Modifies the `PushConfig` for a specified subscription. + * + * This may be used to change a push subscription to a pull one (signified by + * an empty `PushConfig`) or vice versa, or change the endpoint URL and other + * attributes of a push subscription. Messages will accumulate for delivery + * continuously through the call regardless of changes to the `PushConfig`. + * + * + * + */ + public ApiCallable modifyPushConfigCallable() { + return modifyPushConfigCallable; + } + + // ======== + // Cleanup + // ======== + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + * + * + * + */ + @Override + public void close() { + // Manually-added shutdown code + + // Auto-generated shutdown code + channel.shutdown(); + + // Manually-added shutdown code + } + + // ======== + // Manually-added methods: add custom (non-generated) methods after this point. + // ======== + +} diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java new file mode 100644 index 000000000000..0d78a4b45fb8 --- /dev/null +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -0,0 +1,336 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ + +package com.google.gcloud.pubsub.spi; + +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.grpc.ApiCallSettings; +import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; +import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; +import com.google.api.gax.grpc.PageDescriptor; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.protobuf.Empty; +import com.google.pubsub.v1.AcknowledgeRequest; +import com.google.pubsub.v1.DeleteSubscriptionRequest; +import com.google.pubsub.v1.GetSubscriptionRequest; +import com.google.pubsub.v1.ListSubscriptionsRequest; +import com.google.pubsub.v1.ListSubscriptionsResponse; +import com.google.pubsub.v1.ModifyAckDeadlineRequest; +import com.google.pubsub.v1.ModifyPushConfigRequest; +import com.google.pubsub.v1.PullRequest; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.SubscriberGrpc; +import com.google.pubsub.v1.Subscription; +import io.grpc.Status; + +// Manually-added imports: add custom (non-generated) imports after this point. + +// AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +@javax.annotation.Generated("by GAPIC") +public class SubscriberSettings extends ApiCallSettings { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + public static ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder() + .add("https://www.googleapis.com/auth/pubsub") + .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 static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetryParams params = null; + params = + RetryParams.newBuilder() + .setRetryBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(100L) + .setDelayMultiplier(1.2) + .setMaxDelayMillis(1000L) + .build()) + .setTimeoutBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(300L) + .setDelayMultiplier(1.3) + .setMaxDelayMillis(3000L) + .build()) + .setTotalTimeout(30000L) + .build(); + definitions.put("default", params); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private final ApiCallableBuilder createSubscriptionMethod; + private final ApiCallableBuilder getSubscriptionMethod; + private final PageStreamingApiCallableBuilder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsMethod; + private final ApiCallableBuilder deleteSubscriptionMethod; + private final ApiCallableBuilder modifyAckDeadlineMethod; + private final ApiCallableBuilder acknowledgeMethod; + private final ApiCallableBuilder pullMethod; + private final ApiCallableBuilder modifyPushConfigMethod; + private final ImmutableList allMethods; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of SubscriberSettings with default settings. + * + * + * + */ + public static SubscriberSettings create() { + SubscriberSettings settings = new SubscriberSettings(); + settings.provideChannelWith( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + return settings; + } + + /** + * 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() { + createSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); + createSubscriptionMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + getSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); + getSubscriptionMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + listSubscriptionsMethod = + new PageStreamingApiCallableBuilder<>( + SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC); + listSubscriptionsMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + deleteSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); + deleteSubscriptionMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + modifyAckDeadlineMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); + modifyAckDeadlineMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + acknowledgeMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); + acknowledgeMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + pullMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_PULL); + pullMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + modifyPushConfigMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); + modifyPushConfigMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + allMethods = + ImmutableList.builder() + .add( + createSubscriptionMethod, + getSubscriptionMethod, + listSubscriptionsMethod, + deleteSubscriptionMethod, + modifyAckDeadlineMethod, + acknowledgeMethod, + pullMethod, + modifyPushConfigMethod) + .build(); + } + + /** + * Returns the ApiCallableBuilder for the API method createSubscription. + * + * + * + */ + public ApiCallableBuilder createSubscriptionMethod() { + return createSubscriptionMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method getSubscription. + * + * + * + */ + public ApiCallableBuilder getSubscriptionMethod() { + return getSubscriptionMethod; + } + + /** + * Returns the PageStreamingApiCallableBuilder for the API method listSubscriptions. + * + * + * + */ + public PageStreamingApiCallableBuilder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsMethod() { + return listSubscriptionsMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method deleteSubscription. + * + * + * + */ + public ApiCallableBuilder deleteSubscriptionMethod() { + return deleteSubscriptionMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method modifyAckDeadline. + * + * + * + */ + public ApiCallableBuilder modifyAckDeadlineMethod() { + return modifyAckDeadlineMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method acknowledge. + * + * + * + */ + public ApiCallableBuilder acknowledgeMethod() { + return acknowledgeMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method pull. + * + * + * + */ + public ApiCallableBuilder pullMethod() { + return pullMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method modifyPushConfig. + * + * + * + */ + public ApiCallableBuilder modifyPushConfigMethod() { + return modifyPushConfigMethod; + } + + public ImmutableList allMethods() { + return allMethods; + } + + private static PageDescriptor + LIST_SUBSCRIPTIONS_PAGE_STR_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListSubscriptionsRequest injectToken( + ListSubscriptionsRequest payload, Object token) { + return ListSubscriptionsRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListSubscriptionsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListSubscriptionsResponse payload) { + return payload.getSubscriptionsList(); + } + }; +} diff --git a/gcloud-java-pubsub/pom.xml b/gcloud-java-pubsub/pom.xml index d8a1f905633e..fb81bdbfa618 100644 --- a/gcloud-java-pubsub/pom.xml +++ b/gcloud-java-pubsub/pom.xml @@ -19,7 +19,7 @@ com.google.api gax - 0.0.2 + 0.0.3-SNAPSHOT com.google.api.grpc diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index 98ff96ce61dd..f7011dcd0240 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -30,21 +30,12 @@ * * Happy editing! */ + package com.google.gcloud.pubsub.spi; -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.grpc.ApiCallSettings; import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.PageDescriptor; -import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.protobuf.PathTemplate; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import com.google.protobuf.Empty; import com.google.pubsub.v1.DeleteTopicRequest; import com.google.pubsub.v1.GetTopicRequest; @@ -54,16 +45,13 @@ import com.google.pubsub.v1.ListTopicsResponse; import com.google.pubsub.v1.PublishRequest; import com.google.pubsub.v1.PublishResponse; -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.Closeable; import java.io.IOException; -import java.util.EnumMap; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; // Manually-added imports: add custom (non-generated) imports after this point. @@ -75,183 +63,13 @@ * * */ -@javax.annotation.Generated("by the veneer generator") +@javax.annotation.Generated("by GAPIC") public class PublisherApi implements AutoCloseable { - public enum MethodIdentifier { - CREATE_TOPIC, - PUBLISH, - GET_TOPIC, - LIST_TOPICS, - LIST_TOPIC_SUBSCRIPTIONS, - DELETE_TOPIC - } - // ========= // Constants // ========= - /** - * The default address of the service. - * - * - * - */ - public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com"; - - /** - * The default port of the service. - * - * - * - */ - public static final int DEFAULT_SERVICE_PORT = 443; - - /** - * The default scopes of the service. - */ - public static ImmutableList DEFAULT_SERVICE_SCOPES = - ImmutableList.builder() - .add("https://www.googleapis.com/auth/pubsub") - .add("https://www.googleapis.com/auth/cloud-platform") - .build(); - - /** - * The default settings for the service. - */ - public static ServiceApiSettings DEFAULT_SETTINGS = - ServiceApiSettings.builder() - .provideChannelWith( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()) - .build(); - - private static final ImmutableMap> - DEFAULT_RETRY_CONFIG; - - static { - Map> definition = new HashMap<>(); - definition.put( - "idempotent", - Sets.immutableEnumSet( - Lists.newArrayList( - Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); - definition.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); - - Map> retryableCodes = - new EnumMap<>(MethodIdentifier.class); - retryableCodes.put(MethodIdentifier.CREATE_TOPIC, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.PUBLISH, definition.get("non_idempotent")); - retryableCodes.put(MethodIdentifier.GET_TOPIC, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.LIST_TOPICS, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.LIST_TOPIC_SUBSCRIPTIONS, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.DELETE_TOPIC, definition.get("idempotent")); - DEFAULT_RETRY_CONFIG = - Maps.>immutableEnumMap(retryableCodes); - } - - private static final ImmutableMap DEFAULT_RETRY_PARAMS; - - static { - Map definition = new HashMap<>(); - RetryParams params = null; - params = - RetryParams.newBuilder() - .setRetryBackoff( - BackoffParams.newBuilder() - .setInitialDelayMillis(100L) - .setDelayMultiplier(1.2) - .setMaxDelayMillis(1000L) - .build()) - .setTimeoutBackoff( - BackoffParams.newBuilder() - .setInitialDelayMillis(300L) - .setDelayMultiplier(1.3) - .setMaxDelayMillis(3000L) - .build()) - .setTotalTimeout(30000L) - .build(); - definition.put("default", params); - - Map retryParams = new EnumMap<>(MethodIdentifier.class); - retryParams.put(MethodIdentifier.CREATE_TOPIC, definition.get("default")); - retryParams.put(MethodIdentifier.PUBLISH, definition.get("default")); - retryParams.put(MethodIdentifier.GET_TOPIC, definition.get("default")); - retryParams.put(MethodIdentifier.LIST_TOPICS, definition.get("default")); - retryParams.put(MethodIdentifier.LIST_TOPIC_SUBSCRIPTIONS, definition.get("default")); - retryParams.put(MethodIdentifier.DELETE_TOPIC, definition.get("default")); - DEFAULT_RETRY_PARAMS = Maps.immutableEnumMap(retryParams); - } - - private static final ApiCallable CREATE_TOPIC = - ApiCallable.create(PublisherGrpc.METHOD_CREATE_TOPIC); - private static final ApiCallable PUBLISH = - ApiCallable.create(PublisherGrpc.METHOD_PUBLISH); - private static final ApiCallable GET_TOPIC = - ApiCallable.create(PublisherGrpc.METHOD_GET_TOPIC); - private static final ApiCallable LIST_TOPICS = - ApiCallable.create(PublisherGrpc.METHOD_LIST_TOPICS); - private static final ApiCallable - LIST_TOPIC_SUBSCRIPTIONS = ApiCallable.create(PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS); - private static final ApiCallable DELETE_TOPIC = - ApiCallable.create(PublisherGrpc.METHOD_DELETE_TOPIC); - - private static PageDescriptor - LIST_TOPICS_PAGE_DESC = - new PageDescriptor() { - @Override - public Object emptyToken() { - return ""; - } - - @Override - public ListTopicsRequest injectToken(ListTopicsRequest payload, Object token) { - return ListTopicsRequest.newBuilder(payload).setPageToken((String) token).build(); - } - - @Override - public Object extractNextToken(ListTopicsResponse payload) { - return payload.getNextPageToken(); - } - - @Override - public Iterable extractResources(ListTopicsResponse payload) { - return payload.getTopicsList(); - } - }; - - private static PageDescriptor< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - LIST_TOPIC_SUBSCRIPTIONS_PAGE_DESC = - new PageDescriptor< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String>() { - @Override - public Object emptyToken() { - return ""; - } - - @Override - public ListTopicSubscriptionsRequest injectToken( - ListTopicSubscriptionsRequest payload, Object token) { - return ListTopicSubscriptionsRequest.newBuilder(payload) - .setPageToken((String) token) - .build(); - } - - @Override - public Object extractNextToken(ListTopicSubscriptionsResponse payload) { - return payload.getNextPageToken(); - } - - @Override - public Iterable extractResources(ListTopicSubscriptionsResponse payload) { - return payload.getSubscriptionsList(); - } - }; - /** * A PathTemplate representing the fully-qualified path to represent * a project resource. @@ -276,14 +94,30 @@ public Iterable extractResources(ListTopicSubscriptionsResponse payload) // ======== private final ManagedChannel channel; - private final ServiceApiSettings settings; - private final ImmutableMap> retryCodesConfig; - private final ImmutableMap retryParamsConfig; + private final List closeables = new ArrayList<>(); + + private final ApiCallable createTopicCallable; + private final ApiCallable publishCallable; + private final ApiCallable getTopicCallable; + private final ApiCallable listTopicsCallable; + private final ApiCallable> listTopicsIterableCallable; + private final ApiCallable + listTopicSubscriptionsCallable; + private final ApiCallable> + listTopicSubscriptionsIterableCallable; + private final ApiCallable deleteTopicCallable; // =============== // Factory Methods // =============== + /** + * Constructs an instance of PublisherSettings with default settings. + */ + public static PublisherSettings newSettings() { + return PublisherSettings.create(); + } + /** * Constructs an instance of PublisherApi with default settings. * @@ -291,7 +125,7 @@ public Iterable extractResources(ListTopicSubscriptionsResponse payload) * */ public static PublisherApi create() throws IOException { - return create(DEFAULT_SETTINGS); + return create(newSettings()); } /** @@ -301,8 +135,7 @@ public static PublisherApi create() throws IOException { * * */ - public static PublisherApi create(ServiceApiSettings settings) - throws IOException { + public static PublisherApi create(PublisherSettings settings) throws IOException { return new PublisherApi(settings); } @@ -313,20 +146,32 @@ public static PublisherApi create(ServiceApiSettings settings) * * */ - protected PublisherApi(ServiceApiSettings settings) throws IOException { - this.settings = settings; + protected PublisherApi(PublisherSettings settings) throws IOException { this.channel = settings.getChannel(); - Map> retryCodesConfig = - new EnumMap<>(DEFAULT_RETRY_CONFIG); - retryCodesConfig.putAll(settings.getRetryableCodes()); - this.retryCodesConfig = - Maps.>immutableEnumMap(retryCodesConfig); - - Map retryParamsConfig = new EnumMap<>(DEFAULT_RETRY_PARAMS); - retryParamsConfig.putAll(settings.getRetryParams()); - this.retryParamsConfig = - Maps.immutableEnumMap(retryParamsConfig); + for (ApiCallSettings method : settings.allMethods()) { + if (method.getExecutor() == null) { + method.setExecutor(settings.getExecutor()); + } + } + + this.createTopicCallable = settings.createTopicMethod().build(settings); + this.publishCallable = settings.publishMethod().build(settings); + 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.listTopicSubscriptionsIterableCallable = + settings.listTopicSubscriptionsMethod().buildPageStreaming(settings); + this.deleteTopicCallable = settings.deleteTopicMethod().build(settings); + + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); } // ============================== @@ -435,12 +280,7 @@ public Topic createTopic(Topic request) { * */ public ApiCallable createTopicCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.CREATE_TOPIC); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.CREATE_TOPIC); - return CREATE_TOPIC - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return createTopicCallable; } // ----- publish ----- @@ -489,12 +329,7 @@ public PublishResponse publish(PublishRequest request) { * */ public ApiCallable publishCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.PUBLISH); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.PUBLISH); - return PUBLISH - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return publishCallable; } // ----- getTopic ----- @@ -535,12 +370,7 @@ public Topic getTopic(GetTopicRequest request) { * */ public ApiCallable getTopicCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.GET_TOPIC); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.GET_TOPIC); - return GET_TOPIC - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return getTopicCallable; } // ----- listTopics ----- @@ -567,7 +397,7 @@ public Iterable listTopics(String project) { * @param request The request object containing all of the parameters for the API call. */ public Iterable listTopics(ListTopicsRequest request) { - return listTopicsStreamingCallable().call(request); + return listTopicsIterableCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -577,8 +407,8 @@ public Iterable listTopics(ListTopicsRequest request) { * * */ - public ApiCallable> listTopicsStreamingCallable() { - return listTopicsCallable().pageStreaming(LIST_TOPICS_PAGE_DESC); + public ApiCallable> listTopicsIterableCallable() { + return listTopicsIterableCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -589,12 +419,7 @@ public ApiCallable> listTopicsStreamingCallab * */ public ApiCallable listTopicsCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.LIST_TOPICS); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.LIST_TOPICS); - return LIST_TOPICS - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return listTopicsCallable; } // ----- listTopicSubscriptions ----- @@ -622,7 +447,7 @@ public Iterable listTopicSubscriptions(String topic) { * @param request The request object containing all of the parameters for the API call. */ public Iterable listTopicSubscriptions(ListTopicSubscriptionsRequest request) { - return listTopicSubscriptionsStreamingCallable().call(request); + return listTopicSubscriptionsIterableCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -633,8 +458,8 @@ public Iterable listTopicSubscriptions(ListTopicSubscriptionsRequest req * */ public ApiCallable> - listTopicSubscriptionsStreamingCallable() { - return listTopicSubscriptionsCallable().pageStreaming(LIST_TOPIC_SUBSCRIPTIONS_PAGE_DESC); + listTopicSubscriptionsIterableCallable() { + return listTopicSubscriptionsIterableCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -646,13 +471,7 @@ public Iterable listTopicSubscriptions(ListTopicSubscriptionsRequest req */ public ApiCallable listTopicSubscriptionsCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.LIST_TOPIC_SUBSCRIPTIONS); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.LIST_TOPIC_SUBSCRIPTIONS); - return LIST_TOPIC_SUBSCRIPTIONS - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return listTopicSubscriptionsCallable; } // ----- deleteTopic ----- @@ -705,12 +524,7 @@ public void deleteTopic(DeleteTopicRequest request) { * */ public ApiCallable deleteTopicCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.DELETE_TOPIC); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.DELETE_TOPIC); - return DELETE_TOPIC - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return deleteTopicCallable; } // ======== diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java new file mode 100644 index 000000000000..b1985fc40b53 --- /dev/null +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -0,0 +1,333 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ + +package com.google.gcloud.pubsub.spi; + +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.grpc.ApiCallSettings; +import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; +import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; +import com.google.api.gax.grpc.PageDescriptor; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.protobuf.Empty; +import com.google.pubsub.v1.DeleteTopicRequest; +import com.google.pubsub.v1.GetTopicRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsResponse; +import com.google.pubsub.v1.ListTopicsRequest; +import com.google.pubsub.v1.ListTopicsResponse; +import com.google.pubsub.v1.PublishRequest; +import com.google.pubsub.v1.PublishResponse; +import com.google.pubsub.v1.PublisherGrpc; +import com.google.pubsub.v1.Topic; +import io.grpc.Status; + +// Manually-added imports: add custom (non-generated) imports after this point. + +// AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +@javax.annotation.Generated("by GAPIC") +public class PublisherSettings extends ApiCallSettings { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + public static ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder() + .add("https://www.googleapis.com/auth/pubsub") + .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 static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetryParams params = null; + params = + RetryParams.newBuilder() + .setRetryBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(100L) + .setDelayMultiplier(1.2) + .setMaxDelayMillis(1000L) + .build()) + .setTimeoutBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(300L) + .setDelayMultiplier(1.3) + .setMaxDelayMillis(3000L) + .build()) + .setTotalTimeout(30000L) + .build(); + definitions.put("default", params); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private final ApiCallableBuilder createTopicMethod; + private final ApiCallableBuilder publishMethod; + private final ApiCallableBuilder getTopicMethod; + private final PageStreamingApiCallableBuilder + listTopicsMethod; + private final PageStreamingApiCallableBuilder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsMethod; + private final ApiCallableBuilder deleteTopicMethod; + private final ImmutableList allMethods; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of PublisherSettings with default settings. + * + * + * + */ + public static PublisherSettings create() { + PublisherSettings settings = new PublisherSettings(); + settings.provideChannelWith( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + return settings; + } + + /** + * 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() { + createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); + createTopicMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + publishMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_PUBLISH); + publishMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + getTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_GET_TOPIC); + getTopicMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicsMethod = + new PageStreamingApiCallableBuilder<>( + PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC); + listTopicsMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicSubscriptionsMethod = + new PageStreamingApiCallableBuilder<>( + PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); + listTopicSubscriptionsMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + deleteTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_DELETE_TOPIC); + deleteTopicMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + allMethods = + ImmutableList.builder() + .add( + createTopicMethod, + publishMethod, + getTopicMethod, + listTopicsMethod, + listTopicSubscriptionsMethod, + deleteTopicMethod) + .build(); + } + + /** + * Returns the ApiCallableBuilder for the API method createTopic. + * + * + * + */ + public ApiCallableBuilder createTopicMethod() { + return createTopicMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method publish. + * + * + * + */ + public ApiCallableBuilder publishMethod() { + return publishMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method getTopic. + * + * + * + */ + public ApiCallableBuilder getTopicMethod() { + return getTopicMethod; + } + + /** + * Returns the PageStreamingApiCallableBuilder for the API method listTopics. + * + * + * + */ + public PageStreamingApiCallableBuilder + listTopicsMethod() { + return listTopicsMethod; + } + + /** + * Returns the PageStreamingApiCallableBuilder for the API method listTopicSubscriptions. + * + * + * + */ + public PageStreamingApiCallableBuilder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsMethod() { + return listTopicSubscriptionsMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method deleteTopic. + * + * + * + */ + public ApiCallableBuilder deleteTopicMethod() { + return deleteTopicMethod; + } + + public ImmutableList allMethods() { + return allMethods; + } + + private static PageDescriptor + LIST_TOPICS_PAGE_STR_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListTopicsRequest injectToken(ListTopicsRequest payload, Object token) { + return ListTopicsRequest.newBuilder(payload).setPageToken((String) token).build(); + } + + @Override + public Object extractNextToken(ListTopicsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListTopicsResponse payload) { + return payload.getTopicsList(); + } + }; + + private static PageDescriptor< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC = + new PageDescriptor< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String>() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListTopicSubscriptionsRequest injectToken( + ListTopicSubscriptionsRequest payload, Object token) { + return ListTopicSubscriptionsRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListTopicSubscriptionsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListTopicSubscriptionsResponse payload) { + return payload.getSubscriptionsList(); + } + }; +} diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index 39f7a786e474..c50e6b6aaaed 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -30,21 +30,12 @@ * * Happy editing! */ + package com.google.gcloud.pubsub.spi; -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.grpc.ApiCallSettings; import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.grpc.PageDescriptor; -import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.protobuf.PathTemplate; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import com.google.protobuf.Empty; import com.google.pubsub.v1.AcknowledgeRequest; import com.google.pubsub.v1.DeleteSubscriptionRequest; @@ -56,15 +47,12 @@ import com.google.pubsub.v1.PullRequest; import com.google.pubsub.v1.PullResponse; import com.google.pubsub.v1.PushConfig; -import com.google.pubsub.v1.SubscriberGrpc; import com.google.pubsub.v1.Subscription; import io.grpc.ManagedChannel; -import io.grpc.Status; +import java.io.Closeable; import java.io.IOException; -import java.util.EnumMap; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; // Manually-added imports: add custom (non-generated) imports after this point. @@ -76,167 +64,13 @@ * * */ -@javax.annotation.Generated("by the veneer generator") +@javax.annotation.Generated("by GAPIC") public class SubscriberApi implements AutoCloseable { - public enum MethodIdentifier { - CREATE_SUBSCRIPTION, - GET_SUBSCRIPTION, - LIST_SUBSCRIPTIONS, - DELETE_SUBSCRIPTION, - MODIFY_ACK_DEADLINE, - ACKNOWLEDGE, - PULL, - MODIFY_PUSH_CONFIG - } - // ========= // Constants // ========= - /** - * The default address of the service. - * - * - * - */ - public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com"; - - /** - * The default port of the service. - * - * - * - */ - public static final int DEFAULT_SERVICE_PORT = 443; - - /** - * The default scopes of the service. - */ - public static ImmutableList DEFAULT_SERVICE_SCOPES = - ImmutableList.builder() - .add("https://www.googleapis.com/auth/pubsub") - .add("https://www.googleapis.com/auth/cloud-platform") - .build(); - - /** - * The default settings for the service. - */ - public static ServiceApiSettings DEFAULT_SETTINGS = - ServiceApiSettings.builder() - .provideChannelWith( - ConnectionSettings.builder() - .setServiceAddress(DEFAULT_SERVICE_ADDRESS) - .setPort(DEFAULT_SERVICE_PORT) - .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) - .build()) - .build(); - - private static final ImmutableMap> - DEFAULT_RETRY_CONFIG; - - static { - Map> definition = new HashMap<>(); - definition.put( - "idempotent", - Sets.immutableEnumSet( - Lists.newArrayList( - Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); - definition.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); - - Map> retryableCodes = - new EnumMap<>(MethodIdentifier.class); - retryableCodes.put(MethodIdentifier.CREATE_SUBSCRIPTION, definition.get("non_idempotent")); - retryableCodes.put(MethodIdentifier.GET_SUBSCRIPTION, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.LIST_SUBSCRIPTIONS, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.DELETE_SUBSCRIPTION, definition.get("idempotent")); - retryableCodes.put(MethodIdentifier.MODIFY_ACK_DEADLINE, definition.get("non_idempotent")); - retryableCodes.put(MethodIdentifier.ACKNOWLEDGE, definition.get("non_idempotent")); - retryableCodes.put(MethodIdentifier.PULL, definition.get("non_idempotent")); - retryableCodes.put(MethodIdentifier.MODIFY_PUSH_CONFIG, definition.get("non_idempotent")); - DEFAULT_RETRY_CONFIG = - Maps.>immutableEnumMap(retryableCodes); - } - - private static final ImmutableMap DEFAULT_RETRY_PARAMS; - - static { - Map definition = new HashMap<>(); - RetryParams params = null; - params = - RetryParams.newBuilder() - .setRetryBackoff( - BackoffParams.newBuilder() - .setInitialDelayMillis(100L) - .setDelayMultiplier(1.2) - .setMaxDelayMillis(1000L) - .build()) - .setTimeoutBackoff( - BackoffParams.newBuilder() - .setInitialDelayMillis(300L) - .setDelayMultiplier(1.3) - .setMaxDelayMillis(3000L) - .build()) - .setTotalTimeout(30000L) - .build(); - definition.put("default", params); - - Map retryParams = new EnumMap<>(MethodIdentifier.class); - retryParams.put(MethodIdentifier.CREATE_SUBSCRIPTION, definition.get("default")); - retryParams.put(MethodIdentifier.GET_SUBSCRIPTION, definition.get("default")); - retryParams.put(MethodIdentifier.LIST_SUBSCRIPTIONS, definition.get("default")); - retryParams.put(MethodIdentifier.DELETE_SUBSCRIPTION, definition.get("default")); - retryParams.put(MethodIdentifier.MODIFY_ACK_DEADLINE, definition.get("default")); - retryParams.put(MethodIdentifier.ACKNOWLEDGE, definition.get("default")); - retryParams.put(MethodIdentifier.PULL, definition.get("default")); - retryParams.put(MethodIdentifier.MODIFY_PUSH_CONFIG, definition.get("default")); - DEFAULT_RETRY_PARAMS = Maps.immutableEnumMap(retryParams); - } - - private static final ApiCallable CREATE_SUBSCRIPTION = - ApiCallable.create(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); - private static final ApiCallable GET_SUBSCRIPTION = - ApiCallable.create(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); - private static final ApiCallable - LIST_SUBSCRIPTIONS = ApiCallable.create(SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS); - private static final ApiCallable DELETE_SUBSCRIPTION = - ApiCallable.create(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); - private static final ApiCallable MODIFY_ACK_DEADLINE = - ApiCallable.create(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); - private static final ApiCallable ACKNOWLEDGE = - ApiCallable.create(SubscriberGrpc.METHOD_ACKNOWLEDGE); - private static final ApiCallable PULL = - ApiCallable.create(SubscriberGrpc.METHOD_PULL); - private static final ApiCallable MODIFY_PUSH_CONFIG = - ApiCallable.create(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); - - private static PageDescriptor - LIST_SUBSCRIPTIONS_PAGE_DESC = - new PageDescriptor() { - @Override - public Object emptyToken() { - return ""; - } - - @Override - public ListSubscriptionsRequest injectToken( - ListSubscriptionsRequest payload, Object token) { - return ListSubscriptionsRequest.newBuilder(payload) - .setPageToken((String) token) - .build(); - } - - @Override - public Object extractNextToken(ListSubscriptionsResponse payload) { - return payload.getNextPageToken(); - } - - @Override - public Iterable extractResources(ListSubscriptionsResponse payload) { - return payload.getSubscriptionsList(); - } - }; - /** * A PathTemplate representing the fully-qualified path to represent * a project resource. @@ -261,14 +95,31 @@ public Iterable extractResources(ListSubscriptionsResponse payload // ======== private final ManagedChannel channel; - private final ServiceApiSettings settings; - private final ImmutableMap> retryCodesConfig; - private final ImmutableMap retryParamsConfig; + private final List closeables = new ArrayList<>(); + + private final ApiCallable createSubscriptionCallable; + private final ApiCallable getSubscriptionCallable; + private final ApiCallable + listSubscriptionsCallable; + private final ApiCallable> + listSubscriptionsIterableCallable; + private final ApiCallable deleteSubscriptionCallable; + private final ApiCallable modifyAckDeadlineCallable; + private final ApiCallable acknowledgeCallable; + private final ApiCallable pullCallable; + private final ApiCallable modifyPushConfigCallable; // =============== // Factory Methods // =============== + /** + * Constructs an instance of SubscriberSettings with default settings. + */ + public static SubscriberSettings newSettings() { + return SubscriberSettings.create(); + } + /** * Constructs an instance of SubscriberApi with default settings. * @@ -276,7 +127,7 @@ public Iterable extractResources(ListSubscriptionsResponse payload * */ public static SubscriberApi create() throws IOException { - return create(DEFAULT_SETTINGS); + return create(newSettings()); } /** @@ -286,8 +137,7 @@ public static SubscriberApi create() throws IOException { * * */ - public static SubscriberApi create(ServiceApiSettings settings) - throws IOException { + public static SubscriberApi create(SubscriberSettings settings) throws IOException { return new SubscriberApi(settings); } @@ -298,20 +148,33 @@ public static SubscriberApi create(ServiceApiSettings settings * * */ - protected SubscriberApi(ServiceApiSettings settings) throws IOException { - this.settings = settings; + protected SubscriberApi(SubscriberSettings settings) throws IOException { this.channel = settings.getChannel(); - Map> retryCodesConfig = - new EnumMap<>(DEFAULT_RETRY_CONFIG); - retryCodesConfig.putAll(settings.getRetryableCodes()); - this.retryCodesConfig = - Maps.>immutableEnumMap(retryCodesConfig); - - Map retryParamsConfig = new EnumMap<>(DEFAULT_RETRY_PARAMS); - retryParamsConfig.putAll(settings.getRetryParams()); - this.retryParamsConfig = - Maps.immutableEnumMap(retryParamsConfig); + for (ApiCallSettings method : settings.allMethods()) { + if (method.getExecutor() == null) { + method.setExecutor(settings.getExecutor()); + } + } + + this.createSubscriptionCallable = settings.createSubscriptionMethod().build(settings); + this.getSubscriptionCallable = settings.getSubscriptionMethod().build(settings); + this.listSubscriptionsCallable = settings.listSubscriptionsMethod().build(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); + + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); } // ============================== @@ -464,13 +327,7 @@ public Subscription createSubscription(Subscription request) { * */ public ApiCallable createSubscriptionCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.CREATE_SUBSCRIPTION); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.CREATE_SUBSCRIPTION); - return CREATE_SUBSCRIPTION - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return createSubscriptionCallable; } // ----- getSubscription ----- @@ -521,13 +378,7 @@ public Subscription getSubscription(GetSubscriptionRequest request) { * */ public ApiCallable getSubscriptionCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.GET_SUBSCRIPTION); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.GET_SUBSCRIPTION); - return GET_SUBSCRIPTION - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return getSubscriptionCallable; } // ----- listSubscriptions ----- @@ -561,7 +412,7 @@ public Iterable listSubscriptions(String project) { * @param request The request object containing all of the parameters for the API call. */ public Iterable listSubscriptions(ListSubscriptionsRequest request) { - return listSubscriptionsStreamingCallable().call(request); + return listSubscriptionsIterableCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -575,8 +426,8 @@ public Iterable listSubscriptions(ListSubscriptionsRequest request * */ public ApiCallable> - listSubscriptionsStreamingCallable() { - return listSubscriptionsCallable().pageStreaming(LIST_SUBSCRIPTIONS_PAGE_DESC); + listSubscriptionsIterableCallable() { + return listSubscriptionsIterableCallable; } // AUTO-GENERATED DOCUMENTATION AND METHOD - see instructions at the top of the file for editing. @@ -591,13 +442,7 @@ public Iterable listSubscriptions(ListSubscriptionsRequest request */ public ApiCallable listSubscriptionsCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.LIST_SUBSCRIPTIONS); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.LIST_SUBSCRIPTIONS); - return LIST_SUBSCRIPTIONS - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return listSubscriptionsCallable; } // ----- deleteSubscription ----- @@ -651,13 +496,7 @@ public void deleteSubscription(DeleteSubscriptionRequest request) { * */ public ApiCallable deleteSubscriptionCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.DELETE_SUBSCRIPTION); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.DELETE_SUBSCRIPTION); - return DELETE_SUBSCRIPTION - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return deleteSubscriptionCallable; } // ----- modifyAckDeadline ----- @@ -675,7 +514,7 @@ public ApiCallable deleteSubscriptionCallable( * @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. @@ -718,13 +557,7 @@ public void modifyAckDeadline(ModifyAckDeadlineRequest request) { * */ public ApiCallable modifyAckDeadlineCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.MODIFY_ACK_DEADLINE); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.MODIFY_ACK_DEADLINE); - return MODIFY_ACK_DEADLINE - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return modifyAckDeadlineCallable; } // ----- acknowledge ----- @@ -786,12 +619,7 @@ public void acknowledge(AcknowledgeRequest request) { * */ public ApiCallable acknowledgeCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.ACKNOWLEDGE); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.ACKNOWLEDGE); - return ACKNOWLEDGE - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return acknowledgeCallable; } // ----- pull ----- @@ -852,11 +680,7 @@ public PullResponse pull(PullRequest request) { * */ public ApiCallable pullCallable() { - ImmutableSet retryableCodes = retryCodesConfig.get(MethodIdentifier.PULL); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.PULL); - return PULL.retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return pullCallable; } // ----- modifyPushConfig ----- @@ -922,13 +746,7 @@ public void modifyPushConfig(ModifyPushConfigRequest request) { * */ public ApiCallable modifyPushConfigCallable() { - ImmutableSet retryableCodes = - retryCodesConfig.get(MethodIdentifier.MODIFY_PUSH_CONFIG); - RetryParams retryParams = retryParamsConfig.get(MethodIdentifier.MODIFY_PUSH_CONFIG); - return MODIFY_PUSH_CONFIG - .retryableOn(retryableCodes) - .retrying(retryParams, settings.getExecutor()) - .bind(channel); + return modifyPushConfigCallable; } // ======== diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java new file mode 100644 index 000000000000..0d78a4b45fb8 --- /dev/null +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -0,0 +1,336 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/google/googleapis/blob/master/google/pubsub/v1/pubsub.proto + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google engineers. + * Manual additions are allowed because the refresh process performs + * a 3-way merge in order to preserve those manual additions. In order to not + * break the refresh process, only certain types of modifications are + * allowed. + * + * Allowed modifications - currently these are the only types allowed: + * 1. New methods (these should be added to the end of the class) + * 2. New imports + * 3. Additional documentation between "manual edit" demarcations + * + * Happy editing! + */ + +package com.google.gcloud.pubsub.spi; + +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.grpc.ApiCallSettings; +import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; +import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; +import com.google.api.gax.grpc.PageDescriptor; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.protobuf.Empty; +import com.google.pubsub.v1.AcknowledgeRequest; +import com.google.pubsub.v1.DeleteSubscriptionRequest; +import com.google.pubsub.v1.GetSubscriptionRequest; +import com.google.pubsub.v1.ListSubscriptionsRequest; +import com.google.pubsub.v1.ListSubscriptionsResponse; +import com.google.pubsub.v1.ModifyAckDeadlineRequest; +import com.google.pubsub.v1.ModifyPushConfigRequest; +import com.google.pubsub.v1.PullRequest; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.SubscriberGrpc; +import com.google.pubsub.v1.Subscription; +import io.grpc.Status; + +// Manually-added imports: add custom (non-generated) imports after this point. + +// AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. +@javax.annotation.Generated("by GAPIC") +public class SubscriberSettings extends ApiCallSettings { + + // ========= + // Constants + // ========= + + /** + * The default address of the service. + * + * + * + */ + public static final String DEFAULT_SERVICE_ADDRESS = "pubsub-experimental.googleapis.com"; + + /** + * The default port of the service. + * + * + * + */ + public static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + public static ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder() + .add("https://www.googleapis.com/auth/pubsub") + .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 static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetryParams params = null; + params = + RetryParams.newBuilder() + .setRetryBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(100L) + .setDelayMultiplier(1.2) + .setMaxDelayMillis(1000L) + .build()) + .setTimeoutBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(300L) + .setDelayMultiplier(1.3) + .setMaxDelayMillis(3000L) + .build()) + .setTotalTimeout(30000L) + .build(); + definitions.put("default", params); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private final ApiCallableBuilder createSubscriptionMethod; + private final ApiCallableBuilder getSubscriptionMethod; + private final PageStreamingApiCallableBuilder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsMethod; + private final ApiCallableBuilder deleteSubscriptionMethod; + private final ApiCallableBuilder modifyAckDeadlineMethod; + private final ApiCallableBuilder acknowledgeMethod; + private final ApiCallableBuilder pullMethod; + private final ApiCallableBuilder modifyPushConfigMethod; + private final ImmutableList allMethods; + + // =============== + // Factory Methods + // =============== + + /** + * Constructs an instance of SubscriberSettings with default settings. + * + * + * + */ + public static SubscriberSettings create() { + SubscriberSettings settings = new SubscriberSettings(); + settings.provideChannelWith( + ConnectionSettings.builder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build()); + return settings; + } + + /** + * 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() { + createSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); + createSubscriptionMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + getSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); + getSubscriptionMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + listSubscriptionsMethod = + new PageStreamingApiCallableBuilder<>( + SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC); + listSubscriptionsMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + deleteSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); + deleteSubscriptionMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + modifyAckDeadlineMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); + modifyAckDeadlineMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + acknowledgeMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); + acknowledgeMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + pullMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_PULL); + pullMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + modifyPushConfigMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); + modifyPushConfigMethod.setRetryDefaults( + RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); + + allMethods = + ImmutableList.builder() + .add( + createSubscriptionMethod, + getSubscriptionMethod, + listSubscriptionsMethod, + deleteSubscriptionMethod, + modifyAckDeadlineMethod, + acknowledgeMethod, + pullMethod, + modifyPushConfigMethod) + .build(); + } + + /** + * Returns the ApiCallableBuilder for the API method createSubscription. + * + * + * + */ + public ApiCallableBuilder createSubscriptionMethod() { + return createSubscriptionMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method getSubscription. + * + * + * + */ + public ApiCallableBuilder getSubscriptionMethod() { + return getSubscriptionMethod; + } + + /** + * Returns the PageStreamingApiCallableBuilder for the API method listSubscriptions. + * + * + * + */ + public PageStreamingApiCallableBuilder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsMethod() { + return listSubscriptionsMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method deleteSubscription. + * + * + * + */ + public ApiCallableBuilder deleteSubscriptionMethod() { + return deleteSubscriptionMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method modifyAckDeadline. + * + * + * + */ + public ApiCallableBuilder modifyAckDeadlineMethod() { + return modifyAckDeadlineMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method acknowledge. + * + * + * + */ + public ApiCallableBuilder acknowledgeMethod() { + return acknowledgeMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method pull. + * + * + * + */ + public ApiCallableBuilder pullMethod() { + return pullMethod; + } + + /** + * Returns the ApiCallableBuilder for the API method modifyPushConfig. + * + * + * + */ + public ApiCallableBuilder modifyPushConfigMethod() { + return modifyPushConfigMethod; + } + + public ImmutableList allMethods() { + return allMethods; + } + + private static PageDescriptor + LIST_SUBSCRIPTIONS_PAGE_STR_DESC = + new PageDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListSubscriptionsRequest injectToken( + ListSubscriptionsRequest payload, Object token) { + return ListSubscriptionsRequest.newBuilder(payload) + .setPageToken((String) token) + .build(); + } + + @Override + public Object extractNextToken(ListSubscriptionsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListSubscriptionsResponse payload) { + return payload.getSubscriptionsList(); + } + }; +} diff --git a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java index 05a2f2acb9e2..79546855b3c3 100644 --- a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java +++ b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java @@ -21,15 +21,6 @@ import com.google.pubsub.v1.PushConfig; import com.google.pubsub.v1.Topic; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.google.api.gax.grpc.ServiceApiSettings; - import io.grpc.ManagedChannel; import java.io.IOException; @@ -37,6 +28,13 @@ import java.util.Collections; import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + public class PublisherApiTest { private static LocalPubsubHelper pubsubHelper; private PublisherApi publisherApi; @@ -57,16 +55,13 @@ public static void stopServer() throws IOException, InterruptedException { public void setUp() throws Exception { ManagedChannel channel = pubsubHelper.createChannel(); - publisherApi = - PublisherApi.create( - ServiceApiSettings.builder() - .provideChannelWith(channel) - .build()); - subscriberApi = - SubscriberApi.create( - ServiceApiSettings.builder() - .provideChannelWith(channel) - .build()); + PublisherSettings publisherSettings = PublisherApi.newSettings(); + publisherSettings.provideChannelWith(channel); + publisherApi = PublisherApi.create(publisherSettings); + + SubscriberSettings subscriberSettings = SubscriberApi.newSettings(); + subscriberSettings.provideChannelWith(channel); + subscriberApi = SubscriberApi.create(subscriberSettings); } @After From e85e69f7bc2d67eb5896d3fc823e95868d9ec5b1 Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Tue, 8 Mar 2016 11:33:27 -0800 Subject: [PATCH 2/7] Updating to GAX 0.0.3 --- gcloud-java-pubsub/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcloud-java-pubsub/pom.xml b/gcloud-java-pubsub/pom.xml index fb81bdbfa618..64d2bca4e58e 100644 --- a/gcloud-java-pubsub/pom.xml +++ b/gcloud-java-pubsub/pom.xml @@ -19,7 +19,7 @@ com.google.api gax - 0.0.3-SNAPSHOT + 0.0.3 com.google.api.grpc From 6a1f788d753c01b145f47e8369bde88a52056342 Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Wed, 9 Mar 2016 17:47:15 -0800 Subject: [PATCH 3/7] Surface updates from internal review --- .../gcloud/pubsub/spi/PublisherApi.java | 184 +++++++++--------- .../gcloud/pubsub/spi/PublisherSettings.java | 2 +- .../gcloud/pubsub/spi/SubscriberApi.java | 183 ++++++++--------- .../gcloud/pubsub/spi/SubscriberSettings.java | 2 +- .../gcloud/pubsub/spi/PublisherApi.java | 184 +++++++++--------- .../gcloud/pubsub/spi/PublisherSettings.java | 2 +- .../gcloud/pubsub/spi/SubscriberApi.java | 183 ++++++++--------- .../gcloud/pubsub/spi/SubscriberSettings.java | 2 +- .../spi/testing/LocalPublisherImpl.java | 4 +- .../gcloud/pubsub/spi/PublisherApiTest.java | 20 +- 10 files changed, 388 insertions(+), 378 deletions(-) diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index f7011dcd0240..3aa8bb6cae92 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -57,7 +57,7 @@ // AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. /** - * The service that an application uses to manipulate topics, and to send + * Service Description: The service that an application uses to manipulate topics, and to send * messages to a topic. * * @@ -66,28 +66,92 @@ @javax.annotation.Generated("by GAPIC") public class PublisherApi implements AutoCloseable { - // ========= - // Constants - // ========= + public static class ResourceNames { + private ResourceNames() {} + + // ======================= + // ResourceNames Constants + // ======================= + + /** + * 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}"); + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + * + * + * + */ + public static final String formatProjectPath(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } - /** - * 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}"); + /** + * Formats a string containing the fully-qualified path to represent + * a topic resource. + * + * + * + */ + public static final String formatTopicPath(String project, String topic) { + return TOPIC_PATH_TEMPLATE.instantiate("project", project, "topic", topic); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + * + * + * + */ + public static final String parseProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a topic resource. + * + * + * + */ + public static final String parseProjectFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project"); + } + + /** + * Parses the topic from the given fully-qualified path which + * represents a topic resource. + * + * + * + */ + public static final String parseTopicFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic"); + } + } // ======== // Members @@ -174,65 +238,6 @@ public void close() throws IOException { }); } - // ============================== - // Resource Name Helper Functions - // ============================== - - /** - * Creates a string containing the fully-qualified path to represent - * a project resource. - * - * - * - */ - public static final String createProjectPath(String project) { - return PROJECT_PATH_TEMPLATE.instantiate("project", project); - } - - /** - * Creates a string containing the fully-qualified path to represent - * a topic resource. - * - * - * - */ - public static final String createTopicPath(String project, String topic) { - return TOPIC_PATH_TEMPLATE.instantiate("project", project, "topic", topic); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a project resource. - * - * - * - */ - public static final String extractProjectFromProjectPath(String projectPath) { - return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a topic resource. - * - * - * - */ - public static final String extractProjectFromTopicPath(String topicPath) { - return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project"); - } - - /** - * Extracts the topic from the given fully-qualified path which - * represents a topic resource. - * - * - * - */ - public static final String extractTopicFromTopicPath(String topicPath) { - return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic"); - } - // ============= // Service Calls // ============= @@ -268,7 +273,7 @@ public Topic createTopic(String name) { * * @param request The request object containing all of the parameters for the API call. */ - public Topic createTopic(Topic request) { + private Topic createTopic(Topic request) { return createTopicCallable().call(request); } @@ -358,7 +363,7 @@ public Topic getTopic(String topic) { * * @param request The request object containing all of the parameters for the API call. */ - public Topic getTopic(GetTopicRequest request) { + private Topic getTopic(GetTopicRequest request) { return getTopicCallable().call(request); } @@ -508,7 +513,7 @@ public void deleteTopic(String topic) { * * @param request The request object containing all of the parameters for the API call. */ - public void deleteTopic(DeleteTopicRequest request) { + private void deleteTopic(DeleteTopicRequest request) { deleteTopicCallable().call(request); } @@ -539,13 +544,10 @@ public ApiCallable deleteTopicCallable() { * */ @Override - public void close() { - // Manually-added shutdown code - - // Auto-generated shutdown code - channel.shutdown(); - - // Manually-added shutdown code + public void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } } // ======== diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java index b1985fc40b53..f10a7b7357f3 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -87,7 +87,7 @@ public class PublisherSettings extends ApiCallSettings { /** * The default scopes of the service. */ - public static ImmutableList DEFAULT_SERVICE_SCOPES = + public static final ImmutableList DEFAULT_SERVICE_SCOPES = ImmutableList.builder() .add("https://www.googleapis.com/auth/pubsub") .add("https://www.googleapis.com/auth/cloud-platform") diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index c50e6b6aaaed..5b8dd746bdda 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -58,7 +58,7 @@ // AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. /** - * The service that an application uses to manipulate subscriptions and to + * Service Description: The service that an application uses to manipulate subscriptions and to * consume messages from a subscription via the `Pull` method. * * @@ -67,28 +67,93 @@ @javax.annotation.Generated("by GAPIC") public class SubscriberApi implements AutoCloseable { - // ========= - // Constants - // ========= + public static class ResourceNames { + private ResourceNames() {} + + // ======================= + // ResourceNames Constants + // ======================= + + /** + * 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}"); + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + * + * + * + */ + public static final String formatProjectPath(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } - /** - * 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}"); + /** + * Formats a string containing the fully-qualified path to represent + * a subscription resource. + * + * + * + */ + public static final String formatSubscriptionPath(String project, String subscription) { + return SUBSCRIPTION_PATH_TEMPLATE.instantiate( + "project", project, "subscription", subscription); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + * + * + * + */ + public static final String parseProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a subscription resource. + * + * + * + */ + public static final String parseProjectFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project"); + } + + /** + * Parses the subscription from the given fully-qualified path which + * represents a subscription resource. + * + * + * + */ + public static final String parseSubscriptionFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription"); + } + } // ======== // Members @@ -177,65 +242,6 @@ public void close() throws IOException { }); } - // ============================== - // Resource Name Helper Functions - // ============================== - - /** - * Creates a string containing the fully-qualified path to represent - * a project resource. - * - * - * - */ - public static final String createProjectPath(String project) { - return PROJECT_PATH_TEMPLATE.instantiate("project", project); - } - - /** - * Creates a string containing the fully-qualified path to represent - * a subscription resource. - * - * - * - */ - public static final String createSubscriptionPath(String project, String subscription) { - return SUBSCRIPTION_PATH_TEMPLATE.instantiate("project", project, "subscription", subscription); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a project resource. - * - * - * - */ - public static final String extractProjectFromProjectPath(String projectPath) { - return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a subscription resource. - * - * - * - */ - public static final String extractProjectFromSubscriptionPath(String subscriptionPath) { - return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project"); - } - - /** - * Extracts the subscription from the given fully-qualified path which - * represents a subscription resource. - * - * - * - */ - public static final String extractSubscriptionFromSubscriptionPath(String subscriptionPath) { - return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription"); - } - // ============= // Service Calls // ============= @@ -363,7 +369,7 @@ public Subscription getSubscription(String subscription) { * * @param request The request object containing all of the parameters for the API call. */ - public Subscription getSubscription(GetSubscriptionRequest request) { + private Subscription getSubscription(GetSubscriptionRequest request) { return getSubscriptionCallable().call(request); } @@ -480,7 +486,7 @@ public void deleteSubscription(String subscription) { * * @param request The request object containing all of the parameters for the API call. */ - public void deleteSubscription(DeleteSubscriptionRequest request) { + private void deleteSubscription(DeleteSubscriptionRequest request) { deleteSubscriptionCallable().call(request); } @@ -761,13 +767,10 @@ public ApiCallable modifyPushConfigCallable() { * */ @Override - public void close() { - // Manually-added shutdown code - - // Auto-generated shutdown code - channel.shutdown(); - - // Manually-added shutdown code + public void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } } // ======== diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java index 0d78a4b45fb8..e5cfd1d4a9b1 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -88,7 +88,7 @@ public class SubscriberSettings extends ApiCallSettings { /** * The default scopes of the service. */ - public static ImmutableList DEFAULT_SERVICE_SCOPES = + public static final ImmutableList DEFAULT_SERVICE_SCOPES = ImmutableList.builder() .add("https://www.googleapis.com/auth/pubsub") .add("https://www.googleapis.com/auth/cloud-platform") diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index f7011dcd0240..3aa8bb6cae92 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -57,7 +57,7 @@ // AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. /** - * The service that an application uses to manipulate topics, and to send + * Service Description: The service that an application uses to manipulate topics, and to send * messages to a topic. * * @@ -66,28 +66,92 @@ @javax.annotation.Generated("by GAPIC") public class PublisherApi implements AutoCloseable { - // ========= - // Constants - // ========= + public static class ResourceNames { + private ResourceNames() {} + + // ======================= + // ResourceNames Constants + // ======================= + + /** + * 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}"); + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + * + * + * + */ + public static final String formatProjectPath(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } - /** - * 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}"); + /** + * Formats a string containing the fully-qualified path to represent + * a topic resource. + * + * + * + */ + public static final String formatTopicPath(String project, String topic) { + return TOPIC_PATH_TEMPLATE.instantiate("project", project, "topic", topic); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + * + * + * + */ + public static final String parseProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a topic resource. + * + * + * + */ + public static final String parseProjectFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project"); + } + + /** + * Parses the topic from the given fully-qualified path which + * represents a topic resource. + * + * + * + */ + public static final String parseTopicFromTopicPath(String topicPath) { + return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic"); + } + } // ======== // Members @@ -174,65 +238,6 @@ public void close() throws IOException { }); } - // ============================== - // Resource Name Helper Functions - // ============================== - - /** - * Creates a string containing the fully-qualified path to represent - * a project resource. - * - * - * - */ - public static final String createProjectPath(String project) { - return PROJECT_PATH_TEMPLATE.instantiate("project", project); - } - - /** - * Creates a string containing the fully-qualified path to represent - * a topic resource. - * - * - * - */ - public static final String createTopicPath(String project, String topic) { - return TOPIC_PATH_TEMPLATE.instantiate("project", project, "topic", topic); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a project resource. - * - * - * - */ - public static final String extractProjectFromProjectPath(String projectPath) { - return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a topic resource. - * - * - * - */ - public static final String extractProjectFromTopicPath(String topicPath) { - return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project"); - } - - /** - * Extracts the topic from the given fully-qualified path which - * represents a topic resource. - * - * - * - */ - public static final String extractTopicFromTopicPath(String topicPath) { - return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic"); - } - // ============= // Service Calls // ============= @@ -268,7 +273,7 @@ public Topic createTopic(String name) { * * @param request The request object containing all of the parameters for the API call. */ - public Topic createTopic(Topic request) { + private Topic createTopic(Topic request) { return createTopicCallable().call(request); } @@ -358,7 +363,7 @@ public Topic getTopic(String topic) { * * @param request The request object containing all of the parameters for the API call. */ - public Topic getTopic(GetTopicRequest request) { + private Topic getTopic(GetTopicRequest request) { return getTopicCallable().call(request); } @@ -508,7 +513,7 @@ public void deleteTopic(String topic) { * * @param request The request object containing all of the parameters for the API call. */ - public void deleteTopic(DeleteTopicRequest request) { + private void deleteTopic(DeleteTopicRequest request) { deleteTopicCallable().call(request); } @@ -539,13 +544,10 @@ public ApiCallable deleteTopicCallable() { * */ @Override - public void close() { - // Manually-added shutdown code - - // Auto-generated shutdown code - channel.shutdown(); - - // Manually-added shutdown code + public void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } } // ======== diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java index b1985fc40b53..f10a7b7357f3 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -87,7 +87,7 @@ public class PublisherSettings extends ApiCallSettings { /** * The default scopes of the service. */ - public static ImmutableList DEFAULT_SERVICE_SCOPES = + public static final ImmutableList DEFAULT_SERVICE_SCOPES = ImmutableList.builder() .add("https://www.googleapis.com/auth/pubsub") .add("https://www.googleapis.com/auth/cloud-platform") diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index c50e6b6aaaed..5b8dd746bdda 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -58,7 +58,7 @@ // AUTO-GENERATED DOCUMENTATION AND SERVICE - see instructions at the top of the file for editing. /** - * The service that an application uses to manipulate subscriptions and to + * Service Description: The service that an application uses to manipulate subscriptions and to * consume messages from a subscription via the `Pull` method. * * @@ -67,28 +67,93 @@ @javax.annotation.Generated("by GAPIC") public class SubscriberApi implements AutoCloseable { - // ========= - // Constants - // ========= + public static class ResourceNames { + private ResourceNames() {} + + // ======================= + // ResourceNames Constants + // ======================= + + /** + * 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}"); + + // ============================== + // Resource Name Helper Functions + // ============================== + + /** + * Formats a string containing the fully-qualified path to represent + * a project resource. + * + * + * + */ + public static final String formatProjectPath(String project) { + return PROJECT_PATH_TEMPLATE.instantiate("project", project); + } - /** - * 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}"); + /** + * Formats a string containing the fully-qualified path to represent + * a subscription resource. + * + * + * + */ + public static final String formatSubscriptionPath(String project, String subscription) { + return SUBSCRIPTION_PATH_TEMPLATE.instantiate( + "project", project, "subscription", subscription); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a project resource. + * + * + * + */ + public static final String parseProjectFromProjectPath(String projectPath) { + return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); + } + + /** + * Parses the project from the given fully-qualified path which + * represents a subscription resource. + * + * + * + */ + public static final String parseProjectFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project"); + } + + /** + * Parses the subscription from the given fully-qualified path which + * represents a subscription resource. + * + * + * + */ + public static final String parseSubscriptionFromSubscriptionPath(String subscriptionPath) { + return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription"); + } + } // ======== // Members @@ -177,65 +242,6 @@ public void close() throws IOException { }); } - // ============================== - // Resource Name Helper Functions - // ============================== - - /** - * Creates a string containing the fully-qualified path to represent - * a project resource. - * - * - * - */ - public static final String createProjectPath(String project) { - return PROJECT_PATH_TEMPLATE.instantiate("project", project); - } - - /** - * Creates a string containing the fully-qualified path to represent - * a subscription resource. - * - * - * - */ - public static final String createSubscriptionPath(String project, String subscription) { - return SUBSCRIPTION_PATH_TEMPLATE.instantiate("project", project, "subscription", subscription); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a project resource. - * - * - * - */ - public static final String extractProjectFromProjectPath(String projectPath) { - return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project"); - } - - /** - * Extracts the project from the given fully-qualified path which - * represents a subscription resource. - * - * - * - */ - public static final String extractProjectFromSubscriptionPath(String subscriptionPath) { - return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project"); - } - - /** - * Extracts the subscription from the given fully-qualified path which - * represents a subscription resource. - * - * - * - */ - public static final String extractSubscriptionFromSubscriptionPath(String subscriptionPath) { - return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription"); - } - // ============= // Service Calls // ============= @@ -363,7 +369,7 @@ public Subscription getSubscription(String subscription) { * * @param request The request object containing all of the parameters for the API call. */ - public Subscription getSubscription(GetSubscriptionRequest request) { + private Subscription getSubscription(GetSubscriptionRequest request) { return getSubscriptionCallable().call(request); } @@ -480,7 +486,7 @@ public void deleteSubscription(String subscription) { * * @param request The request object containing all of the parameters for the API call. */ - public void deleteSubscription(DeleteSubscriptionRequest request) { + private void deleteSubscription(DeleteSubscriptionRequest request) { deleteSubscriptionCallable().call(request); } @@ -761,13 +767,10 @@ public ApiCallable modifyPushConfigCallable() { * */ @Override - public void close() { - // Manually-added shutdown code - - // Auto-generated shutdown code - channel.shutdown(); - - // Manually-added shutdown code + public void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } } // ======== diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java index 0d78a4b45fb8..e5cfd1d4a9b1 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -88,7 +88,7 @@ public class SubscriberSettings extends ApiCallSettings { /** * The default scopes of the service. */ - public static ImmutableList DEFAULT_SERVICE_SCOPES = + public static final ImmutableList DEFAULT_SERVICE_SCOPES = ImmutableList.builder() .add("https://www.googleapis.com/auth/pubsub") .add("https://www.googleapis.com/auth/cloud-platform") diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java index 45c5dc947d4d..13aeb26cafe0 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/testing/LocalPublisherImpl.java @@ -78,8 +78,8 @@ public void listTopics( ListTopicsRequest request, StreamObserver responseObserver) { List responseTopics = new ArrayList<>(); for (String topicName : topics.keySet()) { - String projectOfTopic = PublisherApi.extractProjectFromTopicPath(topicName); - String projectOfRequest = PublisherApi.extractProjectFromProjectPath(request.getProject()); + String projectOfTopic = PublisherApi.ResourceNames.parseProjectFromTopicPath(topicName); + String projectOfRequest = PublisherApi.ResourceNames.parseProjectFromProjectPath(request.getProject()); if (projectOfTopic.equals(projectOfRequest)) { Topic topicObj = Topic.newBuilder().setName(topicName).build(); responseTopics.add(topicObj); diff --git a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java index 79546855b3c3..4b4c91ebe797 100644 --- a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java +++ b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java @@ -77,17 +77,17 @@ public void tearDown() throws Exception { @Test public void testCreateTopic() throws Exception { - String topicName = PublisherApi.createTopicPath("my-project", "my-topic"); + String topicName = PublisherApi.ResourceNames.formatTopicPath("my-project", "my-topic"); Topic result = publisherApi.createTopic(topicName); Assert.assertEquals(topicName, result.getName()); } @Test public void testPublish() throws Exception { - String topicName = PublisherApi.createTopicPath("my-project", "publish-topic"); + String topicName = PublisherApi.ResourceNames.formatTopicPath("my-project", "publish-topic"); publisherApi.createTopic(topicName); - String subscriberName = SubscriberApi.createSubscriptionPath("my-project", "my-subscribe"); + String subscriberName = SubscriberApi.ResourceNames.formatSubscriptionPath("my-project", "my-subscribe"); PushConfig config = PushConfig.getDefaultInstance(); subscriberApi.createSubscription(subscriberName, topicName, config, 5); @@ -103,7 +103,7 @@ public void testPublish() throws Exception { @Test public void testGetTopic() throws Exception { - String topicName = PublisherApi.createTopicPath("my-project", "fun-topic"); + String topicName = PublisherApi.ResourceNames.formatTopicPath("my-project", "fun-topic"); publisherApi.createTopic(topicName); Topic result = publisherApi.getTopic(topicName); Assert.assertNotNull(result); @@ -112,10 +112,10 @@ public void testGetTopic() throws Exception { @Test public void testListTopics() throws Exception { - String project1 = PublisherApi.createProjectPath("project.1"); - String topicName1 = PublisherApi.createTopicPath("project.1", "topic.1"); - String topicName2 = PublisherApi.createTopicPath("project.1", "topic.2"); - String topicName3 = PublisherApi.createTopicPath("project.2", "topic.3"); + String project1 = PublisherApi.ResourceNames.formatProjectPath("project.1"); + String topicName1 = PublisherApi.ResourceNames.formatTopicPath("project.1", "topic.1"); + String topicName2 = PublisherApi.ResourceNames.formatTopicPath("project.1", "topic.2"); + String topicName3 = PublisherApi.ResourceNames.formatTopicPath("project.2", "topic.3"); publisherApi.createTopic(topicName1); publisherApi.createTopic(topicName2); publisherApi.createTopic(topicName3); @@ -130,8 +130,8 @@ public void testListTopics() throws Exception { @Test public void testDeleteTopic() throws Exception { - String project = PublisherApi.createProjectPath("project.1"); - String topicName = PublisherApi.createTopicPath("my-project", "fun-topic"); + String project = PublisherApi.ResourceNames.formatProjectPath("project.1"); + String topicName = PublisherApi.ResourceNames.formatTopicPath("my-project", "fun-topic"); publisherApi.createTopic(topicName); publisherApi.deleteTopic(topicName); List topics = new ArrayList<>(); From 670111793cb42732e4ae153929b646dc9ce51934 Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Thu, 10 Mar 2016 16:08:49 -0800 Subject: [PATCH 4/7] Using resurrected ServiceApiSettings in Settings classes --- gcloud-java-pubsub/pom.xml | 2 +- .../gcloud/pubsub/spi/PublisherApi.java | 6 - .../gcloud/pubsub/spi/PublisherSettings.java | 128 ++++++++------- .../gcloud/pubsub/spi/SubscriberApi.java | 6 - .../gcloud/pubsub/spi/SubscriberSettings.java | 151 +++++++++--------- .../gcloud/pubsub/spi/PublisherApiTest.java | 22 +++ 6 files changed, 169 insertions(+), 146 deletions(-) diff --git a/gcloud-java-pubsub/pom.xml b/gcloud-java-pubsub/pom.xml index 64d2bca4e58e..2fccc5a6560c 100644 --- a/gcloud-java-pubsub/pom.xml +++ b/gcloud-java-pubsub/pom.xml @@ -19,7 +19,7 @@ com.google.api gax - 0.0.3 + 0.0.4 com.google.api.grpc diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index 3aa8bb6cae92..7f456396501a 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -213,12 +213,6 @@ public static PublisherApi create(PublisherSettings settings) throws IOException protected PublisherApi(PublisherSettings settings) throws IOException { this.channel = settings.getChannel(); - for (ApiCallSettings method : settings.allMethods()) { - if (method.getExecutor() == null) { - method.setExecutor(settings.getExecutor()); - } - } - this.createTopicCallable = settings.createTopicMethod().build(settings); this.publishCallable = settings.publishMethod().build(settings); this.getTopicCallable = settings.getTopicMethod().build(settings); diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java index f10a7b7357f3..a8e76ccf586c 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; import com.google.api.gax.grpc.PageDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -62,7 +63,7 @@ // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. @javax.annotation.Generated("by GAPIC") -public class PublisherSettings extends ApiCallSettings { +public class PublisherSettings extends ServiceApiSettings { // ========= // Constants @@ -131,16 +132,61 @@ public class PublisherSettings extends ApiCallSettings { RETRY_PARAM_DEFINITIONS = definitions.build(); } - private final ApiCallableBuilder createTopicMethod; - private final ApiCallableBuilder publishMethod; - private final ApiCallableBuilder getTopicMethod; - private final PageStreamingApiCallableBuilder - listTopicsMethod; - private final PageStreamingApiCallableBuilder< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - listTopicSubscriptionsMethod; - private final ApiCallableBuilder deleteTopicMethod; - private final ImmutableList allMethods; + private static class MethodBuilders { + public final ApiCallableBuilder createTopicMethod; + public final ApiCallableBuilder publishMethod; + public final ApiCallableBuilder getTopicMethod; + public final PageStreamingApiCallableBuilder + listTopicsMethod; + public final PageStreamingApiCallableBuilder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsMethod; + public final ApiCallableBuilder deleteTopicMethod; + public final ImmutableList allMethods; + + public MethodBuilders() { + createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); + createTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + createTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + publishMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_PUBLISH); + publishMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + publishMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + getTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_GET_TOPIC); + getTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + getTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicsMethod = + new PageStreamingApiCallableBuilder<>( + 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 PageStreamingApiCallableBuilder<>( + PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); + listTopicsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + listTopicsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + deleteTopicMethod = new ApiCallableBuilder<>(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(); + } + } + + private final MethodBuilders methods; // =============== // Factory Methods @@ -153,7 +199,7 @@ public class PublisherSettings extends ApiCallSettings { * */ public static PublisherSettings create() { - PublisherSettings settings = new PublisherSettings(); + PublisherSettings settings = new PublisherSettings(new MethodBuilders()); settings.provideChannelWith( ConnectionSettings.builder() .setServiceAddress(DEFAULT_SERVICE_ADDRESS) @@ -170,45 +216,9 @@ public static PublisherSettings create() { * * */ - protected PublisherSettings() { - createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); - createTopicMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - publishMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_PUBLISH); - publishMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - getTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_GET_TOPIC); - getTopicMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicsMethod = - new PageStreamingApiCallableBuilder<>( - PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC); - listTopicsMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicSubscriptionsMethod = - new PageStreamingApiCallableBuilder<>( - PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); - listTopicSubscriptionsMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - deleteTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_DELETE_TOPIC); - deleteTopicMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createTopicMethod, - publishMethod, - getTopicMethod, - listTopicsMethod, - listTopicSubscriptionsMethod, - deleteTopicMethod) - .build(); + protected PublisherSettings(MethodBuilders methods) { + super(methods.allMethods); + this.methods = methods; } /** @@ -218,7 +228,7 @@ protected PublisherSettings() { * */ public ApiCallableBuilder createTopicMethod() { - return createTopicMethod; + return methods.createTopicMethod; } /** @@ -228,7 +238,7 @@ public ApiCallableBuilder createTopicMethod() { * */ public ApiCallableBuilder publishMethod() { - return publishMethod; + return methods.publishMethod; } /** @@ -238,7 +248,7 @@ public ApiCallableBuilder publishMethod() { * */ public ApiCallableBuilder getTopicMethod() { - return getTopicMethod; + return methods.getTopicMethod; } /** @@ -249,7 +259,7 @@ public ApiCallableBuilder getTopicMethod() { */ public PageStreamingApiCallableBuilder listTopicsMethod() { - return listTopicsMethod; + return methods.listTopicsMethod; } /** @@ -261,7 +271,7 @@ public ApiCallableBuilder getTopicMethod() { public PageStreamingApiCallableBuilder< ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> listTopicSubscriptionsMethod() { - return listTopicSubscriptionsMethod; + return methods.listTopicSubscriptionsMethod; } /** @@ -271,11 +281,7 @@ public ApiCallableBuilder getTopicMethod() { * */ public ApiCallableBuilder deleteTopicMethod() { - return deleteTopicMethod; - } - - public ImmutableList allMethods() { - return allMethods; + return methods.deleteTopicMethod; } private static PageDescriptor diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index 5b8dd746bdda..db69a80c750e 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -216,12 +216,6 @@ public static SubscriberApi create(SubscriberSettings settings) throws IOExcepti protected SubscriberApi(SubscriberSettings settings) throws IOException { this.channel = settings.getChannel(); - for (ApiCallSettings method : settings.allMethods()) { - if (method.getExecutor() == null) { - method.setExecutor(settings.getExecutor()); - } - } - this.createSubscriptionCallable = settings.createSubscriptionMethod().build(settings); this.getSubscriptionCallable = settings.getSubscriptionMethod().build(settings); this.listSubscriptionsCallable = settings.listSubscriptionsMethod().build(settings); diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java index e5cfd1d4a9b1..75ad0b2fbdbe 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; import com.google.api.gax.grpc.PageDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -57,13 +58,14 @@ import com.google.pubsub.v1.PullResponse; import com.google.pubsub.v1.SubscriberGrpc; import com.google.pubsub.v1.Subscription; + import io.grpc.Status; // Manually-added imports: add custom (non-generated) imports after this point. // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. @javax.annotation.Generated("by GAPIC") -public class SubscriberSettings extends ApiCallSettings { +public class SubscriberSettings extends ServiceApiSettings { // ========= // Constants @@ -132,17 +134,70 @@ public class SubscriberSettings extends ApiCallSettings { RETRY_PARAM_DEFINITIONS = definitions.build(); } - private final ApiCallableBuilder createSubscriptionMethod; - private final ApiCallableBuilder getSubscriptionMethod; - private final PageStreamingApiCallableBuilder< - ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> - listSubscriptionsMethod; - private final ApiCallableBuilder deleteSubscriptionMethod; - private final ApiCallableBuilder modifyAckDeadlineMethod; - private final ApiCallableBuilder acknowledgeMethod; - private final ApiCallableBuilder pullMethod; - private final ApiCallableBuilder modifyPushConfigMethod; - private final ImmutableList allMethods; + private static class MethodBuilders { + private final ApiCallableBuilder createSubscriptionMethod; + private final ApiCallableBuilder getSubscriptionMethod; + private final PageStreamingApiCallableBuilder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsMethod; + private final ApiCallableBuilder deleteSubscriptionMethod; + private final ApiCallableBuilder modifyAckDeadlineMethod; + private final ApiCallableBuilder acknowledgeMethod; + private final ApiCallableBuilder pullMethod; + private final ApiCallableBuilder modifyPushConfigMethod; + private final ImmutableList allMethods; + + public MethodBuilders() { + createSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); + createSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + createSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + getSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); + getSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + getSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + listSubscriptionsMethod = + new PageStreamingApiCallableBuilder<>( + 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 ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); + deleteSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + deleteSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyAckDeadlineMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); + modifyAckDeadlineMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + modifyAckDeadlineMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + acknowledgeMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); + acknowledgeMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + acknowledgeMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + pullMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_PULL); + pullMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + pullMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyPushConfigMethod = new ApiCallableBuilder<>(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(); + } + } + + private final MethodBuilders methods; // =============== // Factory Methods @@ -155,7 +210,7 @@ public class SubscriberSettings extends ApiCallSettings { * */ public static SubscriberSettings create() { - SubscriberSettings settings = new SubscriberSettings(); + SubscriberSettings settings = new SubscriberSettings(new MethodBuilders()); settings.provideChannelWith( ConnectionSettings.builder() .setServiceAddress(DEFAULT_SERVICE_ADDRESS) @@ -172,53 +227,9 @@ public static SubscriberSettings create() { * * */ - protected SubscriberSettings() { - createSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); - createSubscriptionMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - getSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); - getSubscriptionMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - listSubscriptionsMethod = - new PageStreamingApiCallableBuilder<>( - SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC); - listSubscriptionsMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - deleteSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); - deleteSubscriptionMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - modifyAckDeadlineMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); - modifyAckDeadlineMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - acknowledgeMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); - acknowledgeMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - pullMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_PULL); - pullMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - modifyPushConfigMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); - modifyPushConfigMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createSubscriptionMethod, - getSubscriptionMethod, - listSubscriptionsMethod, - deleteSubscriptionMethod, - modifyAckDeadlineMethod, - acknowledgeMethod, - pullMethod, - modifyPushConfigMethod) - .build(); + protected SubscriberSettings(MethodBuilders methods) { + super(methods.allMethods); + this.methods = methods; } /** @@ -228,7 +239,7 @@ protected SubscriberSettings() { * */ public ApiCallableBuilder createSubscriptionMethod() { - return createSubscriptionMethod; + return methods.createSubscriptionMethod; } /** @@ -238,7 +249,7 @@ public ApiCallableBuilder createSubscriptionMethod() * */ public ApiCallableBuilder getSubscriptionMethod() { - return getSubscriptionMethod; + return methods.getSubscriptionMethod; } /** @@ -250,7 +261,7 @@ public ApiCallableBuilder getSubscriptionM public PageStreamingApiCallableBuilder< ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> listSubscriptionsMethod() { - return listSubscriptionsMethod; + return methods.listSubscriptionsMethod; } /** @@ -260,7 +271,7 @@ public ApiCallableBuilder getSubscriptionM * */ public ApiCallableBuilder deleteSubscriptionMethod() { - return deleteSubscriptionMethod; + return methods.deleteSubscriptionMethod; } /** @@ -270,7 +281,7 @@ public ApiCallableBuilder deleteSubscriptionMe * */ public ApiCallableBuilder modifyAckDeadlineMethod() { - return modifyAckDeadlineMethod; + return methods.modifyAckDeadlineMethod; } /** @@ -280,7 +291,7 @@ public ApiCallableBuilder modifyAckDeadlineMeth * */ public ApiCallableBuilder acknowledgeMethod() { - return acknowledgeMethod; + return methods.acknowledgeMethod; } /** @@ -290,7 +301,7 @@ public ApiCallableBuilder acknowledgeMethod() { * */ public ApiCallableBuilder pullMethod() { - return pullMethod; + return methods.pullMethod; } /** @@ -300,11 +311,7 @@ public ApiCallableBuilder pullMethod() { * */ public ApiCallableBuilder modifyPushConfigMethod() { - return modifyPushConfigMethod; - } - - public ImmutableList allMethods() { - return allMethods; + return methods.modifyPushConfigMethod; } private static PageDescriptor diff --git a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java index 4b4c91ebe797..981170db930f 100644 --- a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java +++ b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java @@ -14,6 +14,9 @@ package com.google.gcloud.pubsub.spi; +import com.google.api.gax.core.BackoffParams; +import com.google.api.gax.core.RetryParams; +import com.google.api.gax.grpc.ApiCallSettings; import com.google.gcloud.pubsub.testing.LocalPubsubHelper; import com.google.protobuf.ByteString; import com.google.pubsub.v1.PubsubMessage; @@ -55,11 +58,30 @@ public static void stopServer() throws IOException, InterruptedException { public void setUp() throws Exception { ManagedChannel channel = pubsubHelper.createChannel(); + RetryParams retryParams = + RetryParams.newBuilder() + .setRetryBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(1000L) + .setDelayMultiplier(1.2) + .setMaxDelayMillis(10000L) + .build()) + .setTimeoutBackoff( + BackoffParams.newBuilder() + .setInitialDelayMillis(3000L) + .setDelayMultiplier(1.3) + .setMaxDelayMillis(30000L) + .build()) + .setTotalTimeout(30000L) + .build(); + PublisherSettings publisherSettings = PublisherApi.newSettings(); + publisherSettings.setRetryParamsOnAllMethods(retryParams); publisherSettings.provideChannelWith(channel); publisherApi = PublisherApi.create(publisherSettings); SubscriberSettings subscriberSettings = SubscriberApi.newSettings(); + subscriberSettings.setRetryParamsOnAllMethods(retryParams); subscriberSettings.provideChannelWith(channel); subscriberApi = SubscriberApi.create(subscriberSettings); } From d0bc2878047e57a6fcfdc8670e70d498069ebcfa Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Fri, 11 Mar 2016 14:47:06 -0800 Subject: [PATCH 5/7] Updates due to code gen --- .../gcloud/pubsub/spi/PublisherApi.java | 6 - .../gcloud/pubsub/spi/PublisherSettings.java | 129 ++++++++------- .../gcloud/pubsub/spi/SubscriberApi.java | 6 - .../gcloud/pubsub/spi/SubscriberSettings.java | 152 +++++++++--------- .../gcloud/pubsub/spi/PublisherSettings.java | 21 +-- .../gcloud/pubsub/spi/SubscriberSettings.java | 7 +- 6 files changed, 163 insertions(+), 158 deletions(-) diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index 3aa8bb6cae92..7f456396501a 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -213,12 +213,6 @@ public static PublisherApi create(PublisherSettings settings) throws IOException protected PublisherApi(PublisherSettings settings) throws IOException { this.channel = settings.getChannel(); - for (ApiCallSettings method : settings.allMethods()) { - if (method.getExecutor() == null) { - method.setExecutor(settings.getExecutor()); - } - } - this.createTopicCallable = settings.createTopicMethod().build(settings); this.publishCallable = settings.publishMethod().build(settings); this.getTopicCallable = settings.getTopicMethod().build(settings); diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java index f10a7b7357f3..83ac11d19526 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; import com.google.api.gax.grpc.PageDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -62,7 +63,7 @@ // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. @javax.annotation.Generated("by GAPIC") -public class PublisherSettings extends ApiCallSettings { +public class PublisherSettings extends ServiceApiSettings { // ========= // Constants @@ -131,16 +132,62 @@ public class PublisherSettings extends ApiCallSettings { RETRY_PARAM_DEFINITIONS = definitions.build(); } - private final ApiCallableBuilder createTopicMethod; - private final ApiCallableBuilder publishMethod; - private final ApiCallableBuilder getTopicMethod; - private final PageStreamingApiCallableBuilder - listTopicsMethod; - private final PageStreamingApiCallableBuilder< - ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> - listTopicSubscriptionsMethod; - private final ApiCallableBuilder deleteTopicMethod; - private final ImmutableList allMethods; + private static class MethodBuilders { + private final ApiCallableBuilder createTopicMethod; + private final ApiCallableBuilder publishMethod; + private final ApiCallableBuilder getTopicMethod; + private final PageStreamingApiCallableBuilder + listTopicsMethod; + private final PageStreamingApiCallableBuilder< + ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> + listTopicSubscriptionsMethod; + private final ApiCallableBuilder deleteTopicMethod; + private final ImmutableList allMethods; + + public MethodBuilders() { + createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); + createTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + createTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + publishMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_PUBLISH); + publishMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + publishMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + getTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_GET_TOPIC); + getTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + getTopicMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + listTopicsMethod = + new PageStreamingApiCallableBuilder<>( + 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 PageStreamingApiCallableBuilder<>( + 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 ApiCallableBuilder<>(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(); + } + } + + private final MethodBuilders methods; // =============== // Factory Methods @@ -153,7 +200,7 @@ public class PublisherSettings extends ApiCallSettings { * */ public static PublisherSettings create() { - PublisherSettings settings = new PublisherSettings(); + PublisherSettings settings = new PublisherSettings(new MethodBuilders()); settings.provideChannelWith( ConnectionSettings.builder() .setServiceAddress(DEFAULT_SERVICE_ADDRESS) @@ -170,45 +217,9 @@ public static PublisherSettings create() { * * */ - protected PublisherSettings() { - createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); - createTopicMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - publishMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_PUBLISH); - publishMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - getTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_GET_TOPIC); - getTopicMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicsMethod = - new PageStreamingApiCallableBuilder<>( - PublisherGrpc.METHOD_LIST_TOPICS, LIST_TOPICS_PAGE_STR_DESC); - listTopicsMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - listTopicSubscriptionsMethod = - new PageStreamingApiCallableBuilder<>( - PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); - listTopicSubscriptionsMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - deleteTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_DELETE_TOPIC); - deleteTopicMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createTopicMethod, - publishMethod, - getTopicMethod, - listTopicsMethod, - listTopicSubscriptionsMethod, - deleteTopicMethod) - .build(); + protected PublisherSettings(MethodBuilders methods) { + super(methods.allMethods); + this.methods = methods; } /** @@ -218,7 +229,7 @@ protected PublisherSettings() { * */ public ApiCallableBuilder createTopicMethod() { - return createTopicMethod; + return methods.createTopicMethod; } /** @@ -228,7 +239,7 @@ public ApiCallableBuilder createTopicMethod() { * */ public ApiCallableBuilder publishMethod() { - return publishMethod; + return methods.publishMethod; } /** @@ -238,7 +249,7 @@ public ApiCallableBuilder publishMethod() { * */ public ApiCallableBuilder getTopicMethod() { - return getTopicMethod; + return methods.getTopicMethod; } /** @@ -249,7 +260,7 @@ public ApiCallableBuilder getTopicMethod() { */ public PageStreamingApiCallableBuilder listTopicsMethod() { - return listTopicsMethod; + return methods.listTopicsMethod; } /** @@ -261,7 +272,7 @@ public ApiCallableBuilder getTopicMethod() { public PageStreamingApiCallableBuilder< ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> listTopicSubscriptionsMethod() { - return listTopicSubscriptionsMethod; + return methods.listTopicSubscriptionsMethod; } /** @@ -271,11 +282,7 @@ public ApiCallableBuilder getTopicMethod() { * */ public ApiCallableBuilder deleteTopicMethod() { - return deleteTopicMethod; - } - - public ImmutableList allMethods() { - return allMethods; + return methods.deleteTopicMethod; } private static PageDescriptor diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index 5b8dd746bdda..db69a80c750e 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -216,12 +216,6 @@ public static SubscriberApi create(SubscriberSettings settings) throws IOExcepti protected SubscriberApi(SubscriberSettings settings) throws IOException { this.channel = settings.getChannel(); - for (ApiCallSettings method : settings.allMethods()) { - if (method.getExecutor() == null) { - method.setExecutor(settings.getExecutor()); - } - } - this.createSubscriptionCallable = settings.createSubscriptionMethod().build(settings); this.getSubscriptionCallable = settings.getSubscriptionMethod().build(settings); this.listSubscriptionsCallable = settings.listSubscriptionsMethod().build(settings); diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java index e5cfd1d4a9b1..12242dc47005 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -40,6 +40,7 @@ import com.google.api.gax.grpc.ApiCallable.ApiCallableBuilder; import com.google.api.gax.grpc.ApiCallable.PageStreamingApiCallableBuilder; import com.google.api.gax.grpc.PageDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -63,7 +64,7 @@ // AUTO-GENERATED DOCUMENTATION AND CLASS - see instructions at the top of the file for editing. @javax.annotation.Generated("by GAPIC") -public class SubscriberSettings extends ApiCallSettings { +public class SubscriberSettings extends ServiceApiSettings { // ========= // Constants @@ -132,17 +133,72 @@ public class SubscriberSettings extends ApiCallSettings { RETRY_PARAM_DEFINITIONS = definitions.build(); } - private final ApiCallableBuilder createSubscriptionMethod; - private final ApiCallableBuilder getSubscriptionMethod; - private final PageStreamingApiCallableBuilder< - ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> - listSubscriptionsMethod; - private final ApiCallableBuilder deleteSubscriptionMethod; - private final ApiCallableBuilder modifyAckDeadlineMethod; - private final ApiCallableBuilder acknowledgeMethod; - private final ApiCallableBuilder pullMethod; - private final ApiCallableBuilder modifyPushConfigMethod; - private final ImmutableList allMethods; + private static class MethodBuilders { + private final ApiCallableBuilder createSubscriptionMethod; + private final ApiCallableBuilder getSubscriptionMethod; + private final PageStreamingApiCallableBuilder< + ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> + listSubscriptionsMethod; + private final ApiCallableBuilder deleteSubscriptionMethod; + private final ApiCallableBuilder modifyAckDeadlineMethod; + private final ApiCallableBuilder acknowledgeMethod; + private final ApiCallableBuilder pullMethod; + private final ApiCallableBuilder modifyPushConfigMethod; + private final ImmutableList allMethods; + + public MethodBuilders() { + createSubscriptionMethod = + new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); + createSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + createSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + getSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); + getSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + getSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + listSubscriptionsMethod = + new PageStreamingApiCallableBuilder<>( + 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 ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); + deleteSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); + deleteSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyAckDeadlineMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); + modifyAckDeadlineMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + modifyAckDeadlineMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + acknowledgeMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); + acknowledgeMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + acknowledgeMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + pullMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_PULL); + pullMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); + pullMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + + modifyPushConfigMethod = new ApiCallableBuilder<>(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(); + } + } + + private final MethodBuilders methods; // =============== // Factory Methods @@ -155,7 +211,7 @@ public class SubscriberSettings extends ApiCallSettings { * */ public static SubscriberSettings create() { - SubscriberSettings settings = new SubscriberSettings(); + SubscriberSettings settings = new SubscriberSettings(new MethodBuilders()); settings.provideChannelWith( ConnectionSettings.builder() .setServiceAddress(DEFAULT_SERVICE_ADDRESS) @@ -172,53 +228,9 @@ public static SubscriberSettings create() { * * */ - protected SubscriberSettings() { - createSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); - createSubscriptionMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - getSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_GET_SUBSCRIPTION); - getSubscriptionMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - listSubscriptionsMethod = - new PageStreamingApiCallableBuilder<>( - SubscriberGrpc.METHOD_LIST_SUBSCRIPTIONS, LIST_SUBSCRIPTIONS_PAGE_STR_DESC); - listSubscriptionsMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - deleteSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); - deleteSubscriptionMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - modifyAckDeadlineMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_ACK_DEADLINE); - modifyAckDeadlineMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - acknowledgeMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_ACKNOWLEDGE); - acknowledgeMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - pullMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_PULL); - pullMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - modifyPushConfigMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); - modifyPushConfigMethod.setRetryDefaults( - RETRYABLE_CODE_DEFINITIONS.get("non_idempotent"), RETRY_PARAM_DEFINITIONS.get("default")); - - allMethods = - ImmutableList.builder() - .add( - createSubscriptionMethod, - getSubscriptionMethod, - listSubscriptionsMethod, - deleteSubscriptionMethod, - modifyAckDeadlineMethod, - acknowledgeMethod, - pullMethod, - modifyPushConfigMethod) - .build(); + protected SubscriberSettings(MethodBuilders methods) { + super(methods.allMethods); + this.methods = methods; } /** @@ -228,7 +240,7 @@ protected SubscriberSettings() { * */ public ApiCallableBuilder createSubscriptionMethod() { - return createSubscriptionMethod; + return methods.createSubscriptionMethod; } /** @@ -238,7 +250,7 @@ public ApiCallableBuilder createSubscriptionMethod() * */ public ApiCallableBuilder getSubscriptionMethod() { - return getSubscriptionMethod; + return methods.getSubscriptionMethod; } /** @@ -250,7 +262,7 @@ public ApiCallableBuilder getSubscriptionM public PageStreamingApiCallableBuilder< ListSubscriptionsRequest, ListSubscriptionsResponse, Subscription> listSubscriptionsMethod() { - return listSubscriptionsMethod; + return methods.listSubscriptionsMethod; } /** @@ -260,7 +272,7 @@ public ApiCallableBuilder getSubscriptionM * */ public ApiCallableBuilder deleteSubscriptionMethod() { - return deleteSubscriptionMethod; + return methods.deleteSubscriptionMethod; } /** @@ -270,7 +282,7 @@ public ApiCallableBuilder deleteSubscriptionMe * */ public ApiCallableBuilder modifyAckDeadlineMethod() { - return modifyAckDeadlineMethod; + return methods.modifyAckDeadlineMethod; } /** @@ -280,7 +292,7 @@ public ApiCallableBuilder modifyAckDeadlineMeth * */ public ApiCallableBuilder acknowledgeMethod() { - return acknowledgeMethod; + return methods.acknowledgeMethod; } /** @@ -290,7 +302,7 @@ public ApiCallableBuilder acknowledgeMethod() { * */ public ApiCallableBuilder pullMethod() { - return pullMethod; + return methods.pullMethod; } /** @@ -300,11 +312,7 @@ public ApiCallableBuilder pullMethod() { * */ public ApiCallableBuilder modifyPushConfigMethod() { - return modifyPushConfigMethod; - } - - public ImmutableList allMethods() { - return allMethods; + return methods.modifyPushConfigMethod; } private static PageDescriptor diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java index a8e76ccf586c..83ac11d19526 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherSettings.java @@ -133,16 +133,16 @@ public class PublisherSettings extends ServiceApiSettings { } private static class MethodBuilders { - public final ApiCallableBuilder createTopicMethod; - public final ApiCallableBuilder publishMethod; - public final ApiCallableBuilder getTopicMethod; - public final PageStreamingApiCallableBuilder + private final ApiCallableBuilder createTopicMethod; + private final ApiCallableBuilder publishMethod; + private final ApiCallableBuilder getTopicMethod; + private final PageStreamingApiCallableBuilder listTopicsMethod; - public final PageStreamingApiCallableBuilder< + private final PageStreamingApiCallableBuilder< ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> listTopicSubscriptionsMethod; - public final ApiCallableBuilder deleteTopicMethod; - public final ImmutableList allMethods; + private final ApiCallableBuilder deleteTopicMethod; + private final ImmutableList allMethods; public MethodBuilders() { createTopicMethod = new ApiCallableBuilder<>(PublisherGrpc.METHOD_CREATE_TOPIC); @@ -165,9 +165,10 @@ public MethodBuilders() { listTopicSubscriptionsMethod = new PageStreamingApiCallableBuilder<>( - PublisherGrpc.METHOD_LIST_TOPIC_SUBSCRIPTIONS, LIST_TOPIC_SUBSCRIPTIONS_PAGE_STR_DESC); - listTopicsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); - listTopicsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); + 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 ApiCallableBuilder<>(PublisherGrpc.METHOD_DELETE_TOPIC); deleteTopicMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java index 75ad0b2fbdbe..12242dc47005 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberSettings.java @@ -58,7 +58,6 @@ import com.google.pubsub.v1.PullResponse; import com.google.pubsub.v1.SubscriberGrpc; import com.google.pubsub.v1.Subscription; - import io.grpc.Status; // Manually-added imports: add custom (non-generated) imports after this point. @@ -148,7 +147,8 @@ private static class MethodBuilders { private final ImmutableList allMethods; public MethodBuilders() { - createSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); + createSubscriptionMethod = + new ApiCallableBuilder<>(SubscriberGrpc.METHOD_CREATE_SUBSCRIPTION); createSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")); createSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); @@ -162,7 +162,8 @@ public MethodBuilders() { listSubscriptionsMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); listSubscriptionsMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); - deleteSubscriptionMethod = new ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); + deleteSubscriptionMethod = + new ApiCallableBuilder<>(SubscriberGrpc.METHOD_DELETE_SUBSCRIPTION); deleteSubscriptionMethod.setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")); deleteSubscriptionMethod.setRetryParams(RETRY_PARAM_DEFINITIONS.get("default")); From a4609cae9670f81e2ff4b9865835446f893c4e8e Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Tue, 15 Mar 2016 20:59:46 -0700 Subject: [PATCH 6/7] Removing newSettings() method from XApi classes --- .../java/com/google/gcloud/pubsub/spi/PublisherApi.java | 7 ------- .../java/com/google/gcloud/pubsub/spi/SubscriberApi.java | 7 ------- .../java/com/google/gcloud/pubsub/spi/PublisherApi.java | 7 ------- .../java/com/google/gcloud/pubsub/spi/SubscriberApi.java | 7 ------- 4 files changed, 28 deletions(-) diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index 7f456396501a..ce34fb6f8380 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -175,13 +175,6 @@ public static final String parseTopicFromTopicPath(String topicPath) { // Factory Methods // =============== - /** - * Constructs an instance of PublisherSettings with default settings. - */ - public static PublisherSettings newSettings() { - return PublisherSettings.create(); - } - /** * Constructs an instance of PublisherApi with default settings. * diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index db69a80c750e..0a16a41e9c24 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -178,13 +178,6 @@ public static final String parseSubscriptionFromSubscriptionPath(String subscrip // Factory Methods // =============== - /** - * Constructs an instance of SubscriberSettings with default settings. - */ - public static SubscriberSettings newSettings() { - return SubscriberSettings.create(); - } - /** * Constructs an instance of SubscriberApi with default settings. * diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index 7f456396501a..ce34fb6f8380 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -175,13 +175,6 @@ public static final String parseTopicFromTopicPath(String topicPath) { // Factory Methods // =============== - /** - * Constructs an instance of PublisherSettings with default settings. - */ - public static PublisherSettings newSettings() { - return PublisherSettings.create(); - } - /** * Constructs an instance of PublisherApi with default settings. * diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index db69a80c750e..0a16a41e9c24 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -178,13 +178,6 @@ public static final String parseSubscriptionFromSubscriptionPath(String subscrip // Factory Methods // =============== - /** - * Constructs an instance of SubscriberSettings with default settings. - */ - public static SubscriberSettings newSettings() { - return SubscriberSettings.create(); - } - /** * Constructs an instance of SubscriberApi with default settings. * From 5245e7d059ea4073d928562c9ce4afa26cb228b6 Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Tue, 15 Mar 2016 21:04:06 -0700 Subject: [PATCH 7/7] Fixing build from last change --- .../main/java/com/google/gcloud/pubsub/spi/PublisherApi.java | 2 +- .../main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java | 2 +- .../main/java/com/google/gcloud/pubsub/spi/PublisherApi.java | 2 +- .../main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java | 2 +- .../java/com/google/gcloud/pubsub/spi/PublisherApiTest.java | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index ce34fb6f8380..78ccc1f6e026 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -182,7 +182,7 @@ public static final String parseTopicFromTopicPath(String topicPath) { * */ public static PublisherApi create() throws IOException { - return create(newSettings()); + return create(PublisherSettings.create()); } /** diff --git a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index 0a16a41e9c24..191c0006f12b 100644 --- a/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/baseline/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -185,7 +185,7 @@ public static final String parseSubscriptionFromSubscriptionPath(String subscrip * */ public static SubscriberApi create() throws IOException { - return create(newSettings()); + return create(SubscriberSettings.create()); } /** diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java index ce34fb6f8380..78ccc1f6e026 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java @@ -182,7 +182,7 @@ public static final String parseTopicFromTopicPath(String topicPath) { * */ public static PublisherApi create() throws IOException { - return create(newSettings()); + return create(PublisherSettings.create()); } /** diff --git a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java index 0a16a41e9c24..191c0006f12b 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java +++ b/gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java @@ -185,7 +185,7 @@ public static final String parseSubscriptionFromSubscriptionPath(String subscrip * */ public static SubscriberApi create() throws IOException { - return create(newSettings()); + return create(SubscriberSettings.create()); } /** diff --git a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java index 981170db930f..4330c590a182 100644 --- a/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java +++ b/gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java @@ -75,12 +75,12 @@ public void setUp() throws Exception { .setTotalTimeout(30000L) .build(); - PublisherSettings publisherSettings = PublisherApi.newSettings(); + PublisherSettings publisherSettings = PublisherSettings.create(); publisherSettings.setRetryParamsOnAllMethods(retryParams); publisherSettings.provideChannelWith(channel); publisherApi = PublisherApi.create(publisherSettings); - SubscriberSettings subscriberSettings = SubscriberApi.newSettings(); + SubscriberSettings subscriberSettings = SubscriberSettings.create(); subscriberSettings.setRetryParamsOnAllMethods(retryParams); subscriberSettings.provideChannelWith(channel); subscriberApi = SubscriberApi.create(subscriberSettings);