|
| 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 com.optimizationai; |
| 18 | + |
| 19 | +// [START cloudoptimization_async_api] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.optimization.v1.AsyncModelMetadata; |
| 22 | +import com.google.cloud.optimization.v1.BatchOptimizeToursRequest; |
| 23 | +import com.google.cloud.optimization.v1.BatchOptimizeToursRequest.AsyncModelConfig; |
| 24 | +import com.google.cloud.optimization.v1.BatchOptimizeToursResponse; |
| 25 | +import com.google.cloud.optimization.v1.DataFormat; |
| 26 | +import com.google.cloud.optimization.v1.FleetRoutingClient; |
| 27 | +import com.google.cloud.optimization.v1.GcsDestination; |
| 28 | +import com.google.cloud.optimization.v1.GcsSource; |
| 29 | +import com.google.cloud.optimization.v1.InputConfig; |
| 30 | +import com.google.cloud.optimization.v1.OutputConfig; |
| 31 | + |
| 32 | +/** |
| 33 | + * This is an example to send a request to Cloud Fleet Routing asynchronous API via Java API Client. |
| 34 | + * A sample async_request_java.textproto file and a sample request_model_java.json file can be found |
| 35 | + * in the resources folder. |
| 36 | + */ |
| 37 | +public class AsyncApi { |
| 38 | + public static void callAsyncApi() throws Exception { |
| 39 | + // TODO(developer): Replace these variables before running the sample. |
| 40 | + String projectParent = "projects/{YOUR_GCP_PROJECT_ID}"; |
| 41 | + String inputUri = "gs://YOUR_GCS_PATH"; |
| 42 | + String outputUri = "gs://YOUR_SOLUTION_PATH"; |
| 43 | + callAsyncApi(projectParent, inputUri, outputUri); |
| 44 | + } |
| 45 | + |
| 46 | + public static void callAsyncApi(String projectParent, String inputUri, String outputUri) |
| 47 | + throws Exception { |
| 48 | + GcsSource gcsSource = GcsSource.newBuilder().setUri(inputUri).build(); |
| 49 | + InputConfig inputConfig = |
| 50 | + InputConfig.newBuilder().setGcsSource(gcsSource).setDataFormat(DataFormat.JSON).build(); |
| 51 | + GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build(); |
| 52 | + OutputConfig outputConfig = |
| 53 | + OutputConfig.newBuilder() |
| 54 | + .setGcsDestination(gcsDestination) |
| 55 | + .setDataFormat(DataFormat.JSON) |
| 56 | + .build(); |
| 57 | + |
| 58 | + AsyncModelConfig asyncModelConfig = |
| 59 | + AsyncModelConfig.newBuilder() |
| 60 | + .setInputConfig(inputConfig) |
| 61 | + .setOutputConfig(outputConfig) |
| 62 | + .build(); |
| 63 | + BatchOptimizeToursRequest request = |
| 64 | + BatchOptimizeToursRequest.newBuilder() |
| 65 | + .setParent(projectParent) |
| 66 | + .addModelConfigs(asyncModelConfig) |
| 67 | + .build(); |
| 68 | + |
| 69 | + FleetRoutingClient fleetRoutingClient = FleetRoutingClient.create(); |
| 70 | + OperationFuture<BatchOptimizeToursResponse, AsyncModelMetadata> response = |
| 71 | + fleetRoutingClient.batchOptimizeToursAsync(request); |
| 72 | + System.out.format("the response name: %s\n", response.getInitialFuture().get().getName()); |
| 73 | + |
| 74 | + // Block to wait for the job to finish. |
| 75 | + response.getPollingFuture().get(); |
| 76 | + if (response.getMetadata().get().getState() == AsyncModelMetadata.State.SUCCEEDED) { |
| 77 | + // Code to do your stuff |
| 78 | + System.out.println("Job finished successfully."); |
| 79 | + } else { |
| 80 | + System.out.println( |
| 81 | + "Job failed with message:" + response.getPollingFuture().get().getErrorMessage()); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +// [END cloudoptimization_async_api] |
0 commit comments