0

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..");
        }
    }```
  • It's not clear where `InterruptedException` comes from (which method). – Alexey Veleshko Jan 11 '22 at 16:15
  • fetchOutBoundMessages() is the scheduling method which throws InterruptedException according to the code – Mimansa Sinha Jan 11 '22 at 16:43
  • It's not quite clear what you want to achieve, as you're saying running the method at every second, and it's throwing an exception in a few minutes. (Probably the scheduled method is running in multiple threads at the same time). For handling the exception you can look at this article https://www.baeldung.com/java-interrupted-exception – Amrit Malla Jan 11 '22 at 17:33

1 Answers1

0

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.

Alexey Veleshko
  • 792
  • 1
  • 18