How to integrate spring batch+spring boot+Quartz

Asked

Viewed 324 times

2

Good morning. Currently in my project I am using spring boot and spring batch. Creating the batch was easy, even running it when starting the application with Tomcat embebed from spring boot but need to run this batch from time to time. The solution that comes to mind is to use Quartz to run this batch, but I’m not succeeding with this integration.. Can someone help me with some example?

  • Did the answer help you? Some additional information was missing?

1 answer

3

Use the annotation @Scheduled spring.

Create class to run scheduled task:

@Service
public class AgendadorService {

    @Scheduled(fixedRate = 5000)
    public void executaBatch() {
        //Implementar chamada para o batch
    }

}

In this case the task runs every 5 seconds, but there are other changes for scheduling, according to the documentation.

To enable the scheduling feature in Spring Boot, you need to add the annotation @EnableScheduling, as follows:

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}
  • Thanks for the answer! The only drawback is that in this case I can not parameterize the fixedRate = 5000, this way the time gets hardcode. How do pro fixrate get parameterized?

  • You can put it in a properties and access it this way: (fixedRate = ${api.cron})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.