|
| 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.example.optimizationai; |
| 18 | + |
| 19 | +// [START cloudoptimization_long_timeout] |
| 20 | + |
| 21 | +import com.google.cloud.optimization.v1.FleetRoutingClient; |
| 22 | +import com.google.cloud.optimization.v1.FleetRoutingSettings; |
| 23 | +import com.google.cloud.optimization.v1.OptimizeToursRequest; |
| 24 | +import com.google.cloud.optimization.v1.OptimizeToursResponse; |
| 25 | +import com.google.protobuf.Duration; |
| 26 | +import com.google.protobuf.TextFormat; |
| 27 | +import java.io.FileInputStream; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.io.InputStreamReader; |
| 30 | +import java.io.Reader; |
| 31 | + |
| 32 | +/** |
| 33 | + * This is an example to send a request to Cloud Fleet Routing synchronous API via Java API Client. |
| 34 | + */ |
| 35 | +public class SyncApiWithLongTimeout { |
| 36 | + public static void longTimeout() throws Exception { |
| 37 | + // TODO(developer): Replace these variables before running the sample. |
| 38 | + String projectParent = "projects/{YOUR_GCP_PROJECT_ID}"; |
| 39 | + String modelPath = "YOUR_MODEL_PATH"; |
| 40 | + longTimeout(projectParent, modelPath); |
| 41 | + } |
| 42 | + |
| 43 | + public static void longTimeout(String projectParent, String modelPath) throws Exception { |
| 44 | + int timeoutSeconds = 100; |
| 45 | + InputStream modelInputstream = new FileInputStream(modelPath); |
| 46 | + Reader modelInputStreamReader = new InputStreamReader(modelInputstream); |
| 47 | + OptimizeToursRequest.Builder requestBuilder = |
| 48 | + OptimizeToursRequest.newBuilder() |
| 49 | + .setTimeout(Duration.newBuilder().setSeconds(timeoutSeconds).build()) |
| 50 | + .setParent(projectParent); |
| 51 | + TextFormat.getParser().merge(modelInputStreamReader, requestBuilder); |
| 52 | + |
| 53 | + // Checks the gRPC connection every 5 mins and keeps it alive. |
| 54 | + FleetRoutingClient fleetRoutingClientClient = |
| 55 | + FleetRoutingClient.create( |
| 56 | + FleetRoutingSettings.newBuilder() |
| 57 | + .setTransportChannelProvider( |
| 58 | + FleetRoutingSettings.defaultGrpcTransportProviderBuilder() |
| 59 | + .setKeepAliveTime(org.threeten.bp.Duration.ofSeconds(300)) |
| 60 | + .build()) |
| 61 | + .build()); |
| 62 | + OptimizeToursResponse response = fleetRoutingClientClient.optimizeTours(requestBuilder.build()); |
| 63 | + System.out.println(response.toString()); |
| 64 | + } |
| 65 | +} |
| 66 | +// [END cloudoptimization_long_timeout] |
0 commit comments