|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package pubsublite; |
| 18 | + |
| 19 | +// [START pubsublite_create_pubsub_export_subscription] |
| 20 | +import com.google.api.gax.rpc.AlreadyExistsException; |
| 21 | +import com.google.cloud.pubsublite.AdminClient; |
| 22 | +import com.google.cloud.pubsublite.AdminClientSettings; |
| 23 | +import com.google.cloud.pubsublite.BacklogLocation; |
| 24 | +import com.google.cloud.pubsublite.CloudRegion; |
| 25 | +import com.google.cloud.pubsublite.CloudRegionOrZone; |
| 26 | +import com.google.cloud.pubsublite.CloudZone; |
| 27 | +import com.google.cloud.pubsublite.ProjectNumber; |
| 28 | +import com.google.cloud.pubsublite.SeekTarget; |
| 29 | +import com.google.cloud.pubsublite.SubscriptionName; |
| 30 | +import com.google.cloud.pubsublite.SubscriptionPath; |
| 31 | +import com.google.cloud.pubsublite.TopicName; |
| 32 | +import com.google.cloud.pubsublite.TopicPath; |
| 33 | +import com.google.cloud.pubsublite.proto.ExportConfig; |
| 34 | +import com.google.cloud.pubsublite.proto.ExportConfig.PubSubConfig; |
| 35 | +import com.google.cloud.pubsublite.proto.ExportConfig.State; |
| 36 | +import com.google.cloud.pubsublite.proto.Subscription; |
| 37 | +import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig; |
| 38 | +import com.google.cloud.pubsublite.proto.Subscription.DeliveryConfig.DeliveryRequirement; |
| 39 | +import java.util.concurrent.ExecutionException; |
| 40 | + |
| 41 | +public class CreatePubsubExportSubscriptionExample { |
| 42 | + |
| 43 | + public static void main(String... args) throws Exception { |
| 44 | + // TODO(developer): Replace these variables before running the sample. |
| 45 | + String cloudRegion = "your-cloud-region"; |
| 46 | + char zoneId = 'b'; |
| 47 | + String topicId = "your-topic-id"; |
| 48 | + String subscriptionId = "your-subscription-id"; |
| 49 | + String pubsubTopicId = "destination-topic-id"; |
| 50 | + long projectNumber = Long.parseLong("123456789"); |
| 51 | + // True if using a regional location. False if using a zonal location. |
| 52 | + // https://cloud.google.com/pubsub/lite/docs/topics |
| 53 | + boolean regional = false; |
| 54 | + |
| 55 | + createPubsubExportSubscriptionExample( |
| 56 | + cloudRegion, zoneId, projectNumber, topicId, subscriptionId, pubsubTopicId, regional); |
| 57 | + } |
| 58 | + |
| 59 | + public static void createPubsubExportSubscriptionExample( |
| 60 | + String cloudRegion, |
| 61 | + char zoneId, |
| 62 | + long projectNumber, |
| 63 | + String topicId, |
| 64 | + String subscriptionId, |
| 65 | + String pubsubTopicId, |
| 66 | + boolean regional) |
| 67 | + throws Exception { |
| 68 | + |
| 69 | + CloudRegionOrZone location; |
| 70 | + if (regional) { |
| 71 | + location = CloudRegionOrZone.of(CloudRegion.of(cloudRegion)); |
| 72 | + } else { |
| 73 | + location = CloudRegionOrZone.of(CloudZone.of(CloudRegion.of(cloudRegion), zoneId)); |
| 74 | + } |
| 75 | + |
| 76 | + TopicPath topicPath = |
| 77 | + TopicPath.newBuilder() |
| 78 | + .setProject(ProjectNumber.of(projectNumber)) |
| 79 | + .setLocation(location) |
| 80 | + .setName(TopicName.of(topicId)) |
| 81 | + .build(); |
| 82 | + |
| 83 | + SubscriptionPath subscriptionPath = |
| 84 | + SubscriptionPath.newBuilder() |
| 85 | + .setLocation(location) |
| 86 | + .setProject(ProjectNumber.of(projectNumber)) |
| 87 | + .setName(SubscriptionName.of(subscriptionId)) |
| 88 | + .build(); |
| 89 | + |
| 90 | + com.google.pubsub.v1.TopicName pubsubTopicName = |
| 91 | + com.google.pubsub.v1.TopicName.of(String.valueOf(projectNumber), pubsubTopicId); |
| 92 | + |
| 93 | + Subscription subscription = |
| 94 | + Subscription.newBuilder() |
| 95 | + .setDeliveryConfig( |
| 96 | + // Possible values for DeliveryRequirement: |
| 97 | + // - `DELIVER_IMMEDIATELY` |
| 98 | + // - `DELIVER_AFTER_STORED` |
| 99 | + // You may choose whether to wait for a published message to be successfully written |
| 100 | + // to storage before the server delivers it to subscribers. `DELIVER_IMMEDIATELY` is |
| 101 | + // suitable for applications that need higher throughput. |
| 102 | + DeliveryConfig.newBuilder() |
| 103 | + .setDeliveryRequirement(DeliveryRequirement.DELIVER_IMMEDIATELY)) |
| 104 | + .setExportConfig( |
| 105 | + // Configures an export subscription that writes messages to a Pub/Sub topic. |
| 106 | + ExportConfig.newBuilder() |
| 107 | + // Possible values for State: |
| 108 | + // - `ACTIVE`: enable message processing. |
| 109 | + // - `PAUSED`: suspend message processing. |
| 110 | + .setDesiredState(State.ACTIVE) |
| 111 | + .setPubsubConfig( |
| 112 | + PubSubConfig.newBuilder().setTopic(pubsubTopicName.toString()))) |
| 113 | + .setName(subscriptionPath.toString()) |
| 114 | + .setTopic(topicPath.toString()) |
| 115 | + .build(); |
| 116 | + |
| 117 | + // Target location within the message backlog that the subscription should be initialized to. |
| 118 | + SeekTarget initialLocation = SeekTarget.of(BacklogLocation.BEGINNING); |
| 119 | + |
| 120 | + AdminClientSettings adminClientSettings = |
| 121 | + AdminClientSettings.newBuilder().setRegion(location.extractRegion()).build(); |
| 122 | + |
| 123 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 124 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 125 | + // the "close" method on the client to safely clean up any remaining background resources, or |
| 126 | + // use "try-with-close" statement to do this automatically. |
| 127 | + try (AdminClient adminClient = AdminClient.create(adminClientSettings)) { |
| 128 | + Subscription response = adminClient.createSubscription(subscription, initialLocation).get(); |
| 129 | + System.out.println(response.getAllFields() + " created successfully."); |
| 130 | + } catch (ExecutionException e) { |
| 131 | + try { |
| 132 | + throw e.getCause(); |
| 133 | + } catch (AlreadyExistsException alreadyExists) { |
| 134 | + System.out.println("This subscription already exists."); |
| 135 | + } catch (Throwable throwable) { |
| 136 | + throwable.printStackTrace(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +// [END pubsublite_create_pubsub_export_subscription] |
0 commit comments