/** * Constructs a new {@link Builder} using the given topic. * * <p>Example of creating a {@code Publisher}. * * <pre>{@code * String projectName = "my_project"; * String topicName = "my_topic"; * ProjectTopicName topic = ProjectTopicName.create(projectName, topicName); * Publisher publisher = Publisher.newBuilder(topic).build(); * try { * // ... * } finally { * // When finished with the publisher, make sure to shutdown to free up resources. * publisher.shutdown(); * publisher.awaitTermination(1, TimeUnit.MINUTES); * } * }</pre> */ public static Builder newBuilder(TopicName topicName) { return newBuilder(topicName.toString()); }
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; } }
/** Example of creating a {@code Publisher}. */ // [TARGET newBuilder(ProjectTopicName)] // [VARIABLE "my_project"] // [VARIABLE "my_topic"] public static void newBuilder(String projectId, String topicId) throws Exception { ProjectTopicName topic = ProjectTopicName.of(projectId, topicId); Publisher publisher = Publisher.newBuilder(topic).build(); try { // ... } finally { // When finished with the publisher, make sure to shutdown to free up resources. publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES); } }
public MaxwellPubsubProducerWorker(MaxwellContext context, String pubsubProjectId, String pubsubTopic, String ddlPubsubTopic, ArrayBlockingQueue<RowMap> queue) throws IOException { super(context); this.projectId = pubsubProjectId; this.topic = TopicName.of(pubsubProjectId, pubsubTopic); this.pubsub = Publisher.newBuilder(this.topic).build(); if ( context.getConfig().outputConfig.outputDDL == true && ddlPubsubTopic != pubsubTopic ) { this.ddlTopic = TopicName.of(pubsubProjectId, ddlPubsubTopic); this.ddlPubsub = Publisher.newBuilder(this.ddlTopic).build(); } else { this.ddlTopic = this.topic; this.ddlPubsub = this.pubsub; } Metrics metrics = context.getMetrics(); this.queue = queue; this.taskState = new StoppableTaskState("MaxwellPubsubProducerWorker"); }
public Publisher getSingleThreadedPublisher(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_concurrency_control] // create a publisher with a single threaded executor ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build(); Publisher publisher = Publisher.newBuilder(topicName).setExecutorProvider(executorProvider).build(); // [END pubsub_publisher_concurrency_control] return publisher; }
publisher = Publisher.newBuilder(topicName).build();
publisher = Publisher.newBuilder(topicName).build();
public Publisher getPublisherWithCustomBatchSettings(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_batch_settings] // Batch settings control how the publisher batches messages long requestBytesThreshold = 5000L; // default : 1 byte long messageCountBatchSize = 10L; // default : 1 message Duration publishDelayThreshold = Duration.ofMillis(100); // default : 1 ms // Publish request get triggered based on request size, messages count & time since last publish BatchingSettings batchingSettings = BatchingSettings.newBuilder() .setElementCountThreshold(messageCountBatchSize) .setRequestByteThreshold(requestBytesThreshold) .setDelayThreshold(publishDelayThreshold) .build(); Publisher publisher = Publisher.newBuilder(topicName).setBatchingSettings(batchingSettings).build(); // [END pubsub_publisher_batch_settings] return publisher; }
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()); } }
private Builder getTestPublisherBuilder() { return Publisher.newBuilder(TEST_TOPIC) .setExecutorProvider(FixedExecutorProvider.create(fakeExecutor)) .setChannelProvider(TEST_CHANNEL_PROVIDER) .setCredentialsProvider(NoCredentialsProvider.create()); } }
@Test public void testBuilderParametersAndDefaults() { Publisher.Builder builder = Publisher.newBuilder(TEST_TOPIC); assertEquals(TEST_TOPIC.toString(), builder.topicName); assertEquals(Publisher.Builder.DEFAULT_EXECUTOR_PROVIDER, builder.executorProvider); assertEquals( Publisher.Builder.DEFAULT_REQUEST_BYTES_THRESHOLD, builder.batchingSettings.getRequestByteThreshold().longValue()); assertEquals( Publisher.Builder.DEFAULT_DELAY_THRESHOLD, builder.batchingSettings.getDelayThreshold()); assertEquals( Publisher.Builder.DEFAULT_ELEMENT_COUNT_THRESHOLD, builder.batchingSettings.getElementCountThreshold().longValue()); assertEquals(Publisher.Builder.DEFAULT_RETRY_SETTINGS, builder.retrySettings); }
Publisher.newBuilder(topicName) .setChannelProvider(channelProvider) .setCredentialsProvider(credentialsProvider)
public Publisher getPublisherWithCustomRetrySettings(ProjectTopicName topicName) throws Exception { // [START pubsub_publisher_retry_settings] // Retry settings control how the publisher handles retryable failures Duration retryDelay = Duration.ofMillis(100); // default : 1 ms double retryDelayMultiplier = 2.0; // back off for repeated failures Duration maxRetryDelay = Duration.ofSeconds(5); // default : 10 seconds Duration totalTimeout = Duration.ofSeconds(1); // default: 0 Duration initialRpcTimeout = Duration.ofSeconds(1); // default: 0 Duration maxRpcTimeout = Duration.ofSeconds(10); // default: 0 RetrySettings retrySettings = RetrySettings.newBuilder() .setInitialRetryDelay(retryDelay) .setRetryDelayMultiplier(retryDelayMultiplier) .setMaxRetryDelay(maxRetryDelay) .setTotalTimeout(totalTimeout) .setInitialRpcTimeout(initialRpcTimeout) .setMaxRpcTimeout(maxRpcTimeout) .build(); Publisher publisher = Publisher.newBuilder(topicName).setRetrySettings(retrySettings).build(); // [END pubsub_publisher_retry_settings] return publisher; }
subscriber.startAsync(); Publisher publisher = Publisher.newBuilder(topicName).build(); publisher .publish(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8("msg1")).build())
publisher = Publisher.newBuilder(topicName).build();
@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); }
@Test public void testBuilderInvalidArguments() { Publisher.Builder builder = Publisher.newBuilder(TEST_TOPIC);
try { Publisher.Builder publisherBuilder = Publisher.newBuilder(ProjectTopicName.of(this.projectId, key));
@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(); }
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; }