public Builder setBatchingSettings(BatchingSettings batchingSettings) { Preconditions.checkNotNull(batchingSettings); Preconditions.checkNotNull(batchingSettings.getElementCountThreshold()); Preconditions.checkArgument(batchingSettings.getElementCountThreshold() > 0); Preconditions.checkNotNull(batchingSettings.getRequestByteThreshold()); Preconditions.checkArgument(batchingSettings.getRequestByteThreshold() > 0); Preconditions.checkNotNull(batchingSettings.getDelayThreshold()); Preconditions.checkArgument(batchingSettings.getDelayThreshold().toMillis() > 0); this.batchingSettings = batchingSettings; return this; }
if (messagesBatch.size() == getBatchingSettings().getElementCountThreshold()) { batchToSend = new OutstandingBatch(messagesBatch, batchedBytes); messagesBatch = new LinkedList<>();
@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); }
@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); }
/** Build the BatchingSettings object. */ public BatchingSettings build() { BatchingSettings settings = autoBuild(); Preconditions.checkArgument( settings.getElementCountThreshold() == null || settings.getElementCountThreshold() > 0, "elementCountThreshold must be either unset or positive"); Preconditions.checkArgument( settings.getRequestByteThreshold() == null || settings.getRequestByteThreshold() > 0, "requestByteThreshold must be either unset or positive"); Preconditions.checkArgument( settings.getDelayThreshold() == null || settings.getDelayThreshold().compareTo(Duration.ZERO) > 0, "delayThreshold must be either unset or positive"); return settings; } }
/** Build the BatchingSettings object. */ public BatchingSettings build() { BatchingSettings settings = autoBuild(); Preconditions.checkArgument( settings.getElementCountThreshold() == null || settings.getElementCountThreshold() > 0, "elementCountThreshold must be either unset or positive"); Preconditions.checkArgument( settings.getRequestByteThreshold() == null || settings.getRequestByteThreshold() > 0, "requestByteThreshold must be either unset or positive"); Preconditions.checkArgument( settings.getDelayThreshold() == null || settings.getDelayThreshold().compareTo(Duration.ZERO) > 0, "delayThreshold must be either unset or positive"); return settings; } }
private ImmutableList<BatchingThreshold<Batch<RequestT, ResponseT>>> getThresholds( BatchingSettings batchingSettings) { ImmutableList.Builder<BatchingThreshold<Batch<RequestT, ResponseT>>> listBuilder = ImmutableList.builder(); if (batchingSettings.getElementCountThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> elementCounter = new BatchElementCounter<>(batchingDescriptor); BatchingThreshold<Batch<RequestT, ResponseT>> countThreshold = new NumericThreshold<>(batchingSettings.getElementCountThreshold(), elementCounter); listBuilder.add(countThreshold); } if (batchingSettings.getRequestByteThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> requestByteCounter = new BatchByteCounter<RequestT, ResponseT>(); BatchingThreshold<Batch<RequestT, ResponseT>> byteThreshold = new NumericThreshold<>(batchingSettings.getRequestByteThreshold(), requestByteCounter); listBuilder.add(byteThreshold); } return listBuilder.build(); } }
private ImmutableList<BatchingThreshold<Batch<RequestT, ResponseT>>> getThresholds( BatchingSettings batchingSettings) { ImmutableList.Builder<BatchingThreshold<Batch<RequestT, ResponseT>>> listBuilder = ImmutableList.builder(); if (batchingSettings.getElementCountThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> elementCounter = new BatchElementCounter<>(batchingDescriptor); BatchingThreshold<Batch<RequestT, ResponseT>> countThreshold = new NumericThreshold<>(batchingSettings.getElementCountThreshold(), elementCounter); listBuilder.add(countThreshold); } if (batchingSettings.getRequestByteThreshold() != null) { ElementCounter<Batch<RequestT, ResponseT>> requestByteCounter = new BatchByteCounter<RequestT, ResponseT>(); BatchingThreshold<Batch<RequestT, ResponseT>> byteThreshold = new NumericThreshold<>(batchingSettings.getRequestByteThreshold(), requestByteCounter); listBuilder.add(byteThreshold); } return listBuilder.build(); } }