Skip to content

Commit c50b416

Browse files
committed
feat: add scheduled executor pool size configuration for SQS batching operations
1 parent 12ad251 commit c50b416

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

docs/src/main/asciidoc/sqs.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ The Spring Boot Starter for SQS provides the following auto-configuration proper
10081008
| `spring.cloud.aws.sqs.batch.wait-time-seconds` | Wait time for receiveMessage requests in batch. | No | AWS SDK default
10091009
| `spring.cloud.aws.sqs.batch.system-attribute-names` | System attributes to request for receiveMessage calls. | No | None
10101010
| `spring.cloud.aws.sqs.batch.attribute-names` | Message attributes to request for receiveMessage calls. | No | None
1011+
| `spring.cloud.aws.sqs.batch.scheduled-executor-pool-size` | Size of the scheduled thread pool for batching operations. | No | 5
10111012
|===
10121013

10131014

spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/sqs/SqsAutoConfiguration.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.awspring.cloud.sqs.support.observation.SqsTemplateObservation;
3636
import io.micrometer.observation.ObservationRegistry;
3737
import java.util.concurrent.Executors;
38+
import java.util.concurrent.ScheduledExecutorService;
3839
import org.springframework.beans.factory.ObjectProvider;
3940
import org.springframework.boot.autoconfigure.AutoConfiguration;
4041
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -60,6 +61,7 @@
6061
* @author Maciej Walkowiak
6162
* @author Wei Jiang
6263
* @author Dongha Kim
64+
* @author khc41
6365
* @since 3.0
6466
*/
6567
@AutoConfiguration
@@ -91,14 +93,27 @@ public SqsAsyncClient sqsAsyncClient(AwsClientBuilderConfigurer awsClientBuilder
9193
@ConditionalOnProperty(name = "spring.cloud.aws.sqs.batch.enabled", havingValue = "true")
9294
@Bean
9395
@Primary
94-
public SqsAsyncClient batchSqsAsyncClient(SqsAsyncClient sqsAsyncClient) {
95-
SqsAsyncBatchManager batchManager = createBatchManager(sqsAsyncClient);
96+
public SqsAsyncClient batchSqsAsyncClient(SqsAsyncClient sqsAsyncClient,
97+
ScheduledExecutorService sqsBatchingScheduledExecutor) {
98+
SqsAsyncBatchManager batchManager = createBatchManager(sqsAsyncClient, sqsBatchingScheduledExecutor);
9699
return new BatchingSqsClientAdapter(batchManager);
97100
}
98101

99-
private SqsAsyncBatchManager createBatchManager(SqsAsyncClient sqsAsyncClient) {
100-
return SqsAsyncBatchManager.builder().client(sqsAsyncClient)
101-
.scheduledExecutor(Executors.newScheduledThreadPool(5))
102+
@ConditionalOnProperty(name = "spring.cloud.aws.sqs.batch.enabled", havingValue = "true")
103+
@ConditionalOnMissingBean(name = "sqsBatchingScheduledExecutor")
104+
@Bean
105+
public ScheduledExecutorService sqsBatchingScheduledExecutor() {
106+
int poolSize = this.sqsProperties.getBatch().getScheduledExecutorPoolSize();
107+
if (poolSize <= 0) {
108+
throw new IllegalArgumentException(
109+
"scheduledExecutorPoolSize must be greater than 0, but was: " + poolSize);
110+
}
111+
return Executors.newScheduledThreadPool(poolSize);
112+
}
113+
114+
private SqsAsyncBatchManager createBatchManager(SqsAsyncClient sqsAsyncClient,
115+
ScheduledExecutorService scheduledExecutor) {
116+
return SqsAsyncBatchManager.builder().client(sqsAsyncClient).scheduledExecutor(scheduledExecutor)
102117
.overrideConfiguration(this::configurationProperties).build();
103118
}
104119

spring-cloud-aws-autoconfigure/src/main/java/io/awspring/cloud/autoconfigure/sqs/SqsProperties.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ public static class Batch {
235235
@Nullable
236236
private List<String> attributeNames;
237237

238+
/**
239+
* The size of the scheduled thread pool used for batching operations. This thread pool handles periodic batch
240+
* sending and other scheduled tasks.
241+
*
242+
* <p>
243+
* Default is {@code 5}.
244+
*/
245+
private int scheduledExecutorPoolSize = 5;
246+
238247
public boolean isEnabled() {
239248
return enabled;
240249
}
@@ -297,6 +306,14 @@ public void setAttributeNames(List<String> attributeNames) {
297306
this.attributeNames = attributeNames;
298307
}
299308

309+
public int getScheduledExecutorPoolSize() {
310+
return scheduledExecutorPoolSize;
311+
}
312+
313+
public void setScheduledExecutorPoolSize(int scheduledExecutorPoolSize) {
314+
this.scheduledExecutorPoolSize = scheduledExecutorPoolSize;
315+
}
316+
300317
}
301318

302319
}

spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/sqs/SqsAutoConfigurationTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ void sqsBatchConfigurationPropertiesWithDefaults() {
408408
assertThat(batchConfig.getWaitTimeSeconds()).isNull();
409409
assertThat(batchConfig.getSystemAttributeNames()).isNull();
410410
assertThat(batchConfig.getAttributeNames()).isNull();
411+
assertThat(batchConfig.getScheduledExecutorPoolSize()).isEqualTo(5);
411412

412413
assertThat(context).hasSingleBean(SqsAsyncClient.class);
413414
SqsAsyncClient client = context.getBean(SqsAsyncClient.class);
@@ -457,6 +458,42 @@ void sqsBatchConfigurationWithAttributeNames() {
457458
});
458459
}
459460

461+
@Test
462+
void sqsBatchConfigurationWithDefaultScheduledExecutorPoolSize() {
463+
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.batch.enabled:true").run(context -> {
464+
assertThat(context).hasSingleBean(SqsProperties.class);
465+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
466+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
467+
468+
assertThat(batchConfig.isEnabled()).isTrue();
469+
assertThat(batchConfig.getScheduledExecutorPoolSize()).isEqualTo(5);
470+
471+
assertThat(context).hasBean("sqsBatchingScheduledExecutor");
472+
});
473+
}
474+
475+
@Test
476+
void sqsBatchConfigurationWithCustomScheduledExecutorPoolSize() {
477+
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.batch.enabled:true",
478+
"spring.cloud.aws.sqs.batch.scheduled-executor-pool-size:10").run(context -> {
479+
assertThat(context).hasSingleBean(SqsProperties.class);
480+
SqsProperties sqsProperties = context.getBean(SqsProperties.class);
481+
SqsProperties.Batch batchConfig = sqsProperties.getBatch();
482+
483+
assertThat(batchConfig.isEnabled()).isTrue();
484+
assertThat(batchConfig.getScheduledExecutorPoolSize()).isEqualTo(10);
485+
486+
assertThat(context).hasBean("sqsBatchingScheduledExecutor");
487+
});
488+
}
489+
490+
@Test
491+
void sqsBatchConfigurationWithBatchDisabledDoesNotCreateScheduledExecutor() {
492+
this.contextRunner.withPropertyValues("spring.cloud.aws.sqs.batch.enabled:false").run(context -> {
493+
assertThat(context).doesNotHaveBean("sqsBatchingScheduledExecutor");
494+
});
495+
}
496+
460497
@Configuration(proxyBeanMethods = false)
461498
static class CustomComponentsConfiguration {
462499

0 commit comments

Comments
 (0)