Spring Scheduling Annotations – Spring调度注释

最后修改: 2018年 6月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

When single-threaded execution isn’t enough, we can use annotations from the org.springframework.scheduling.annotation package.

当单线程执行还不够时,我们可以使用org.springframework.scheduling.annotation包中的注释。

In this quick tutorial, we’re going to explore the Spring Scheduling Annotations.

在这个快速教程中,我们将探索Spring调度注释。

2. @EnableAsync

2.@EnableAsync

With this annotation, we can enable asynchronous functionality in Spring.

通过这个注解,我们可以在Spring中启用异步功能。

We must use it with @Configuration:

我们必须与@Configuration一起使用它。

@Configuration
@EnableAsync
class VehicleFactoryConfig {}

Now, that we enabled asynchronous calls, we can use @Async to define the methods supporting it.

现在,我们启用了异步调用,我们可以使用@Async来定义支持它的方法。

3. @EnableScheduling

3.@EnableScheduling

With this annotation, we can enable scheduling in the application.

有了这个注解,我们可以在应用程序中启用调度。

We also have to use it in conjunction with @Configuration:

我们还必须把它与@Configuration结合起来使用。

@Configuration
@EnableScheduling
class VehicleFactoryConfig {}

As a result, we can now run methods periodically with @Scheduled.

因此,我们现在可以用@Scheduled定期运行方法。

4. @Async

4. @Async

We can define methods we want to execute on a different thread, hence run them asynchronously.

我们可以定义我们想在不同的线程上执行的方法,从而异步地运行它们。

To achieve this, we can annotate the method with @Async:

为了实现这一点,我们可以用@Async来注解该方法。

@Async
void repairCar() {
    // ...
}

If we apply this annotation to a class, then all methods will be called asynchronously.

如果我们把这个注解应用于一个类,那么所有的方法都将被异步调用。

Note, that we need to enable the asynchronous calls for this annotation to work, with @EnableAsync or XML configuration.

注意,我们需要通过@EnableAsync或XML配置来启用该注解的异步调用,以便工作。

More information about @Async can be found in this article.

关于@Async的更多信息可以在这篇文章中找到。

5. @Scheduled

5.@Scheduled(预定)

If we need a method to execute periodically, we can use this annotation:

如果我们需要一个方法来定期执行,我们可以使用这个注解。

@Scheduled(fixedRate = 10000)
void checkVehicle() {
    // ...
}

We can use it to execute a method at fixed intervals, or we can fine-tune it with cron-like expressions.

我们可以用它以固定的时间间隔执行一个方法,也可以用类似cron的表达式对其进行微调。

@Scheduled leverages the Java 8 repeating annotations feature, which means we can mark a method with it multiple times:

@Scheduled利用了Java 8的重复注释功能,这意味着我们可以用它多次标记一个方法。

@Scheduled(fixedRate = 10000)
@Scheduled(cron = "0 * * * * MON-FRI")
void checkVehicle() {
    // ...
}

Note, that the method annotated with @Scheduled should have a void return type.

注意,用@Scheduled注释的方法应该有一个void的返回类型。

Moreover, we have to enable scheduling for this annotation to work for example with @EnableScheduling or XML configuration.

此外,我们必须为这个注释启用调度,例如使用@EnableScheduling或XML配置。

For more information about scheduling read this article.

关于调度的更多信息,请阅读这篇文章

6. @Schedules

6.@Schedules(时间表)

We can use this annotation to specify multiple @Scheduled rules:

我们可以使用这个注解来指定多个@Scheduled规则。

@Schedules({ 
  @Scheduled(fixedRate = 10000), 
  @Scheduled(cron = "0 * * * * MON-FRI")
})
void checkVehicle() {
    // ...
}

Note, that since Java 8 we can achieve the same with the repeating annotations feature as described above.

注意,从Java 8开始,我们可以通过上述的重复注解功能实现同样的目的。

7. Conclusion

7.结语

In this article, we saw an overview of the most common Spring scheduling annotations.

在这篇文章中,我们看到了一个最常见的Spring调度注释的概述。

As usual, the examples are available over on GitHub.

像往常一样,这些例子可以在GitHub上找到over

Next »

Spring Data Annotations

« Previous

Spring Boot Annotations