Spring integration 使用Spring消费来自pubsub的消息批
如何使用来自pubsub的多条消息?这似乎是一个简单的问题,应该有简单的解决方案,但目前我可以通过Spring integration 使用Spring消费来自pubsub的消息批,spring-integration,spring-cloud,google-cloud-pubsub,Spring Integration,Spring Cloud,Google Cloud Pubsub,如何使用来自pubsub的多条消息?这似乎是一个简单的问题,应该有简单的解决方案,但目前我可以通过spring-cloud-gcp-pubsub找到从pubsub消费批量记录的简单方法 我正在使用springcloudgcpubsub来使用来自pubsub的消息,并在springboot应用程序中处理它们。我当前的设置非常简单,我有使用记录的PubSubInboundChannelAdapter和ServiceActivator。经过研究,我发现了spring集成聚合器,但它们似乎不是一种很好的
spring-cloud-gcp-pubsub
找到从pubsub消费批量记录的简单方法
我正在使用
springcloudgcpubsub
来使用来自pubsub的消息,并在springboot应用程序中处理它们。我当前的设置非常简单,我有使用记录的PubSubInboundChannelAdapter
和ServiceActivator
。经过研究,我发现了spring集成聚合器
,但它们似乎不是一种很好的方法,因为它不容易向下游传播确认。我有什么遗漏吗?如何使用成批邮件?PubSubInboundChannelAdapter基于对主题的订阅。因此,它将是一个消息流,这个PubSubInboundChannelAdapter
会对每个消息做出反应,将其转换为Spring消息并将其发送到配置的通道下游。
在订阅期间,确实无法获取一批消息
您还需要记住,GCP发布/订阅中没有类似于offset
的内容。您确实应该确认您从发布/订阅中使用的每一条消息
虽然可以使用PubSubMessageSource
一次提取一批消息。messageSource.setMaxFetchSize(5)
可以做到这一点,但是这个PubSubMessageSource
仍然会单独生成每条消息,因此您可以(n)独立地确认它们
当然,您可以利用功能PubSubMessageSource
uses-PubSubSubscriberOperations.pullAndConvert()
。有关更多信息,请参阅JavaDocs:
/**
* Pull a number of messages from a Google Cloud Pub/Sub subscription and convert them to Spring messages with
* the desired payload type.
* @param subscription the subscription name
* @param maxMessages the maximum number of pulled messages
* @param returnImmediately returns immediately even if subscription doesn't contain enough
* messages to satisfy {@code maxMessages}
* @param payloadType the type to which the payload of the Pub/Sub messages should be converted
* @param <T> the type of the payload
* @return the list of received acknowledgeable messages
* @since 1.1
*/
<T> List<ConvertedAcknowledgeablePubsubMessage<T>> pullAndConvert(String subscription, Integer maxMessages,
Boolean returnImmediately, Class<T> payloadType);
/**
*从Google云发布/订阅中提取大量消息,并使用
*所需的有效负载类型。
*@param subscription订阅名称
*@param maxMessages拉取消息的最大数量
*@param return即使订阅内容不足,也会立即返回
*满足{@code maxMessages}的消息
*@param payloadType发布/订阅消息的有效负载应转换为的类型
*@param有效载荷的类型
*@返回接收到的可确认消息列表
*@自1.1
*/
列表pullAndConvert(字符串订阅、整数maxMessages、,
布尔值(立即返回,类payloadType);
因此,这一个看起来像您正在寻找的,因为您确实将有一个消息列表,并且每个消息都是一个带有(n)个ack回调的包装器
此API可用于自定义@InboundChannelAdapter
MessageSource
或供应商@Bean
实现
但仍然:我看不到整个批处理的好处,因为每个消息都可以单独确认,而不会影响所有其他消息。尝试使用以下方法:
@Bean
@InboundChannelAdapter(channel = "pubsubInputChannel", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "3"))
public MessageSource<Object> pubsubAdapter(PubSubTemplate pubSubTemplate) {
PubSubMessageSource messageSource = new PubSubMessageSource(pubSubTemplate, "testSubscription");
messageSource.setAckMode(AckMode.MANUAL);
return messageSource;
}
@Bean
@InboundChannelAdapter(channel=“pubsubInputChannel”,poller=@poller(fixedDelay=“5000”,maxMessagesPerPoll=“3”))
public MessageSource pubsubAdapter(PubSubTemplate PubSubTemplate){
PubSubMessageSource messageSource=新的PubSubMessageSource(pubSubTemplate,“testSubscription”);
messageSource.setAckMode(AckMode.MANUAL);
返回消息源;
}
maxMessagesPerPoll
属性确定将轮询多少条消息