2

I am trying to run the task for every 5 minutes using ScheduledThreadPoolExecutor's scheduleAtFixedRate but it is running only for the first time, please let me know what I am doing wrong here. Thanks.

@RestController
@RequestMapping("/app")
public class MyRestController {
    private RequestValidator validator;
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
    private MyOtherServiceResponse myOtherServiceResponse;

    @Autowired
    public MyRestController(RequestValidator validator, MyOtherServiceResponse myOtherServiceResponse){
        this.validator = validator;
        this.myOtherServiceResponse = myOtherServiceResponse;
        startScheduler();// calling scheduler for the first time to start 
    }

    @RequestMapping( value = "/getDetails/{prdId}", method= RequestMethod.GET, produces={"application/json"})
    public ResponseEntity<> getProductInfo(@PathVariable(value="prdId") String prdId) {
        long endTime = 0;
        long startTime = 0;
        startTime = System.currentTimeMillis();
        if((validator.validateRequest(prdId))== null) {
            ServiceRequest serviceRequest = validator.getServiceRequest();
            endTime = System.currentTimeMillis() - startTime;
            //getThisServiceResponse(serviceRequest, prdId); 
            System.out.println("Service final Time --------------    " + endTime + "  prdId : " + prdId);
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else {
            errorResponse = validator.validateRequest(prdId);
            return new ResponseEntity<>(errorResponse, HttpStatus.OK);
        }

    }

    public void startScheduler(){
        executor.scheduleAtFixedRate(() -> myOtherServiceResponse.getOtherResponse(validator.getServiceRequest(), validator.PRDID), 0, 5, TimeUnit.MINUTES);
    }
}

The startScheduler() is called by MyController Constructor, but as per the scheduler it should run getOtherResponse for every 5 minutes

user1653027
  • 789
  • 1
  • 16
  • 38
  • 1
    Do you know what's the meaning of the last three arguments in the last line ? `executor.scheduleAtFixedRate(() -> myOtherServiceResponse.getOtherResponse(validator.getServiceRequest(), validator.PRDID), 0, 2, TimeUnit.MINUTES)` – Nir Alfasi Feb 11 '17 at 03:01
  • If the lambda throws an exception, you probably won't see it, and the task will be cancelled. – David Ehrmann Feb 11 '17 at 03:41
  • @alfasin yes, 0 is no delay, 2 is to run every two minutes and TimeUnit.MINUTES is to tell the 2 is in minutes, so to run every 5 minutes I have 5 in place of 2 – user1653027 Feb 11 '17 at 05:38
  • @DavidEhrmann the lamda didn't through any exception , it worked for the first time – user1653027 Feb 11 '17 at 05:40
  • @alfasin I used 2 for testing purpose just to reduce wait time for me to test this scheduler. I have replaced 2 with 5 in my question. Please let me know if I am doing anything wrong – user1653027 Feb 11 '17 at 05:43
  • It still could have thrown an exception the second time. – David Ehrmann Feb 12 '17 at 01:06
  • I'm surprised `myOtherServiceResponse` and `validator` didn't have to be final. I know there's the "effective final" thing, but when it's a member variable, it isn't nearly as safe as a local variable. It also makes it weird that changing a variable from `private` to `protected` could break compilation. – David Ehrmann Feb 12 '17 at 01:08
  • Is it possible MyRestController is never constructed and you're seeing someone else call getOtherResponse? – David Ehrmann Feb 12 '17 at 01:10
  • @DavidEhrmann Thanks a lot for your time, the only way I am making call to getOtherResponse is from scheduler – user1653027 Feb 12 '17 at 05:49
  • Try this: replace your lambda with `() -> System.err.printf("Executor running at %d%n", System.currentTimeMillis)` and lower the 5 minutes to something like 10 seconds. Does it get called every 10s? – David Ehrmann Feb 12 '17 at 06:33
  • Also, can you distill this down to a minimally reproducible example? We're left guessing a parts, here. – David Ehrmann Feb 12 '17 at 06:34
  • 1
    @DavidEhrmann sincere apologies for wasting your time, you are correct at the first time it self `If the lambda throws an exception, you probably won't see it, and the task will be cancelled` while digging it deeply found one method was not handled for the exceptions. Fixed the issue by handling exceptions in all method calls. Really appreciate your help and sincere apologies once again – user1653027 Feb 13 '17 at 00:59

0 Answers0