定时任务

定时任务

定时任务

Scheduling Tasks

通过此教程你将了解如何使用Spring来做定时任务。

This guide walks you through the steps for scheduling tasks with Spring.

将要做什么

What you’ll build

你将创建一个应用,使用Spring的@Scheduled注解来实现每5秒钟输出当前时间。

You’ll build an application that prints out the current time every five seconds using Spring’s @Scheduled annotation.

创建一个定时任务

Create a scheduled task

创建好项目工程后,你就可以开始创建一个定时任务了。

Now that you’ve set up your project, you can create a scheduled task.

src/main/java/hello/ScheduledTasks.java

package hello;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

Scheduled注解定义了一个方法单独运行。注意:此例中使用了fixedRate来指定每次调用的时间间隔。也有的其他选项,例如fixedDelay等来指定调用间的间隔。还可以使用@Scheduled(cron=". . .")表达式来指定更复杂的定时任务

The Scheduled annotation defines when a particular method runs. NOTE: This example uses fixedRate, which specifies the interval between method invocations measured from the start time of each invocation. There are other options, like fixedDelay, which specifies the interval between invocations measured from the completion of the task. You can also use @Scheduled(cron=". . .") expressions for more sophisticated task scheduling.

启用定时任务

Enable Scheduling

虽然定时任务可以嵌入在web应用和WAR包中,下面展示了一个更简单的独立应用的方法。你可以将所有东西打包为一个独立可执行的JAR包,由Java的main()方法来驱动。

Although scheduled tasks can be embedded in web apps and WAR files, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main() method.

src/main/java/hello/Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

创建一个可执行JAR包

Build an executable JAR

打包运行后,可以看见日志输出,每个日志都是在后台线程中。每隔5秒触发一次定时任务。

Logging output is displayed and you can see from the logs that it is on a background thread. You should see your scheduled task fire every 5 seconds:

[...]
2016-08-25 13:10:00.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:00
2016-08-25 13:10:05.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:05
2016-08-25 13:10:10.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:10
2016-08-25 13:10:15.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:15

总结

Summary

恭喜!你完成了一个定时任务应用的创建。而且定时任务部分的代码比构建文件代码还少!此技术可以用在任何类型的应用上。

Congratulations! You created an application with a scheduled task. Heck, the actual code was shorter than the build file! This technique works in any type of application.

翻译部分版权归YahaCode团队所有。仅供学习研究之用,任何组织或个人不得私自以此用于任何形式的商业目的

发表评论