Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce PollerMetadataCustomizer for customizing auto-configured Spring Integration PollerMetadata #44637

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.rsocket.transport.netty.server.TcpServerTransport;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
Expand Down Expand Up @@ -85,6 +86,7 @@
* @author Vedran Pavic
* @author Madhura Bhave
* @author Yong-Hyun Kim
* @author Yanming Zhou
* @since 1.1.0
*/
@AutoConfiguration(after = { DataSourceAutoConfiguration.class, JmxAutoConfiguration.class,
Expand Down Expand Up @@ -130,7 +132,8 @@ protected static class IntegrationConfiguration {

@Bean(PollerMetadata.DEFAULT_POLLER)
@ConditionalOnMissingBean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPollerMetadata(IntegrationProperties integrationProperties) {
public PollerMetadata defaultPollerMetadata(IntegrationProperties integrationProperties,
ObjectProvider<PollerMetadataCustomizer> customizers) {
IntegrationProperties.Poller poller = integrationProperties.getPoller();
MutuallyExclusiveConfigurationPropertiesException.throwIfMultipleNonNullValuesIn((entries) -> {
entries.put("spring.integration.poller.cron",
Expand All @@ -143,6 +146,7 @@ public PollerMetadata defaultPollerMetadata(IntegrationProperties integrationPro
map.from(poller::getMaxMessagesPerPoll).to(pollerMetadata::setMaxMessagesPerPoll);
map.from(poller::getReceiveTimeout).as(Duration::toMillis).to(pollerMetadata::setReceiveTimeout);
map.from(poller).as(this::asTrigger).to(pollerMetadata::setTrigger);
customizers.orderedStream().forEach((customizer) -> customizer.customize(pollerMetadata));
return pollerMetadata;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.integration;

import org.springframework.integration.scheduling.PollerMetadata;

/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link PollerMetadata} whilst retaining default auto-configuration.
*
* @author Yanming Zhou
* @since 3.5.0
*/
@FunctionalInterface
public interface PollerMetadataCustomizer {

/**
* Customize the {@link PollerMetadata}.
* @param pollerMetadata the {@code PollerMetadata} to customize
*/
void customize(PollerMetadata pollerMetadata);

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
Expand Down Expand Up @@ -105,6 +107,7 @@
* @author Stephane Nicoll
* @author Vedran Pavic
* @author Yong-Hyun Kim
* @author Yanming Zhou
*/
class IntegrationAutoConfigurationTests {

Expand Down Expand Up @@ -543,6 +546,19 @@ void integrationVirtualThreadsEnabled() {
.usesVirtualThreads()));
}

@Test
void pollerMetadataCanBeCustomizedViaPollerMetadataCustomizer() {
TaskExecutor taskExecutor = new SyncTaskExecutor();
this.contextRunner.withUserConfiguration(PollingConsumerConfiguration.class)
.withBean(PollerMetadataCustomizer.class,
() -> (pollerMetadata) -> pollerMetadata.setTaskExecutor(taskExecutor))
.run((context) -> {
assertThat(context).hasSingleBean(PollerMetadata.class);
PollerMetadata metadata = context.getBean(PollerMetadata.DEFAULT_POLLER, PollerMetadata.class);
assertThat(metadata.getTaskExecutor()).isSameAs(taskExecutor);
});
}

@Configuration(proxyBeanMethods = false)
static class CustomMBeanExporter {

Expand Down