I need to implement a resource which needs to verify payments via Google 
Play's API.

Unfortunately, google play's access tokens expire every hour. So 1) I need 
to generate a token, 2) As incoming requests arrive at /my/api/android I 
need to handle them using the token generated, 3) In 1 hour the token will 
have expired and will need to be re-generated.

Its step # 3 that's causing me issues. There could be multiple concurrent 
requests hitting the api, and multiple requests might hit it at the same 
time as the token expires.

I was thinking of resolving it via something like this:

public class MyResource
{
    private final GooglePlayClient client = new GooglePlayClient(); //I 
assume this will be a singleton, right?
   @POST
    public Foo handleRequest(Bar someParams)
   {
        String token = client.getToken();
        //Make a request to play's API via the token and return the result
   }
}




public class GooglePlayClient
{
    private String token;
    private long tokenExpireTime;


   public GooglePlayClient()
   {
       genNewToken();
   }


    private void genNewToken()
    {
        token = //get new token via google play's api
        tokenExpireTime = new Date().getTime();
    }
}


   public synchronized String getToken()
   {
        if (new Date().getTime() >= tokenExpireTime)
              genNewToken();


       return token;
  }


}

Will the above implementation work for handling the concurrency / thread 
safety aspect? Is there a better way?

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to