0

I am new to GCP pub/sub and trying to resend a message which is not acknowledged (ack/nack). In subscription at GCP console dashboard, I have mentioned: enter image description here

In my java code, I have created a subscriber

public Subscriber createSubscriber(String subscriptionId, MessageReceiver receiver) throws MessagingException {
        Subscriber subscriber = null;
        ProjectSubscriptionName subscriptionName = null;
        String projectId = getProjectId();
        if (Objects.isNull(projectId) || Objects.isNull(subscriptionId)) {
            throw new MessagingException(MessagingErrorCodes.MIX90810
                    + " Project Id/Subscription Id is null for subscriptionId = " + subscriptionId + " projectId= "
                    + projectId, MessagingErrorCodes.MIX90810);
        }
        try {
            subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
            subscriber = Subscriber.newBuilder(subscriptionName, receiver).setExecutorProvider(getExecutorProvider()).build();
        } catch (Exception e) {
            throw new MessagingException(MessagingErrorCodes.MAX34540
                            + " Error occurred while creating the subscriber for the subscriptionId = " + subscriptionId
                            + "projectId " + projectId + "subscriptionName= " + subscriptionName,
                    MessagingErrorCodes.MAX34540, e);
        }
    enter code here
        return subscriber;
    }

I am getting messages on my receiveMessage(PubsubMessage message, AckReplyConsumer consumer) the first time but not getting again if I am not acknowledging the message. But if sending nack it's sending the message again.

@Service
public class MyMessageReceiver implements MessageReceiver {

    @Override
    public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
        System.out.println(message.getMessageId());
    }
}

should I need to mention other configuration to enable retry in case of not acknowledging the message as well?

Rishabh Jain
  • 113
  • 1
  • 14

1 Answers1

0

Regarding the retry policy, the documentation says that Pub/Sub tries to redeliver the message, only if the acknowledgement deadline expires or if the subscriber nacks the message. Once the acknowledgement deadline passes, the message becomes a candidate to be redelivered. The redelivery may not be immediate as redelivery is performed on a best effort basis.

As already mentioned in the comments, the DEFAULT_MAX_ACK_EXTENSION_PERIOD is set to 60 minutes in Subscriber.java, which is the cause of this delay. The Ack deadline will continue to be extended (by the client library as a native functionality) until this duration is reached. Meaning, the unacked message is leased by the subscriber for 60 minutes and is not eligible to be redelivered within this period. The setMaxAckExtensionPeriod(Duration maxAckExtensionPeriod) is used to set a custom value to the maximum period a message’s acknowledgement deadline will be extended to.

Please also note that none of these values is a guarantee that the message will not be redelivered within that time frame. It is possible for the message to be redelivered before maxAckExtensionPeriod due to network or server blips.

Krish
  • 752
  • 3
  • 10
  • After setting setMaxAckExtensionPeriod(Duration.ofSeconds(20)). I am getting messages early but not in an interval of 20 seconds. Sometimes 1 min or even later. – Rishabh Jain Sep 16 '21 at 05:39
  • Hi @RishabhJain, I updated the answer as per your comment. – Krish Sep 27 '21 at 04:40