GH-3966: Kafka XML config: Expose more attributes

Fixes https://github.com/spring-projects/spring-integration/issues/3966

Exposed setters in the `KafkaInboundGateway` and `KafkaMessageDrivenChannelAdapter`
as an XML attributes for `kafka` namespace

**Cherry-pick to `5.5.x`**
This commit is contained in:
abilan
2022-12-12 15:03:45 -05:00
committed by Gary Russell
parent 280eb29f62
commit e63edde7cb
7 changed files with 128 additions and 20 deletions

View File

@@ -19,7 +19,9 @@
payload-type="java.lang.String"
error-message-strategy="ems"
retry-template="retryTemplate"
recovery-callback="recoveryCallback"/>
recovery-callback="recoveryCallback"
bind-source-record="true"
on-partitions-assigned-seek-callback="onPartitionsAssignedSeekCallback"/>
<bean id="template" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.kafka.core.KafkaTemplate"/>
@@ -44,6 +46,10 @@
</constructor-arg>
</bean>
<bean id="onPartitionsAssignedSeekCallback" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="java.util.function.BiConsumer"/>
</bean>
<bean id="ems" class="org.springframework.integration.kafka.support.RawRecordHeaderErrorMessageStrategy"/>
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate"/>

View File

@@ -30,6 +30,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.4
*
@@ -58,13 +59,16 @@ public class KafkaInboundGatewayTests {
assertThat(TestUtils.getPropertyValue(this.gateway1, "listener.fallbackType"))
.isEqualTo(String.class);
assertThat(TestUtils.getPropertyValue(this.gateway1, "errorMessageStrategy"))
.isSameAs(this.context.getBean("ems"));
.isSameAs(this.context.getBean("ems"));
assertThat(TestUtils.getPropertyValue(this.gateway1, "retryTemplate"))
.isSameAs(this.context.getBean("retryTemplate"));
.isSameAs(this.context.getBean("retryTemplate"));
assertThat(TestUtils.getPropertyValue(this.gateway1, "recoveryCallback"))
.isSameAs(this.context.getBean("recoveryCallback"));
.isSameAs(this.context.getBean("recoveryCallback"));
assertThat(TestUtils.getPropertyValue(this.gateway1, "onPartitionsAssignedSeekCallback"))
.isSameAs(this.context.getBean("onPartitionsAssignedSeekCallback"));
assertThat(TestUtils.getPropertyValue(this.gateway1, "messagingTemplate.sendTimeout")).isEqualTo(5000L);
assertThat(TestUtils.getPropertyValue(this.gateway1, "replyTimeout")).isEqualTo(43L);
assertThat(TestUtils.getPropertyValue(this.gateway1, "bindSourceRecord", Boolean.class)).isTrue();
}
}

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration/kafka https://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd">
@@ -20,7 +20,12 @@
payload-type="java.lang.String"
error-message-strategy="ems"
retry-template="retryTemplate"
recovery-callback="recoveryCallback" />
recovery-callback="recoveryCallback"
bind-source-record="true"
on-partitions-assigned-seek-callback="onPartitionsAssignedSeekCallback"
filter-in-retry="true"
ack-discarded="true"
record-filter-strategy="recordFilterStrategy"/>
<int-kafka:message-driven-channel-adapter
id="kafkaBatchListener"
@@ -31,7 +36,7 @@
channel="nullChannel"
mode="batch"
message-converter="messageConverter"
error-channel="errorChannel" />
error-channel="errorChannel"/>
<bean id="messageConverter" class="org.springframework.kafka.support.converter.MessagingMessageConverter"/>
@@ -40,39 +45,47 @@
<bean class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
<constructor-arg>
<map>
<entry key="" value="" />
<entry key="" value=""/>
</map>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.kafka.listener.ContainerProperties">
<constructor-arg name="topics" value="foo" />
<constructor-arg name="topics" value="foo"/>
</bean>
</constructor-arg>
</bean>
<bean id="onPartitionsAssignedSeekCallback" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="java.util.function.BiConsumer"/>
</bean>
<bean id="recordFilterStrategy" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.kafka.listener.adapter.RecordFilterStrategy"/>
</bean>
<bean id="container2" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
<constructor-arg>
<bean class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
<constructor-arg>
<map>
<entry key="" value="" />
<entry key="" value=""/>
</map>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.kafka.listener.ContainerProperties">
<constructor-arg name="topics" value="foo" />
<constructor-arg name="topics" value="foo"/>
</bean>
</constructor-arg>
</bean>
<bean id="ems" class="org.springframework.integration.kafka.support.RawRecordHeaderErrorMessageStrategy" />
<bean id="ems" class="org.springframework.integration.kafka.support.RawRecordHeaderErrorMessageStrategy"/>
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate" />
<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate"/>
<bean id="recoveryCallback" class="org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer" />
<bean id="recoveryCallback" class="org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer"/>
</beans>

View File

@@ -24,6 +24,7 @@ import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.channel.QueueChannel;
@@ -43,7 +44,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
/**
* @author Artem Bilan.
* @author Artem Bilan
* @author Gary Russell
*
* @since 5.4
@@ -52,6 +53,9 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@DirtiesContext
class KafkaMessageDrivenChannelAdapterParserTests {
@Autowired
private ApplicationContext context;
@Autowired
private NullChannel nullChannel;
@@ -92,6 +96,13 @@ class KafkaMessageDrivenChannelAdapterParserTests {
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "errorMessageStrategy")).isSameAs(this.ems);
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "retryTemplate")).isSameAs(this.retryTemplate);
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "recoveryCallback")).isSameAs(this.recoveryCallback);
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "onPartitionsAssignedSeekCallback"))
.isSameAs(this.context.getBean("onPartitionsAssignedSeekCallback"));
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "bindSourceRecord", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "filterInRetry", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "ackDiscarded", Boolean.class)).isTrue();
assertThat(TestUtils.getPropertyValue(this.kafkaListener, "recordFilterStrategy"))
.isSameAs(this.context.getBean("recordFilterStrategy"));
}
@Test