com.google.api.gax.core.ExecutorProvider Java Examples

The following examples show how to use com.google.api.gax.core.ExecutorProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public PublisherFactory defaultPublisherFactory(
		@Qualifier("publisherExecutorProvider") ExecutorProvider executorProvider,
		@Qualifier("publisherBatchSettings") ObjectProvider<BatchingSettings> batchingSettings,
		@Qualifier("publisherRetrySettings") ObjectProvider<RetrySettings> retrySettings,
		TransportChannelProvider transportChannelProvider) {
	DefaultPublisherFactory factory = new DefaultPublisherFactory(this.finalProjectIdProvider);
	factory.setExecutorProvider(executorProvider);
	factory.setCredentialsProvider(this.finalCredentialsProvider);
	factory.setHeaderProvider(this.headerProvider);
	factory.setChannelProvider(transportChannelProvider);
	retrySettings.ifAvailable(factory::setRetrySettings);
	batchingSettings.ifAvailable(factory::setBatchingSettings);
	return factory;
}
 
Example #2
Source File: StackdriverTraceAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "traceExecutorProvider")
public ExecutorProvider traceExecutorProvider(GcpTraceProperties traceProperties, @Qualifier("traceSenderThreadPool") Optional<ThreadPoolTaskScheduler> userProvidedScheduler) {
	ThreadPoolTaskScheduler scheduler;
	if (userProvidedScheduler.isPresent()) {
		scheduler = userProvidedScheduler.get();
	}
	else {
		this.defaultTraceSenderThreadPool = new ThreadPoolTaskScheduler();
		scheduler = this.defaultTraceSenderThreadPool;
		scheduler.setPoolSize(traceProperties.getNumExecutorThreads());
		scheduler.setThreadNamePrefix("gcp-trace-sender");
		scheduler.setDaemon(true);
		scheduler.initialize();
	}
	return FixedExecutorProvider.create(scheduler.getScheduledExecutor());
}
 
Example #3
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public SubscriberFactory defaultSubscriberFactory(
		@Qualifier("subscriberExecutorProvider") ExecutorProvider executorProvider,
		@Qualifier("subscriberSystemExecutorProvider")
		ObjectProvider<ExecutorProvider> systemExecutorProvider,
		@Qualifier("subscriberFlowControlSettings")
				ObjectProvider<FlowControlSettings> flowControlSettings,
		@Qualifier("subscriberApiClock") ObjectProvider<ApiClock> apiClock,
		@Qualifier("subscriberRetrySettings") ObjectProvider<RetrySettings> retrySettings,
		TransportChannelProvider transportChannelProvider) {
	DefaultSubscriberFactory factory = new DefaultSubscriberFactory(this.finalProjectIdProvider);
	factory.setExecutorProvider(executorProvider);
	factory.setCredentialsProvider(this.finalCredentialsProvider);
	factory.setHeaderProvider(this.headerProvider);
	factory.setChannelProvider(transportChannelProvider);
	systemExecutorProvider.ifAvailable(factory::setSystemExecutorProvider);
	flowControlSettings.ifAvailable(factory::setFlowControlSettings);
	apiClock.ifAvailable(factory::setApiClock);
	retrySettings.ifAvailable(factory::setSubscriberStubRetrySettings);
	if (this.gcpPubSubProperties.getSubscriber().getMaxAckExtensionPeriod() != null) {
		factory.setMaxAckExtensionPeriod(Duration.ofSeconds(
				this.gcpPubSubProperties.getSubscriber().getMaxAckExtensionPeriod()));
	}
	if (this.gcpPubSubProperties.getSubscriber().getParallelPullCount() != null) {
		factory.setParallelPullCount(
				this.gcpPubSubProperties.getSubscriber().getParallelPullCount());
	}
	if (this.gcpPubSubProperties.getSubscriber()
			.getPullEndpoint() != null) {
		factory.setPullEndpoint(
				this.gcpPubSubProperties.getSubscriber().getPullEndpoint());
	}
	return factory;
}
 
Example #4
Source File: StackdriverTraceAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultSchedulerUsedWhenNoneProvided() {
	this.contextRunner
			.run(context -> {
				final ExecutorProvider executorProvider = context.getBean("traceExecutorProvider", ExecutorProvider.class);
				assertThat(executorProvider.getExecutor()).isNotNull();
			});
}
 
Example #5
Source File: StackdriverTraceAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void customSchedulerUsedWhenAvailable() {
	ThreadPoolTaskScheduler threadPoolTaskSchedulerMock = mock(ThreadPoolTaskScheduler.class);
	ScheduledExecutorService scheduledExecutorServiceMock = mock(ScheduledExecutorService.class);
	when(threadPoolTaskSchedulerMock.getScheduledExecutor()).thenReturn(scheduledExecutorServiceMock);

	this.contextRunner
			.withBean("traceSenderThreadPool", ThreadPoolTaskScheduler.class, () -> threadPoolTaskSchedulerMock)
			.run(context -> {
				final ExecutorProvider executorProvider = context.getBean("traceExecutorProvider", ExecutorProvider.class);
				assertThat(executorProvider.getExecutor()).isEqualTo(scheduledExecutorServiceMock);
			});
}
 
Example #6
Source File: PhotosLibrarySettings.java    From java-photoslibrary with Apache License 2.0 4 votes vote down vote up
@Override
public Builder setExecutorProvider(ExecutorProvider executorProvider) {
  super.setExecutorProvider(executorProvider);
  return this;
}
 
Example #7
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "publisherExecutorProvider")
public ExecutorProvider publisherExecutorProvider(
		@Qualifier("pubsubPublisherThreadPool") ThreadPoolTaskScheduler scheduler) {
	return FixedExecutorProvider.create(scheduler.getScheduledExecutor());
}
 
Example #8
Source File: GcpPubSubAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "subscriberExecutorProvider")
public ExecutorProvider subscriberExecutorProvider(
		@Qualifier("pubsubSubscriberThreadPool") ThreadPoolTaskScheduler scheduler) {
	return FixedExecutorProvider.create(scheduler.getScheduledExecutor());
}
 
Example #9
Source File: StackdriverTraceAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Bean(SENDER_BEAN_NAME)
@ConditionalOnMissingBean(name = SENDER_BEAN_NAME)
public Sender stackdriverSender(GcpTraceProperties traceProperties,
		@Qualifier("traceExecutorProvider") ExecutorProvider executorProvider,
		@Qualifier("stackdriverSenderChannel") ManagedChannel channel)
		throws IOException {
	CallOptions callOptions = CallOptions.DEFAULT
			.withCallCredentials(
					MoreCallCredentials.from(
							this.finalCredentialsProvider.getCredentials()))
			.withExecutor(executorProvider.getExecutor());

	if (traceProperties.getAuthority() != null) {
		callOptions = callOptions.withAuthority(traceProperties.getAuthority());
	}

	if (traceProperties.getCompression() != null) {
		callOptions = callOptions.withCompression(traceProperties.getCompression());
	}

	if (traceProperties.getDeadlineMs() != null) {
		callOptions = callOptions.withDeadlineAfter(traceProperties.getDeadlineMs(), TimeUnit.MILLISECONDS);
	}

	if (traceProperties.getMaxInboundSize() != null) {
		callOptions = callOptions.withMaxInboundMessageSize(traceProperties.getMaxInboundSize());
	}

	if (traceProperties.getMaxOutboundSize() != null) {
		callOptions = callOptions.withMaxOutboundMessageSize(traceProperties.getMaxOutboundSize());
	}

	if (traceProperties.isWaitForReady() != null) {
		if (Boolean.TRUE.equals(traceProperties.isWaitForReady())) {
			callOptions = callOptions.withWaitForReady();
		}
		else {
			callOptions = callOptions.withoutWaitForReady();
		}
	}

	return StackdriverSender.newBuilder(channel)
			.projectId(this.finalProjectIdProvider.getProjectId())
			.callOptions(callOptions)
			.build();
}
 
Example #10
Source File: Connection.java    From heroic with Apache License 2.0 4 votes vote down vote up
Connection start() {
    log.info("Starting PubSub connection");

    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName
        .of(projectId, subscriptionId);

    FlowControlSettings flowControlSettings =
        FlowControlSettings.newBuilder()
            .setMaxOutstandingElementCount(maxOutstandingElementCount)
            .setMaxOutstandingRequestBytes(maxOutstandingRequestBytes)
            .build();

    ExecutorProvider executorProvider =
        InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(threads).build();


    log.info("Subscribing to {}", subscriptionName);
    final Receiver receiver = new Receiver(consumer, reporter, errors, consumed);
    subscriber = Subscriber
        .newBuilder(subscriptionName, receiver)
        .setFlowControlSettings(flowControlSettings)
        .setParallelPullCount(threads)
        .setExecutorProvider(executorProvider)
        .setChannelProvider(channelProvider)
        .setCredentialsProvider(credentialsProvider)
        .build();

    subscriber.addListener(
      new Subscriber.Listener() {
          @Override
          public void failed(Subscriber.State from, Throwable failure) {
              // Called when the Subscriber encountered a fatal error and is shutting down
              log.error(
                "An error on subscriber happened (from state: " + from.name() + ")", failure);
              System.exit(1);
          } }, MoreExecutors.directExecutor());


    subscriber.startAsync().awaitRunning();

    log.info("PubSub connection started");
    return this;
}
 
Example #11
Source File: PubSubSource.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public void produce(Map<String, String> lastOffsets, int maxBatchSize) throws StageException {
  SynchronousQueue<MessageReplyConsumerBundle> workQueue = new SynchronousQueue<>();

  ProjectSubscriptionName subscriptionName = ProjectSubscriptionName
      .of(
          conf.credentials.projectId,
          conf.subscriptionId
      );

  executor = Executors.newFixedThreadPool(getNumberOfThreads());

  int batchSize = Math.min(maxBatchSize, conf.basic.maxBatchSize);
  if (!getContext().isPreview() && conf.basic.maxBatchSize > maxBatchSize) {
    getContext().reportError(Errors.PUBSUB_10, maxBatchSize);
  }

  for (int i = 0; i < conf.maxThreads; i++) {
    MessageProcessor messageProcessor = new MessageProcessorImpl(
        getContext(),
        batchSize,
        conf.basic.maxWaitTime,
        parserFactory,
        workQueue
    );
    executor.submit(messageProcessor);
    messageProcessors.add(messageProcessor);
  }

  ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder()
      .setExecutorThreadCount(conf.advanced.numThreadsPerSubscriber)
      .build();

  InstantiatingGrpcChannelProvider channelProvider = getChannelProvider();

  FlowControlSettings flowControlSettings = getFlowControlSettings();

  for (int i = 0; i < conf.advanced.numSubscribers; i++) {
    Subscriber s = Subscriber.newBuilder(subscriptionName, new MessageReceiverImpl(workQueue))
        .setCredentialsProvider(credentialsProvider)
        .setExecutorProvider(executorProvider)
        .setChannelProvider(channelProvider)
        .setFlowControlSettings(flowControlSettings)
        .build();
    s.addListener(new Subscriber.Listener() {
      @Override
      public void failed(Subscriber.State from, Throwable failure) {
        LOG.error("Exception thrown in Subscriber: {}", failure.toString(), failure);
        LOG.error("Subscriber state: {}", from.toString());
        Throwables.propagate(failure);
      }
    }, MoreExecutors.directExecutor());
    subscribers.add(s);
  }

  try {
    subscribers.forEach(Subscriber::startAsync);
  } finally {
    LOG.info("Started {} subscribers.", conf.maxThreads);
  }

  while (!getContext().isStopped()) {
    ThreadUtil.sleep(1000);
  }
}
 
Example #12
Source File: DefaultPublisherFactory.java    From spring-cloud-gcp with Apache License 2.0 2 votes vote down vote up
/**
 * Set the provider for the executor that will be used by the publisher. Useful to specify the number of threads to
 * be used by each executor.
 * @param executorProvider the executor provider to set
 */
public void setExecutorProvider(ExecutorProvider executorProvider) {
	this.executorProvider = executorProvider;
}
 
Example #13
Source File: DefaultSubscriberFactory.java    From spring-cloud-gcp with Apache License 2.0 2 votes vote down vote up
/**
 * Set the provider for the subscribers' executor. Useful to specify the number of threads to be
 * used by each executor.
 * @param executorProvider the executor provider to set
 */
public void setExecutorProvider(ExecutorProvider executorProvider) {
	this.executorProvider = executorProvider;
}
 
Example #14
Source File: DefaultSubscriberFactory.java    From spring-cloud-gcp with Apache License 2.0 2 votes vote down vote up
/**
 * Set the provider for the system executor, to poll and manage lease extensions.
 * @param systemExecutorProvider the system executor provider to set
 */
public void setSystemExecutorProvider(ExecutorProvider systemExecutorProvider) {
	this.systemExecutorProvider = systemExecutorProvider;
}