简体   繁体   English

春季的任务执行和计划

[英]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: 请帮助我了解我必须使用什么(TaskExecutor,@ Sceduled,Quarts Sceduler等)来实现此问题:

I have an Order object and Contacts ( connected with 1:N relationship . One Order can have many Contacts). 我有一个Order对象和Contacts以1:N关系连接 。一个Order可以有多个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步:

  1. When Order is created, application have to send email to all connected Contacts. 创建订单后,应用程序必须向所有连接的联系人发送电子邮件。

Add 2 new columns into Contacts Table ( or similar table). 在联系人表(或类似表)中添加2个新列。

is_Send_Email -> boolean type is_Send_Email->布尔类型

Email_Send_Time -> timestamp/date type Email_Send_Time->时间戳/日期类型

While inserting a new row ( new order is created), set is_Send_Email= true and Email_Send_Time = current time. 插入新行(创建新订单)时,设置is_Send_Email = true和Email_Send_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. 2.最近创建新联系人并连接到已创建的订单时,该联系人还必须获取电子邮件。

When adding a contact to the Order,set is_Send_Email=true and Email_Send_Time = Current time (while inserting) for newly added Contacts . 将联系人添加到订单时,请设置is_Send_Email = true,并且Email_Send_Time = 新添加的联系人的当前时间(插入时)。

3.When Order will expire , 2 days later Contact have to get an email. 3.Order到期时,两天后联系必须获得电子邮件。

Set is_Send_Email=true for all contacts in that expiring order and Email_Send_Time = Current time+2 days. 为该联系列表中的所有联系人设置is_Send_Email = true,并且Email_Send_Time =当前时间+ 2天。

Step2: 第2步:

Enable scheduling using @EnableScheduling in your configuration class. 使用配置类中的@EnableScheduling启用调度。

@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. 使用@Scheduled批注以特定的时间间隔调用您的邮件发送方法。

As per Spring documentation .. 根据Spring 文档 ..

34.4.2 The @Scheduled Annotation 34.4.2 @计划的注释

The @Scheduled annotation can be added to a method along with trigger metadata. 可以将@Scheduled批注与触发器元数据一起添加到方法中。

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. 例如,以下方法将每隔5秒钟以固定的延迟被调用一次,这意味着将从每个先前调用的完成时间开始计算该时间段。

@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. 在每次调用的连续开始时间之间,每5秒执行一次以下操作。

@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. 如果简单的周期性调度不能充分表达,则可以提供cron表达式。 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. [提示]您还可以使用zone属性指定将解析cron表达式的时区。 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. 检查订单表中的每条记录,如果is_Send_Email = true以获取记录,然后触发该订单/联系人的电子邮件。

How to send email using Spring , You can refer this article . 如何使用Spring发送电子邮件,可以参考本文

Happy Learning :-) 快乐学习:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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