gpt4 book ai didi

java - Spring中的任务执行与调度

转载 作者:行者123 更新时间:2023-12-01 09:44:51 24 4
gpt4 key购买 nike

我是 Spring 的新人。请帮助我理解我必须使用什么(TaskExecutor,@Sceduled,Quarts Sceduler,...)来实现这个问题:

我有一个订单对象和联系人(以 1:N 关系连接。一个订单可以有多个联系人)。所以

  1. 创建订单后,应用程序必须向所有连接的联系人发送电子邮件。
  2. 当最近创建新联系人并将其连接到已创建的订单时,该联系人还必须收到一封电子邮件。
  3. 当订单到期时,2 天后联系人必须收到一封电子邮件。

最佳答案

第一步:

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

在联系人表(或类似表)中添加 2 个新列。

is_Send_Email -> boolean 类型

Email_Send_Time -> 时间戳/日期类型

插入新行(创建新订单)时,为设置 is_Send_Email=true 和 Email_Send_Time = 当前时间。所有相关联系人。

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

将联系人添加到订单时,为新添加的联系人设置 is_Send_Email=true 和 Email_Send_Time = 当前时间(插入时)。

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

该到期订单中的所有联系人设置 is_Send_Email=true ,并且 Email_Send_Time = 当前时间+2 天。

第二步:

在配置类中使用@EnableScheduling启用调度。

@Configuration
@EnableScheduling
public class AppConfig {

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

}

第三步:

使用@Scheduled注解以特定的时间间隔调用您的邮件发送方法。

根据 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.

第四步:

检查订单表中的每条记录,如果某条记录的 is_Send_Email=true,则触发该订单/联系人的电子邮件。

如何使用Spring发送邮件,可以引用这个article .

快乐学习:-)

关于java - Spring中的任务执行与调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38148829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com