|
| 1 | +/* |
| 2 | + * Copyright 2025 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 examples; |
| 18 | + |
| 19 | +// [START managedkafka_create_connect_cluster] |
| 20 | + |
| 21 | +import com.google.api.gax.longrunning.OperationFuture; |
| 22 | +import com.google.api.gax.longrunning.OperationSnapshot; |
| 23 | +import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; |
| 24 | +import com.google.api.gax.retrying.RetrySettings; |
| 25 | +import com.google.api.gax.retrying.RetryingFuture; |
| 26 | +import com.google.api.gax.retrying.TimedRetryAlgorithm; |
| 27 | +import com.google.cloud.managedkafka.v1.CapacityConfig; |
| 28 | +import com.google.cloud.managedkafka.v1.ConnectAccessConfig; |
| 29 | +import com.google.cloud.managedkafka.v1.ConnectCluster; |
| 30 | +import com.google.cloud.managedkafka.v1.ConnectGcpConfig; |
| 31 | +import com.google.cloud.managedkafka.v1.ConnectNetworkConfig; |
| 32 | +import com.google.cloud.managedkafka.v1.CreateConnectClusterRequest; |
| 33 | +import com.google.cloud.managedkafka.v1.LocationName; |
| 34 | +import com.google.cloud.managedkafka.v1.ManagedKafkaConnectClient; |
| 35 | +import com.google.cloud.managedkafka.v1.ManagedKafkaConnectSettings; |
| 36 | +import com.google.cloud.managedkafka.v1.OperationMetadata; |
| 37 | +import java.time.Duration; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | + |
| 40 | +public class CreateConnectCluster { |
| 41 | + |
| 42 | + public static void main(String[] args) throws Exception { |
| 43 | + // TODO(developer): Replace these variables before running the example. |
| 44 | + String projectId = "my-project-id"; |
| 45 | + String region = "my-region"; // e.g. us-east1 |
| 46 | + String clusterId = "my-connect-cluster"; |
| 47 | + String subnet = "my-subnet"; // e.g. projects/my-project/regions/my-region/subnetworks/my-subnet |
| 48 | + String kafkaCluster = "my-kafka-cluster"; // The Kafka cluster to connect to |
| 49 | + int cpu = 12; |
| 50 | + long memoryBytes = 12884901888L; // 12 GiB |
| 51 | + createConnectCluster(projectId, region, clusterId, subnet, kafkaCluster, cpu, memoryBytes); |
| 52 | + } |
| 53 | + |
| 54 | + public static void createConnectCluster( |
| 55 | + String projectId, |
| 56 | + String region, |
| 57 | + String clusterId, |
| 58 | + String subnet, |
| 59 | + String kafkaCluster, |
| 60 | + int cpu, |
| 61 | + long memoryBytes) |
| 62 | + throws Exception { |
| 63 | + CapacityConfig capacityConfig = CapacityConfig.newBuilder().setVcpuCount(cpu) |
| 64 | + .setMemoryBytes(memoryBytes).build(); |
| 65 | + ConnectNetworkConfig networkConfig = ConnectNetworkConfig.newBuilder() |
| 66 | + .setPrimarySubnet(subnet) |
| 67 | + .build(); |
| 68 | + // Optionally, you can also specify additional accessible subnets and resolvable |
| 69 | + // DNS domains as part of your network configuration. For example: |
| 70 | + // .addAllAdditionalSubnets(List.of("subnet-1", "subnet-2")) |
| 71 | + // .addAllDnsDomainNames(List.of("dns-1", "dns-2")) |
| 72 | + ConnectGcpConfig gcpConfig = ConnectGcpConfig.newBuilder() |
| 73 | + .setAccessConfig(ConnectAccessConfig.newBuilder().addNetworkConfigs(networkConfig).build()) |
| 74 | + .build(); |
| 75 | + ConnectCluster connectCluster = ConnectCluster.newBuilder() |
| 76 | + .setCapacityConfig(capacityConfig) |
| 77 | + .setGcpConfig(gcpConfig) |
| 78 | + .setKafkaCluster(kafkaCluster) |
| 79 | + .build(); |
| 80 | + |
| 81 | + // Create the settings to configure the timeout for polling operations |
| 82 | + ManagedKafkaConnectSettings.Builder settingsBuilder = ManagedKafkaConnectSettings.newBuilder(); |
| 83 | + TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create( |
| 84 | + RetrySettings.newBuilder() |
| 85 | + .setTotalTimeoutDuration(Duration.ofHours(1L)) |
| 86 | + .build()); |
| 87 | + settingsBuilder.createConnectClusterOperationSettings() |
| 88 | + .setPollingAlgorithm(timedRetryAlgorithm); |
| 89 | + |
| 90 | + try (ManagedKafkaConnectClient managedKafkaConnectClient = ManagedKafkaConnectClient |
| 91 | + .create(settingsBuilder.build())) { |
| 92 | + CreateConnectClusterRequest request = CreateConnectClusterRequest.newBuilder() |
| 93 | + .setParent(LocationName.of(projectId, region).toString()) |
| 94 | + .setConnectClusterId(clusterId) |
| 95 | + .setConnectCluster(connectCluster) |
| 96 | + .build(); |
| 97 | + |
| 98 | + // The duration of this operation can vary considerably, typically taking |
| 99 | + // between 10-30 minutes. |
| 100 | + OperationFuture<ConnectCluster, OperationMetadata> future = managedKafkaConnectClient |
| 101 | + .createConnectClusterOperationCallable().futureCall(request); |
| 102 | + |
| 103 | + // Get the initial LRO and print details. |
| 104 | + OperationSnapshot operation = future.getInitialFuture().get(); |
| 105 | + System.out.printf( |
| 106 | + "Connect cluster creation started. Operation name: %s\nDone: %s\nMetadata: %s\n", |
| 107 | + operation.getName(), operation.isDone(), future.getMetadata().get().toString()); |
| 108 | + |
| 109 | + while (!future.isDone()) { |
| 110 | + // The pollingFuture gives us the most recent status of the operation |
| 111 | + RetryingFuture<OperationSnapshot> pollingFuture = future.getPollingFuture(); |
| 112 | + OperationSnapshot currentOp = pollingFuture.getAttemptResult().get(); |
| 113 | + System.out.printf("Polling Operation:\nName: %s\n Done: %s\n", |
| 114 | + currentOp.getName(), |
| 115 | + currentOp.isDone()); |
| 116 | + } |
| 117 | + |
| 118 | + // NOTE: future.get() blocks completion until the operation is complete (isDone |
| 119 | + // = True) |
| 120 | + ConnectCluster response = future.get(); |
| 121 | + System.out.printf("Created connect cluster: %s\n", response.getName()); |
| 122 | + } catch (ExecutionException e) { |
| 123 | + System.err.printf("managedKafkaConnectClient.createConnectCluster got err: %s\n", |
| 124 | + e.getMessage()); |
| 125 | + throw e; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +// [END managedkafka_create_connect_cluster] |
0 commit comments