Skip to content

Commit e81d249

Browse files
committed
DIP for channel
1 parent c3b811e commit e81d249

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed

speech/grpc/src/main/java/com/google/cloud/speech/grpc/demos/AsyncRecognizeClient.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ public void shutdown() throws InterruptedException {
9494
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
9595
}
9696

97+
public static ManagedChannel createChannel(String host, int port) throws IOException {
98+
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
99+
creds = creds.createScoped(OAUTH2_SCOPES);
100+
ManagedChannel channel =
101+
NettyChannelBuilder.forAddress(host, port)
102+
.negotiationType(NegotiationType.TLS)
103+
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
104+
.build();
105+
106+
return channel;
107+
108+
}
109+
97110
/**
98111
* Sends a request to the speech API and returns an Operation handle.
99112
*/
@@ -224,13 +237,7 @@ public static void main(String[] args) throws Exception {
224237
System.exit(1);
225238
}
226239

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();
240+
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
234241

235242
AsyncRecognizeClient client =
236243
new AsyncRecognizeClient(channel, URI.create(audioFile), sampling);

speech/grpc/src/main/java/com/google/cloud/speech/grpc/demos/StreamingRecognizeClient.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
*/
6666
public class StreamingRecognizeClient {
6767

68-
private final String host;
69-
private final int port;
7068
private final String file;
7169
private final int samplingRate;
7270

@@ -82,22 +80,13 @@ public class StreamingRecognizeClient {
8280
/**
8381
* Construct client connecting to Cloud Speech server at {@code host:port}.
8482
*/
85-
public StreamingRecognizeClient(String host, int port, String file, int samplingRate)
83+
public StreamingRecognizeClient(ManagedChannel channel, String file, int samplingRate)
8684
throws IOException {
87-
this.host = host;
88-
this.port = port;
8985
this.file = file;
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.newStub(channel);
100-
logger.info("Created speech client for " + host + ":" + port);
10190
}
10291

10392
public void shutdown() throws InterruptedException {
@@ -248,7 +237,8 @@ public static void main(String[] args) throws Exception {
248237
System.exit(1);
249238
}
250239

251-
StreamingRecognizeClient client = new StreamingRecognizeClient(host, port, audioFile, sampling);
240+
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
241+
StreamingRecognizeClient client = new StreamingRecognizeClient(channel, audioFile, sampling);
252242
try {
253243
client.recognize();
254244
} finally {

speech/grpc/src/main/java/com/google/cloud/speech/grpc/demos/SyncRecognizeClient.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public class SyncRecognizeClient {
6666
private static final List<String> OAUTH2_SCOPES =
6767
Arrays.asList("https://www.googleapis.com/auth/cloud-platform");
6868

69-
private final String host;
70-
private final int port;
7169
private final URI input;
7270
private final int samplingRate;
7371

@@ -77,22 +75,13 @@ public class SyncRecognizeClient {
7775
/**
7876
* Construct client connecting to Cloud Speech server at {@code host:port}.
7977
*/
80-
public SyncRecognizeClient(String host, int port, URI input, int samplingRate)
78+
public SyncRecognizeClient(ManagedChannel channel, URI input, int samplingRate)
8179
throws IOException {
82-
this.host = host;
83-
this.port = port;
8480
this.input = input;
8581
this.samplingRate = samplingRate;
82+
this.channel = channel;
8683

87-
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
88-
creds = creds.createScoped(OAUTH2_SCOPES);
89-
channel =
90-
NettyChannelBuilder.forAddress(host, port)
91-
.negotiationType(NegotiationType.TLS)
92-
.intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor()))
93-
.build();
9484
speechClient = SpeechGrpc.newBlockingStub(channel);
95-
logger.info("Created speech client for " + host + ":" + port);
9685
}
9786

9887
private RecognitionAudio createRecognitionAudio() throws IOException {
@@ -200,8 +189,9 @@ public static void main(String[] args) throws Exception {
200189
System.exit(1);
201190
}
202191

192+
ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port);
203193
SyncRecognizeClient client =
204-
new SyncRecognizeClient(host, port, URI.create(audioFile), sampling);
194+
new SyncRecognizeClient(channel, URI.create(audioFile), sampling);
205195
try {
206196
client.recognize();
207197
} finally {

0 commit comments

Comments
 (0)