1

I have a scheduler(using @Scheduler) running between 7 to 9 PM for every 15 mins. It looks for a file every 15 mins. If the file is found then the scheduler should stop for today and run next day again. How to achieve this in spring boot?

Deepa
  • 41
  • 1
  • 9
  • What I understood is your file comes to a location and you are polling the location , I think you are consuming more resources in polling however in my view you should use **WatchService** in Java NIO2 and process once file arrives, I guess file comes once in a day only , otherwise you can do error handling too – Shailesh Chandra Apr 16 '21 at 06:27
  • @ShaileshChandra yes my requirement is similar. Am waiting for a file to come into the system between x and y time. If the file arrives, i need not look for it anymore and stop checking until next day. Can watchService do this? – Deepa Apr 16 '21 at 07:02
  • step 1 -> change your scheduler frequency to run once in day at time x step 2 -> inside scheduler start monitoring folder It will keep on watching until file has received even if file is delayed and once file is process you can stop Monitoring and next day things will repeat for more details read https://stackoverflow.com/questions/40020999/how-to-monitor-folder-directory-in-spring – Shailesh Chandra Apr 16 '21 at 07:08
  • in fact if it is dedicated folder and file comes once in a day only then you can do it without scheduler – Shailesh Chandra Apr 16 '21 at 07:26
  • we r polling fr a database column value. its not a folder. Any suggestion how to do it with scheduler? as watchService wont work here – Deepa Apr 16 '21 at 10:21
  • you need to design it, as far as immediate design is concerned, create a scheduled job which runs once a day and then inside the scheduled job, you can create a TimerTask which runs every 5 minute once timertask is completed you can cancel TimerTask next day things will repeat – Shailesh Chandra Apr 16 '21 at 10:45

2 Answers2

2

Probably the easiest way is to implement it at the level of business logic. Spring provides a way to run a periodic task, that's true, but it can't stop the job for some time if a business case is met (the file is found).

Having said that, you could implement the scheduled job as follows:

@Component
public class MyScheduledJob {
   private LocalDateTime runNextTime = null;
   private boolean isFileFound = false;

   @Scheduled(/**here comes your original cron expression: 7 am to 9 pm with an interval of 15 minutes as you say **/)
   public void runMe() {
     if(isFileFound && LocalDateTime.now().isBefore(runNextTime)) {
       // do not run a real job processing
       return;
     }

     isFileFound = checkWhetherFileExists();
     if(isFileFound) {
       runNextTime = calculateWhenDoYouWantToStartRunningTheActualJobProcessingNextTime();
     }
     
     ... do actual job processing... Its not clear from the question whether it should do something if the file is not found as well, but I'm sure you've got the point ...
       
   }
}

Since the bean is a singleton you can safely create a state for it, no-one won't change that state anyway.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

You can do something like this. You can write your logic for checking files inside this method.

@Scheduled(fixedDelay = 1000, initialDelay = 1000)
    public void scheduleFixedRateWithInitialDelayTask() {
     
        long now = System.currentTimeMillis() / 1000;
        System.out.println(
          "Fixed-rate task with one-second initial delay - " + now);
    }

See this for more details.

kameshsr
  • 327
  • 3
  • 13
  • Hi @Kameshsr, we are using @ Scheduled but need to stop the scheduler once the file arrives. the scheduler should start again next day as per the scheduled time and do the same job. how to achieve this using @ Scheduled? or is there any there way? – Deepa Apr 16 '21 at 07:05