use of com.google.api.gax.core.ExecutorAsBackgroundResource in project gax-java by googleapis.
the class ClientContext method create.
/**
* Instantiates the executor, credentials, and transport context based on the given client
* settings.
*/
public static ClientContext create(StubSettings settings) throws IOException {
ApiClock clock = settings.getClock();
ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider();
final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor();
Credentials credentials = settings.getCredentialsProvider().getCredentials();
if (settings.getQuotaProjectId() != null) {
// If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as
// QuotaProjectIdHidingCredentials.
// Ensure that a custom set quota project id takes priority over one detected by credentials.
// Avoid the backend receiving possibly conflict values of quotaProjectId
credentials = new QuotaProjectIdHidingCredentials(credentials);
}
TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider();
// will have a default executor if it needs one.
if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) {
transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor);
}
Map<String, String> headers = getHeadersFromSettings(settings);
if (transportChannelProvider.needsHeaders()) {
transportChannelProvider = transportChannelProvider.withHeaders(headers);
}
if (transportChannelProvider.needsCredentials() && credentials != null) {
transportChannelProvider = transportChannelProvider.withCredentials(credentials);
}
String endpoint = getEndpoint(settings.getEndpoint(), settings.getMtlsEndpoint(), settings.getSwitchToMtlsEndpointAllowed(), new MtlsProvider());
if (transportChannelProvider.needsEndpoint()) {
transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
}
TransportChannel transportChannel = transportChannelProvider.getTransportChannel();
ApiCallContext defaultCallContext = transportChannel.getEmptyCallContext().withTransportChannel(transportChannel);
if (credentials != null) {
defaultCallContext = defaultCallContext.withCredentials(credentials);
}
WatchdogProvider watchdogProvider = settings.getStreamWatchdogProvider();
@Nullable Watchdog watchdog = null;
if (watchdogProvider != null) {
if (watchdogProvider.needsCheckInterval()) {
watchdogProvider = watchdogProvider.withCheckInterval(settings.getStreamWatchdogCheckInterval());
}
if (watchdogProvider.needsClock()) {
watchdogProvider = watchdogProvider.withClock(clock);
}
if (watchdogProvider.needsExecutor()) {
watchdogProvider = watchdogProvider.withExecutor(backgroundExecutor);
}
watchdog = watchdogProvider.getWatchdog();
}
ImmutableList.Builder<BackgroundResource> backgroundResources = ImmutableList.builder();
if (transportChannelProvider.shouldAutoClose()) {
backgroundResources.add(transportChannel);
}
if (backgroundExecutorProvider.shouldAutoClose()) {
backgroundResources.add(new ExecutorAsBackgroundResource(backgroundExecutor));
}
if (watchdogProvider != null && watchdogProvider.shouldAutoClose()) {
backgroundResources.add(watchdog);
}
return newBuilder().setBackgroundResources(backgroundResources.build()).setExecutor(backgroundExecutor).setCredentials(credentials).setTransportChannel(transportChannel).setHeaders(ImmutableMap.copyOf(settings.getHeaderProvider().getHeaders())).setInternalHeaders(ImmutableMap.copyOf(settings.getInternalHeaderProvider().getHeaders())).setClock(clock).setDefaultCallContext(defaultCallContext).setEndpoint(settings.getEndpoint()).setQuotaProjectId(settings.getQuotaProjectId()).setStreamWatchdog(watchdog).setStreamWatchdogCheckInterval(settings.getStreamWatchdogCheckInterval()).setTracerFactory(settings.getTracerFactory()).build();
}
use of com.google.api.gax.core.ExecutorAsBackgroundResource in project java-pubsub by googleapis.
the class Subscriber method startStreamingConnections.
private void startStreamingConnections() {
synchronized (streamingSubscriberConnections) {
for (int i = 0; i < numPullers; i++) {
final ScheduledExecutorService executor = executorProvider.getExecutor();
if (executorProvider.shouldAutoClose()) {
backgroundResources.add(new ExecutorAsBackgroundResource((executor)));
}
StreamingSubscriberConnection.Builder streamingSubscriberConnectionBuilder;
if (receiverWithAckResponse != null) {
streamingSubscriberConnectionBuilder = StreamingSubscriberConnection.newBuilder(receiverWithAckResponse);
} else {
streamingSubscriberConnectionBuilder = StreamingSubscriberConnection.newBuilder(receiver);
}
StreamingSubscriberConnection streamingSubscriberConnection = streamingSubscriberConnectionBuilder.setSubscription(subscriptionName).setAckExpirationPadding(ACK_EXPIRATION_PADDING_DEFAULT).setMaxAckExtensionPeriod(maxAckExtensionPeriod).setMinDurationPerAckExtension(minDurationPerAckExtension).setMinDurationPerAckExtensionDefaultUsed(minDurationPerAckExtensionDefaultUsed).setMaxDurationPerAckExtension(maxDurationPerAckExtension).setMaxDurationPerAckExtensionDefaultUsed(maxDurationPerAckExtensionDefaultUsed).setAckLatencyDistribution(ackLatencyDistribution).setSubscriberStub(subscriberStub).setChannelAffinity(i).setFlowControlSettings(flowControlSettings).setFlowController(flowController).setUseLegacyFlowControl(useLegacyFlowControl).setExecutor(executor).setSystemExecutor(alarmsExecutor).setClock(clock).build();
streamingSubscriberConnections.add(streamingSubscriberConnection);
}
startConnections(streamingSubscriberConnections, new Listener() {
@Override
public void failed(State from, Throwable failure) {
// If a connection failed is because of a fatal error, we should fail the
// whole subscriber.
runShutdown();
try {
notifyFailed(failure);
} catch (IllegalStateException e) {
if (isRunning()) {
throw e;
}
// It could happen that we are shutting down while some channels fail.
}
}
});
}
}
Aggregations