@@ -70,8 +70,6 @@ public class AsyncRecognizeClient {
7070 private static final List <String > OAUTH2_SCOPES =
7171 Arrays .asList ("https://www.googleapis.com/auth/cloud-platform" );
7272
73- private final String host ;
74- private final int port ;
7573 private final URI input ;
7674 private final int samplingRate ;
7775
@@ -82,24 +80,14 @@ public class AsyncRecognizeClient {
8280 /**
8381 * Construct client connecting to Cloud Speech server at {@code host:port}.
8482 */
85- public AsyncRecognizeClient (String host , int port , URI input , int samplingRate )
83+ public AsyncRecognizeClient (ManagedChannel channel , URI input , int samplingRate )
8684 throws IOException {
87- this .host = host ;
88- this .port = port ;
8985 this .input = input ;
9086 this .samplingRate = samplingRate ;
87+ this .channel = channel ;
9188
92- GoogleCredentials creds = GoogleCredentials .getApplicationDefault ();
93- creds = creds .createScoped (OAUTH2_SCOPES );
94- channel =
95- NettyChannelBuilder .forAddress (host , port )
96- .negotiationType (NegotiationType .TLS )
97- .intercept (new ClientAuthInterceptor (creds , Executors .newSingleThreadExecutor ()))
98- .build ();
9989 speechClient = SpeechGrpc .newBlockingStub (channel );
10090 statusClient = OperationsGrpc .newBlockingStub (channel );
101-
102- logger .info ("Created speech clientfor " + host + ":" + port );
10391 }
10492
10593 public void shutdown () throws InterruptedException {
@@ -236,8 +224,16 @@ public static void main(String[] args) throws Exception {
236224 System .exit (1 );
237225 }
238226
227+ GoogleCredentials creds = GoogleCredentials .getApplicationDefault ();
228+ creds = creds .createScoped (OAUTH2_SCOPES );
229+ ManagedChannel channel =
230+ NettyChannelBuilder .forAddress (host , port )
231+ .negotiationType (NegotiationType .TLS )
232+ .intercept (new ClientAuthInterceptor (creds , Executors .newSingleThreadExecutor ()))
233+ .build ();
234+
239235 AsyncRecognizeClient client =
240- new AsyncRecognizeClient (host , port , URI .create (audioFile ), sampling );
236+ new AsyncRecognizeClient (channel , URI .create (audioFile ), sampling );
241237 try {
242238 client .recognize ();
243239 } finally {
0 commit comments