@@ -70,8 +70,6 @@ public class AsyncRecognizeClient {
70
70
private static final List <String > OAUTH2_SCOPES =
71
71
Arrays .asList ("https://www.googleapis.com/auth/cloud-platform" );
72
72
73
- private final String host ;
74
- private final int port ;
75
73
private final URI input ;
76
74
private final int samplingRate ;
77
75
@@ -82,24 +80,14 @@ public class AsyncRecognizeClient {
82
80
/**
83
81
* Construct client connecting to Cloud Speech server at {@code host:port}.
84
82
*/
85
- public AsyncRecognizeClient (String host , int port , URI input , int samplingRate )
83
+ public AsyncRecognizeClient (ManagedChannel channel , URI input , int samplingRate )
86
84
throws IOException {
87
- this .host = host ;
88
- this .port = port ;
89
85
this .input = input ;
90
86
this .samplingRate = samplingRate ;
87
+ this .channel = channel ;
91
88
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 ();
99
89
speechClient = SpeechGrpc .newBlockingStub (channel );
100
90
statusClient = OperationsGrpc .newBlockingStub (channel );
101
-
102
- logger .info ("Created speech clientfor " + host + ":" + port );
103
91
}
104
92
105
93
public void shutdown () throws InterruptedException {
@@ -236,8 +224,16 @@ public static void main(String[] args) throws Exception {
236
224
System .exit (1 );
237
225
}
238
226
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
+
239
235
AsyncRecognizeClient client =
240
- new AsyncRecognizeClient (host , port , URI .create (audioFile ), sampling );
236
+ new AsyncRecognizeClient (channel , URI .create (audioFile ), sampling );
241
237
try {
242
238
client .recognize ();
243
239
} finally {
0 commit comments