What is best way to schedule task in spring boot application
Asked Answered
S

5

23

I am current developing an application based on Spring-Boot.

I know that annotation like @Scheduled can schedule tasks. Since users in my application wanna send mails at different time and send only once.

I have already read the post Spring scheduling task - run only once, but it is weird always "new" an localExecutor in a Spring based application.

In that way , once a user schedule sending an email, I have to "new" an localExecutor for his task.

So , are there any better ways?

Sensualist answered 28/1, 2016 at 14:46 Comment(1)
For more dynamic use cases checkout #46974772Passive
W
9

you should use quartz-scheduler and send mails at different time and send only once.- put this as a business logic in your code. Please see for spring boot -quartz integration https://github.com/davidkiss/spring-boot-quartz-demo

Wifeless answered 28/1, 2016 at 14:56 Comment(0)
F
27

The simplest way to schedule tasks in Spring is to create method annotated by @Scheduled in spring managed bean. It also required @EnableScheduling in any @Configuration classes.

Spring tutorial

Fusco answered 28/1, 2016 at 14:52 Comment(4)
yes , I have already use this kind of annotations for scheduling task. But in my condition , a user only want to send his mail once at a given time. So if I use the @Schedule annotation , I have to “polling” check whether an mail should be send each several minutes or hours.....Sensualist
Hope this can be helpful for youNicko
Does this works smooth when you have multiple instances of one service in production?Frederickfredericka
Checkout use of shedlock with spring for making this work better in clustered environments (multiple instances)Shelter
D
15

You can use crontab inside @Scheduled

 private AtomicInteger counter = new AtomicInteger(0);

@Scheduled(cron = "*/2 * * * * *")
public void cronJob() {
    int jobId = counter.incrementAndGet();
    System.out.println("Job " + new Date() + ", jobId: " + jobId);
}
Denudate answered 4/5, 2017 at 6:9 Comment(0)
W
9

you should use quartz-scheduler and send mails at different time and send only once.- put this as a business logic in your code. Please see for spring boot -quartz integration https://github.com/davidkiss/spring-boot-quartz-demo

Wifeless answered 28/1, 2016 at 14:56 Comment(0)
O
1

Spring @Scheduled annotation will execute multiple times if you have more than one instance of your app where you have @Scheduled annotation.

If you are using PCF, you can use PCF scheduler https://docs.pivotal.io/scheduler/1-2/using-jobs.html to avoid this issue. Using Tasks can solve this issue.

Ovation answered 11/8, 2020 at 19:15 Comment(0)
E
-1

I have implemented a 'cron job' system using Quartz Scheduler and Spring Boot Scheduler.

The system supports scheduling based on start and end dates.

  1. It supports scheduling based on daily, weekday, monthly, and weekly frequencies.
  2. It supports running on multiple application instances, but only runs on one
  3. server at a time.
  4. It generates cron expressions dynamically.
  5. It supports scheduling based on time zone.
  6. It is built on Java 8.
  7. And more.

Let me know if anyone needs it, and I will provide the package format

Elenaelenchus answered 30/4, 2023 at 18:22 Comment(1)
This is not the solution, if you have implemented something, you can share the github link for thatElson

© 2022 - 2024 — McMap. All rights reserved.