Skip to content

Commit

Permalink
Add support for configuring Pulsar listener container concurrency
Browse files Browse the repository at this point in the history
This commit adds configuration property that allows users to configure
Pulsar message listener container concurrency.
  • Loading branch information
vpavic committed Aug 30, 2024
1 parent ad730a6 commit 01e745e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @author Alexander Preuß
* @author Phillip Webb
* @author Jonas Geiregat
* @author Vedran Pavic
* @since 3.2.0
*/
@AutoConfiguration
Expand Down Expand Up @@ -187,7 +188,10 @@ ConcurrentPulsarListenerContainerFactory<?> pulsarListenerContainerFactory(
}
pulsarTransactionManager.ifUnique(containerProperties.transactions()::setTransactionManager);
this.propertiesMapper.customizeContainerProperties(containerProperties);
return new ConcurrentPulsarListenerContainerFactory<>(pulsarConsumerFactory, containerProperties);
ConcurrentPulsarListenerContainerFactory<Object> listenerContainerFactory = new ConcurrentPulsarListenerContainerFactory<>(
pulsarConsumerFactory, containerProperties);
this.propertiesMapper.customizeConcurrentPulsarListenerContainerFactory(listenerContainerFactory);
return listenerContainerFactory;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* @author Chris Bono
* @author Phillip Webb
* @author Swamy Mavuri
* @author Vedran Pavic
* @since 3.2.0
*/
@ConfigurationProperties("spring.pulsar")
Expand Down Expand Up @@ -801,6 +802,11 @@ public static class Listener {
*/
private SchemaType schemaType;

/**
* Number of threads used by listener container.
*/
private Integer concurrency;

/**
* Whether to record observations for when the Observations API is available and
* the client supports it.
Expand All @@ -815,6 +821,14 @@ public void setSchemaType(SchemaType schemaType) {
this.schemaType = schemaType;
}

public Integer getConcurrency() {
return this.concurrency;
}

public void setConcurrency(Integer concurrency) {
this.concurrency = concurrency;
}

public boolean isObservationEnabled() {
return this.observationEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.json.JsonWriter;
import org.springframework.pulsar.config.ConcurrentPulsarListenerContainerFactory;
import org.springframework.pulsar.core.PulsarTemplate;
import org.springframework.pulsar.listener.PulsarContainerProperties;
import org.springframework.pulsar.reader.PulsarReaderContainerProperties;
Expand All @@ -50,6 +51,7 @@
* @author Chris Bono
* @author Phillip Webb
* @author Swamy Mavuri
* @author Vedran Pavic
*/
final class PulsarPropertiesMapper {

Expand Down Expand Up @@ -195,6 +197,13 @@ private void customizePulsarContainerListenerProperties(PulsarContainerPropertie
map.from(properties::isObservationEnabled).to(containerProperties::setObservationEnabled);
}

<T> void customizeConcurrentPulsarListenerContainerFactory(
ConcurrentPulsarListenerContainerFactory<T> listenerContainerFactory) {
PulsarProperties.Listener properties = this.properties.getListener();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(properties::getConcurrency).to(listenerContainerFactory::setConcurrency);
}

<T> void customizeReaderBuilder(ReaderBuilder<T> readerBuilder) {
PulsarProperties.Reader properties = this.properties.getReader();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import org.springframework.boot.autoconfigure.pulsar.PulsarProperties.Consumer;
import org.springframework.boot.autoconfigure.pulsar.PulsarProperties.Failover.BackupCluster;
import org.springframework.pulsar.config.ConcurrentPulsarListenerContainerFactory;
import org.springframework.pulsar.core.PulsarProducerFactory;
import org.springframework.pulsar.core.PulsarTemplate;
import org.springframework.pulsar.listener.PulsarContainerProperties;
Expand All @@ -59,6 +60,7 @@
* @author Chris Bono
* @author Phillip Webb
* @author Swamy Mavuri
* @author Vedran Pavic
*/
class PulsarPropertiesMapperTests {

Expand Down Expand Up @@ -267,6 +269,17 @@ void customizeContainerProperties() {
assertThat(containerProperties.transactions().isEnabled()).isTrue();
}

@Test
void customizeConcurrentPulsarListenerContainerFactory() {
PulsarProperties properties = new PulsarProperties();
properties.getListener().setConcurrency(10);
ConcurrentPulsarListenerContainerFactory<?> listenerContainerFactory = mock(
ConcurrentPulsarListenerContainerFactory.class);
new PulsarPropertiesMapper(properties)
.customizeConcurrentPulsarListenerContainerFactory(listenerContainerFactory);
then(listenerContainerFactory).should().setConcurrency(10);
}

@Test
@SuppressWarnings("unchecked")
void customizeReaderBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Soby Chacko
* @author Phillip Webb
* @author Swamy Mavuri
* @author Vedran Pavic
*/
class PulsarPropertiesTests {

Expand Down Expand Up @@ -382,9 +383,11 @@ class ListenerProperties {
void bind() {
Map<String, String> map = new HashMap<>();
map.put("spring.pulsar.listener.schema-type", "avro");
map.put("spring.pulsar.listener.concurrency", "10");
map.put("spring.pulsar.listener.observation-enabled", "true");
PulsarProperties.Listener properties = bindProperties(map).getListener();
assertThat(properties.getSchemaType()).isEqualTo(SchemaType.AVRO);
assertThat(properties.getConcurrency()).isEqualTo(10);
assertThat(properties.isObservationEnabled()).isTrue();
}

Expand Down

0 comments on commit 01e745e

Please sign in to comment.