Spring AMQP Integration版本迁移的配置疑问及自定义容器可行性咨询
版本与依赖信息
我正在将Spring AMQP Integration从2.2.3.RELEASE迁移至5.5.20版本,当前Maven依赖树如下:
[INFO] | +- org.springframework.integration:spring-integration-stream:jar:5.5.20:compile [INFO] | +- org.springframework.integration:spring-integration-amqp:jar:5.5.20:compile [INFO] | | \- org.springframework.amqp:spring-rabbit:jar:2.3.16:compile [INFO] | | +- org.springframework.amqp:spring-amqp:jar:2.3.16:compile [INFO] | | \- com.rabbitmq:amqp-client:jar:5.9.0:compile
原配置的tx-size弃用问题
对应版本官方文档显示原XML配置的属性应仍可正常使用,原配置如下:
<int-amqp:inbound-channel-adapter id="amqpAdapter" channel="amqpChatQ" queue-names="${amqp.queue.v1}" connection-factory="connectionFactory" auto-startup="true" concurrent-consumers="${amqp.consumer.count}" channel-transacted="true" task-executor="taskExecutor" prefetch-count="2" tx-size="2" acknowledge-mode="AUTO" error-handler="messageErrorHandler" />
但实际发现tx-size="2"已被弃用,官方建议替换为batch-size,然而<int-amqp:inbound-channel-adapter>并不支持batch-size属性,希望得到解决建议。
自定义监听器容器配置可行性咨询
另外想咨询以下配置是否可行:
自定义SimpleMessageListenerContainer:
<bean id="customAmqpListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="autoStartup" value="true" /> <property name="queueNames" value="${amqp.queue.v1}" /> <property name="concurrentConsumers" value="${amqp.consumer.count}" /> <property name="channelTransacted" value="true" /> <!-- 其他待补充配置 --> </bean>
关联到入站通道适配器:
<int-amqp:inbound-channel-adapter id="amqpAdapter" channel="amqpChatQ" listener-container="customAmqpListenerContainer" />
问题解决建议
tx-size替代方案
虽然<int-amqp:inbound-channel-adapter>不直接支持batch-size,但可以通过自定义SimpleMessageListenerContainer配置该属性——batch-size是SimpleMessageListenerContainer的原生属性,在容器中设置batchSize即可替代原tx-size的功能,同时保留事务性、预取数等原有配置。自定义容器配置可行性
你提出的配置方案完全可行。通过显式定义SimpleMessageListenerContainer,可以更灵活地配置各项参数,包括原配置中的taskExecutor、errorHandler、prefetchCount等属性,只需将这些配置迁移到自定义容器中即可。完整示例配置如下:<bean id="customAmqpListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="autoStartup" value="true" /> <property name="queueNames" value="${amqp.queue.v1}" /> <property name="concurrentConsumers" value="${amqp.consumer.count}" /> <property name="channelTransacted" value="true" /> <property name="taskExecutor" ref="taskExecutor" /> <property name="prefetchCount" value="2" /> <property name="batchSize" value="2" /> <property name="acknowledgeMode" value="AUTO" /> <property name="errorHandler" ref="messageErrorHandler" /> </bean>配置完成后,入站通道适配器会复用该自定义容器的所有参数,既完全替代原XML配置的功能,又解决了
tx-size弃用的问题。
内容的提问来源于stack exchange,提问作者manoharnr fiserv

