2

In a spring-boot application, I need to create a scheduled task that executes an exposed method (in the application domain).

Through the graphical interface, the user can configure the execution time and periodicity.

I already have this part.

The problem is to know if using quartz or similar library is possible to program the task and to be reprogrammed if the user modifies the configuration.

Please can you give me documentation about it so I can configure it this way.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
  • You can find several good solutions [here](http://stackoverflow.com/questions/37335351/schedule-a-task-with-cron-which-allows-dynamic-update) and [here](http://stackoverflow.com/questions/14630539/scheduling-a-job-with-spring-programmatically-with-fixedrate-set-dynamically/14632758#14632758) – dmytro Apr 28 '17 at 15:24
  • You should at least try to make a research in Google with "spring boot scheduled tasks". – Mickael Apr 28 '17 at 16:03

1 Answers1

2

you can use @Scheduled annotation of spring. You can annotate any method on the spring component by specifying the delaystring ,i.e. the interval it should be running after. This can be configured in the properties file. To specify interval you can use "fixedRate" or "fixedDelay".

fixedRate- Executes the new run even if previous run of the job is still in progress.

fixedDelay-controls the next execution time when the last execution finishes.

This had helped me in the past.

  1. https://spring.io/guides/gs/scheduling-tasks
  2. https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

1.You can create the JOB class containing the task to be done:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyScheduledTask {

    private static final SimpleDateFormat currentTime = 
        new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    @Scheduled(fixedRate = 2000) //interval in millisconds
    public void doSomeTask() {

        System.out.println("doSome task exceuted at " 
            + currentTime.format(new Date()));

    }
}

2.Main Spring boot class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(new Object[] { Application.class }, args);

    }

}

Hope this helps you a bit.

Mani
  • 283
  • 3
  • 21