1

How i can set Retry policy where pushing record to cosmosDB using java.

I want to retry pushing record if its failed to push record for first time. I want to retry pushing 5 time after an interval of 2 seconds.

how i can make such changes in java i read about ConnectionPolicy in java but not able to understand how it can full fill my requirements.

reference link: https://learn.microsoft.com/bs-latn-ba/azure/cosmos-db/performance-tips-java

Code:

retryOptions = new RetryOptions();
retryOptions.setMaxRetryAttemptsOnThrottledRequests(5);
retryOptions.setMaxRetryWaitTimeInSeconds(10);
connectionPolicy = new ConnectionPolicy();
connectionPolicy.setRetryOptions(retryOptions);
documentClient = new DocumentClient(END_POINT,
                MASTER_KEY, connectionPolicy,
                ConsistencyLevel.Session);
Mohit Singh
  • 401
  • 1
  • 10
  • 30

1 Answers1

0

You can use ConnectionPolicy.setRetryOptions(RetryOptions retryOptions) method and create an instance of RetryOption with setMaxRetryAttemptsOnThrottledRequests as 5 and setMaxRetryWaitTimeInSeconds as 10 (2 seconds x 5 retry attempts).

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • can you plz review the above code in question one more dough setMaxRetryWaitTimeInSeconds in this we are setting 10 how it come to know that it has to wait for 2 second and then retry again? – Mohit Singh Mar 19 '20 at 09:46
  • I am not able to find the source code for `RetryOptions` but I did find the code for `ResourceThrottleRetryPolicy` here: https://github.com/Azure/azure-cosmosdb-java/blob/81a56e9506827e647253b1b6ae39f95a7aee37a3/gateway/src/main/java/com/microsoft/azure/cosmosdb/rx/internal/ResourceThrottleRetryPolicy.java. This should give you an idea about how the retry is implemented in SDK. – Gaurav Mantri Mar 19 '20 at 09:55
  • ok not getting exactly what they are doing but if i set setMaxRetryWaitTimeInSeconds as 10 the retry will occur after every 2 second? if after 5 try its not able to push record doest it return any error code? documentClient.createDocument( collectionLink, document, null, false) like in this i am creating Document it try to push record and perform all retry will it return any code so we can compare that record is sent sucessfull? something like 200 status code – Mohit Singh Mar 19 '20 at 10:17
  • So not all error codes are retryable. In this case only when you get a throttling error (429 status code), the operation will be retried. Also to note is the presence of header in response which tells after how much time the operation should be retried (x-ms-retry-after-ms). – Gaurav Mantri Mar 19 '20 at 10:25
  • Also, you can download the source code for the SDK from here: https://github.com/Azure/azure-documentdb-java/releases (1.8.0) and understand exactly how it is implemented. – Gaurav Mantri Mar 19 '20 at 11:06