简体   繁体   中英

Task Execution and Scheduling in Spring

I'm new in spring. Please help me to understand what I have to use (TaskExecutor, @Sceduled,Quarts Sceduler,...) to implement this problem:

I have an Order object and Contacts ( connected with 1:N relationship . One Order can have many Contacts). So

  1. When Order is created, application have to send email to all connected Contacts.
  2. When new Contact lately is created and connected to the already created Order, this Contact also have to get an email.
  3. When Order will expire , 2 days later Contact have to get an email.

Step1:

  1. When Order is created, application have to send email to all connected Contacts.

Add 2 new columns into Contacts Table ( or similar table).

is_Send_Email -> boolean type

Email_Send_Time -> timestamp/date type

While inserting a new row ( new order is created), set is_Send_Email= true and Email_Send_Time = current time. for all related Contacts.

2.When new Contact lately is created and connected to the already created Order, this Contact also have to get an email.

When adding a contact to the Order,set is_Send_Email=true and Email_Send_Time = Current time (while inserting) for newly added Contacts .

3.When Order will expire , 2 days later Contact have to get an email.

Set is_Send_Email=true for all contacts in that expiring order and Email_Send_Time = Current time+2 days.

Step2:

Enable scheduling using @EnableScheduling in your configuration class.

@Configuration
@EnableScheduling
public class AppConfig {

    @Bean
    public MyBean bean() {
        return new MyBean();
    }

}

Step3:

Use @Scheduled annotation to call your mail sending method at specific intervals.

As per Spring documentation ..

34.4.2 The @Scheduled Annotation

The @Scheduled annotation can be added to a method along with trigger metadata.

For example, the following method would be invoked every 5 seconds with a fixed delay, meaning that the period will be measured from the completion time of each preceding invocation.

@Scheduled(fixedDelay=5000) public void doSomething() {
 // something that should execute periodically
 }

If a fixed rate execution is desired, simply change the property name specified within the annotation. The following would be executed every 5 seconds measured between the successive start times of each invocation.

@Scheduled(fixedRate=5000) public void doSomething() {
     // something that should execute periodically 
}

For fixed-delay and fixed-rate tasks, an initial delay may be specified indicating the number of milliseconds to wait before the first execution of the method.

@Scheduled(initialDelay=1000, fixedRate=5000) public void
doSomething() {
     // something that should execute periodically 
}

If simple periodic scheduling is not expressive enough, then a cron expression may be provided. For example, the following will only execute on weekdays.

@Scheduled(cron="*/5 * * * * MON-FRI") public void doSomething() {
     // something that should execute on weekdays only 
}

[Tip] You can additionally use the zone attribute to specify the time zone in which the cron expression will be resolved. Notice that the methods to be scheduled must have void returns and must not expect any arguments. If the method needs to interact with other objects from the Application Context, then those would typically have been provided through dependency injection.

Step4:

Check each record in Order table, if is_Send_Email=true for an record, then trigger email for that Order/Contacts whatever.

How to send email using Spring , You can refer this article .

Happy Learning :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM