private Publisher createPublisherWithCustomCredentials(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_custom_credentials] // read service account credentials from file CredentialsProvider credentialsProvider = FixedCredentialsProvider.create( ServiceAccountCredentials.fromStream(new FileInputStream("credentials.json"))); Publisher publisher = Publisher.newBuilder(topicName).setCredentialsProvider(credentialsProvider).build(); // [END pubsub_publisher_custom_credentials] return publisher; } }
Publisher.newBuilder(topicName) .setChannelProvider(channelProvider) .setCredentialsProvider(credentialsProvider) .build(); } finally {
@Provides @Singleton public Publisher publisher() throws IOException { // Publish request based on request size, messages count & time since last publish BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setElementCountThreshold(messageCountBatchSize) .setRequestByteThreshold(requestBytesThreshold) .setDelayThreshold(publishDelayThreshold) .build(); ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder() .setExecutorThreadCount(1).build(); final Publisher.Builder publisher = Publisher.newBuilder(topicName()) .setBatchingSettings(batchingSettings) .setExecutorProvider(executorProvider); final String emulatorHost = System.getenv("PUBSUB_EMULATOR_HOST"); if (emulatorHost != null) { ManagedChannel channel = ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build(); TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel)); publisher.setChannelProvider(channelProvider); publisher.setCredentialsProvider(NoCredentialsProvider.create()); } return publisher.build(); }
publisherBuilder.setCredentialsProvider(this.credentialsProvider);
private void createPublisher() { ProjectTopicName fullTopic = ProjectTopicName.of(cpsProject, cpsTopic); com.google.cloud.pubsub.v1.Publisher.Builder builder = com.google.cloud.pubsub.v1.Publisher.newBuilder(fullTopic) .setCredentialsProvider(gcpCredentialsProvider) .setBatchingSettings( BatchingSettings.newBuilder() .setDelayThreshold(Duration.ofMillis(maxDelayThresholdMs)) .setElementCountThreshold(maxBufferSize) .setRequestByteThreshold(maxBufferBytes) .build()) .setRetrySettings( RetrySettings.newBuilder() // All values that are not configurable come from the defaults for the publisher // client library. .setTotalTimeout(Duration.ofMillis(maxTotalTimeoutMs)) .setMaxRpcTimeout(Duration.ofMillis(maxRequestTimeoutMs)) .setInitialRetryDelay(Duration.ofMillis(5)) .setRetryDelayMultiplier(2) .setMaxRetryDelay(Duration.ofMillis(Long.MAX_VALUE)) .setInitialRpcTimeout(Duration.ofSeconds(10)) .setRpcTimeoutMultiplier(2) .build()); try { publisher = builder.build(); } catch (Exception e) { throw new RuntimeException(e); } }
publisherBuilder.setCredentialsProvider(this.credentialsProvider);
public static GooglePubsubPublisher buildPublisher(GooglePubsubPublisherConfig config, ObjectMapper mapper) { GooglePubsubPublisher publisher = new GooglePubsubPublisher(); publisher.setName(config.getName()); ProjectTopicName fullName = ProjectTopicName.of(config.getProject(), config.getTopicName()); publisher.setTopicName(config.getTopicName()); publisher.setFullTopicName(fullName.toString()); publisher.setContent(config.getContent()); publisher.setMapper(mapper); BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setElementCountThreshold(config.getBatchCountThreshold()) .setDelayThreshold(Duration.ofMillis(config.getDelayMillisecondsThreshold())) .build(); try { Publisher p = Publisher.newBuilder(fullName) .setCredentialsProvider(new GooglePubsubCredentialsProvider(config.getJsonPath())) .setBatchingSettings(batchingSettings) .build(); publisher.setPublisher(p); } catch (IOException ioe) { log.error("Could not create Google Pubsub Publishers: {}", ioe); } return publisher; }
@Test public void testPublisherGetters() throws Exception { Publisher.Builder builder = Publisher.newBuilder(TEST_TOPIC); builder.setChannelProvider(TEST_CHANNEL_PROVIDER); builder.setExecutorProvider(SINGLE_THREAD_EXECUTOR); builder.setBatchingSettings( BatchingSettings.newBuilder() .setRequestByteThreshold(10L) .setDelayThreshold(Duration.ofMillis(11)) .setElementCountThreshold(12L) .build()); builder.setCredentialsProvider(NoCredentialsProvider.create()); Publisher publisher = builder.build(); assertEquals(TEST_TOPIC, publisher.getTopicName()); assertEquals(10, (long) publisher.getBatchingSettings().getRequestByteThreshold()); assertEquals(Duration.ofMillis(11), publisher.getBatchingSettings().getDelayThreshold()); assertEquals(12, (long) publisher.getBatchingSettings().getElementCountThreshold()); publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); }
private Builder getTestPublisherBuilder() { return Publisher.newBuilder(TEST_TOPIC) .setExecutorProvider(FixedExecutorProvider.create(fakeExecutor)) .setChannelProvider(TEST_CHANNEL_PROVIDER) .setCredentialsProvider(NoCredentialsProvider.create()); } }
private Publisher.Builder getPublisherBuilder(ProcessContext context) { final Long batchSize = context.getProperty(BATCH_SIZE).asLong(); return Publisher.newBuilder(getTopicName(context)) .setCredentialsProvider(FixedCredentialsProvider.create(getGoogleCredentials(context))) .setBatchingSettings(BatchingSettings.newBuilder() .setElementCountThreshold(batchSize) .setIsEnabled(true) .build()); } }