Scheduling a function in Spring boot

757 Views Asked by At

I am trying to schedule the following function to run every 10seconds. But it tells that it needs a void function. Is there any way to schedule the following function?

@Scheduled(fixedRate = 10000)
public FtpOutboundGateway getFiles() {
    FtpOutboundGateway gateway = new FtpOutboundGateway(sf(), "mget", "payload");
    gateway.setAutoCreateDirectory(true);
    gateway.setLocalDirectory(new File("./downloads/"));
    gateway.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
    gateway.setFilter(new AcceptOnceFileListFilter<>());
    gateway.setOutputChannelName("fileResults");
    return gateway;
}
2

There are 2 best solutions below

0
On BEST ANSWER

For this, we need to do the following things.

  1. First put this Annotation @EnableScheduling at the class level.
  2. then at the function which needs to be scheduled should be marked with @Scheduled.
  3. The function that we are trying to schedule should not return anything.
@EnableScheduling
class <ClassName>{
//def

 @Scheduled(fixedDelayString = "10000")
 public void fxnName() {
   //def
}

}
0
On

Since @Scheduled method is called internally by the threads in thread pool in a given interval or time, the annotation expect it to be void because it doesn't make sense to return values. if you want to track down the response the extract it to another method and call it from the @Scheduled method as well as from other services.