How to implement maximum 3 try for requesting resource in rest api java

Viewed 174

I want to do something like, I should allow user to request for 3 times. After that only it should throw error.

I have go through many articles, they said to use something like :

for(int i=0;i<3;i++)
{
  try
  {
  }
  catch(Exception e)
  {
    if(count<3)
    {
        count++;
        countinue;
   }
}

I tried implementing this, but this executed in just one request.

What I want to do is that for each request made it should check for count.

e.g when I make a request through postman once, it should check for one.

When I again made request through postman it should increase count to 2 and at last for 3rd time it should throw error.

Here is the sample code here :

    @PUT
    @Path("/{containerId}/assignnexttask")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response somemethod(@Context HttpHeaders headers, @PathParam("containerId") String containerId,
            @RequestBody String payload) throws JSONException {
        Variant v = RestUtils.getVariant(headers);
        String contentType = RestUtils.getContentType(headers);
        MarshallingFormat format = MarshallingFormat.fromType(contentType);
        // for loop is for maximum 3 attempts ,
        int count=0;
        for (int i = 0; i < 3; i++) {
            try { 
              //here is some code 
            }
            catch(Exception e )
            {
               if(count<3)
               {
                  count++;
                  continue;
               }
             }
         }
     }

Hope i am able to explain.Thanks in Advance.

2 Answers

Rather you should updated your code as below (sample pseudo code). (Assuming that if there is error on first and second request null should be returned as Response).

So you need to maintain the static variable. And after error successful request or before error throw, variable should be re-initialized to 0 to make it ready for subsequent requests.

//class level variable to count the error
private static int count = 0;

    @PUT
        @Path("/{containerId}/assignnexttask")
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        public Response somemethod(@Context HttpHeaders headers, @PathParam("containerId") String containerId,
                @RequestBody String payload) throws JSONException {
            Variant v = RestUtils.getVariant(headers);
            String contentType = RestUtils.getContentType(headers);
            MarshallingFormat format = MarshallingFormat.fromType(contentType);
            // for loop is for maximum 3 attempts ,
            try { 
                  //here is some code

                   // count = 0 to make it ready for next request if no error
                   count = 0;
                   // Response data
                   return response;
            }
            catch(Exception e ) {
                   if(count<3)
                   {
                      count = count + 1;
                      return null;
                   }
                   // count = 0 to make it ready for next request
                   count = 0;
                   throw e;
             }
        }

Like this?

for(int i=0;;i++)
{
  try
  {
     doSomething();
     break;
  }
  catch(Throwable e)
  {
    if(i==2)
    {
       throw e;
    }
  }
}
Related