Spring Boot Scheduler throws InterruptedException

1.2k Views Asked by At

I have a SpringBoot Scheduler which is executed every 1 sec. The scheduler throws InterruptedException . Now ,this scheduler often stops working and then gets restarted automatically after few minutes. The InterruptedException is not handled anywhere in the code. So ,can this be the reason of the scheduler stopping? If yes , how can this exception be handled? Below is the code snippet-

@Scheduled(fixedRateString = "${OUTBOUND_MESSAGE_CHK_SCHEDULE_FREQUENCY}")
    
public void fetchOutBoundMessages() throws InterruptedException{
        
log.debug("fetchOutBoundMessages started>>");
        
if(LockHolder.hasValidLock()){
            
log.debug("Fetching Outbound message for >>>>>>>>>");
            
JSONArray jsonArrayObj= service.putOutBoundService(boroCode);
        }
        
else {
            
log.info("Lock has not available..");
        }
    }```
1

There are 1 best solutions below

0
Alexey Veleshko On

InterruptedException is an exception that gets thrown by Java synchronization primitives when the thread they are waiting in gets interrupted from another thread via the Thread.interrupt method. The Thread.interrupt method is the Java way for gracefully cancelling concurrent tasks.

In your case it means that the method fetchOutBoundMessages was waiting on a lock of some sort and there was a thread in your system that for some reason decided to call interrupt on the thread running fetchOutBoundMessages.

Maybe try to set a breakpoint on Thread.interrupt to see who does that and why.