Search code examples
springgoogle-cloud-platformgoogle-cloud-pubsub

GCP PubSub Spring Integration using subscribeAndConvert auto_ack config


so creating a subscriber using

pubSubTemplate.subscribeAndConvert( subs, { message ->
...

is very concise.

Is it possible though, to set the Ack Mode using this approach to creating subscribers?

Using channel adapters (which are less concise imo, and reason why ia am exploring subscribeAndConvert option), as described here https://cloud.google.com/pubsub/docs/spring#receiving-messages-using-channel-adapters - i can do it, e.g.

adapter.setAckMode(AckMode.MANUAL);

There is a config available with spring cloud stream for this;

 spring.cloud.stream.gcp.pubsub.default.consumer.ack-mode: AUTO_ACK

Thanks!


Solution

  • In method pubSubSubscriberTemplate.subscribeAndConvert there is no parameter to set AckMode(AckMode.AUTO).

    In Spring Integration, you can configure bind an input channel to a Pub/Sub Subscription using the PubSubInboundChannelAdapter.

    As you have mentioned using PubSubInboundChannelAdapter you can set acknowledgement mode to auto using the syntax adapter.setAckMode(AckMode.AUTO).

    Example code:

    @Bean
    public MessageChannel orderRequestInputChannel() {
      return MessageChannels.direct().get();
    }
    
    @Bean
    public PubSubInboundChannelAdapter orderRequestChannelAdapter(
        @Qualifier("orderRequestInputChannel") MessageChannel inputChannel,
        PubSubTemplate pubSubTemplate) {
      PubSubInboundChannelAdapter adapter =
          new PubSubInboundChannelAdapter(
              pubSubTemplate, "orders-subscription");
      adapter.setOutputChannel(inputChannel);
      adapter.setPayloadType(Order.class);
      adapter.setAckMode(AckMode.AUTO);
    
      return adapter;
    }
    

    You can set the ACK mode of the consumer endpoint in application.properties as:

    spring.cloud.stream.gcp.pubsub.bindings.{CONSUMER_NAME}.consumer.ack-mode=AUTO_ACK