Quantcast
Channel: Active questions tagged retry-logic - Stack Overflow
Viewing all articles
Browse latest Browse all 950

How do I use com.microsoft.graph.httpcore.middlewareoption.IShouldRetry with BaseCollectionRequest?

$
0
0

I am using Java with Microsoft's GraphServiceClient. What I am trying to do is to first create a B2C User and save that User on Azure B2C. Once that User is successfully saved, I get a response back with the User's information, including the newly generated id. I get that id, and then I make another request to Azure B2C to set up the email authentication method, EmailAuthenticationMethodCollectionRequest.

I am using version 5 of the Graph API

implementation 'com.microsoft.graph:microsoft-graph:5.48.0'

Here is the code after I got the User's id and try to make another call.

final var emailAuthenticationMethod = new EmailAuthenticationMethod();emailAuthenticationMethod.emailAddress = emailAddress;final EmailAuthenticationMethodCollectionRequest buildRequest = graphServiceClient.users(id).authentication().emailMethods().buildRequest();buildRequest.setShouldRetry(new GraphRetryLogic());buildRequest.setMaxRetries(3);buildRequest.setDelay(500);buildRequest.post(emailAuthenticationMethod);

The reason I do this is because these calls happen very fast, and back to back, so there are times when the User gets created, another call to update the User fails as it couldn't be found, yet. But if I wait for an additional second, and try again, I am generally able to find that User.

Here is how my GraphRetryLogic() looks like:

@Log4j2public class GraphRetryLogic implements IShouldRetry {    @Override    public boolean shouldRetry(long delayInMilliseconds, int i, @NotNull Request request, @NotNull Response response) {        log.info("It never calls this shouldRetry()");        if (i < 3 && response.code() == 404) {            try {                Thread.sleep(delayInMilliseconds);            } catch (InterruptedException e) {                log.info("Error occurred: " + e.getMessage());                Thread.currentThread().interrupt();            }            return true;        }        return false;    }}

For my test, I would run the code, create a B2C User, and then after the User is created, the code above gets called with the User's id to reference the newly created B2C user. I have a breakpoint right before the post from the buildRequest, so I can delete the User from B2C and then I let it continue.

Now instead of retrying 3 times to try and update the User, with a wait time of 500 milliseconds between, the response comes back immediately as a failure. I put a log in the shouldRetry() to see if it ever gets inside the method, but it doesn't. I can't figure out why it is not retrying.


Viewing all articles
Browse latest Browse all 950

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>