简体   繁体   English

使用 spring 调度任务

[英]Scheduling a task with spring

I'm using java Spring framework for my web application and i want to schedule a task to run in the server at particular time.我正在为我的 Web 应用程序使用 java Spring 框架,我想安排一个任务在特定时间在服务器中运行。 The time is given by user of the application.时间由应用程序的用户提供。

I looked into TaskScheduler and I found examples like,我查看了TaskScheduler ,我发现了这样的例子,

@Configuration
@EnableAsync
@EnableScheduling
public class MyComponent {

    @Async
    @Scheduled(fixedDelay=5000)
    public void doSomething() {
       System.out.println("Scheduled Task");
    }
}

Same task happen over and over again in regular intervals.相同的任务以固定的时间间隔一遍又一遍地发生。 It prints "Scheduled task" in every 5 seconds.But I want to print that only one time (ex: at 11.35 am)它每 5 秒打印一次“计划任务”。但我只想打印一次(例如:上午 11.35)

Can any one help me with that.任何人都可以帮助我。

如果您只想运行任务一次,那么您必须提供 cron 表达式,使其包含整个日期和时间(包括年份),以便它只匹配一次。

Simplest fix, perhaps not best for practice最简单的修复,可能不适合练习

The easy fix is to add an if statement to your method and only print if the current time matches the time that you do want to print out.简单的解决方法是在您的方法中添加一个 if 语句,并且仅在当前时间与您想要打印的时间匹配时才打印。

Java has not always had the best time/date implementations, but as of Java 8, there is a new library called LocalDateTime and it works nicely. Java 并不总是有最好的时间/日期实现,但从 Java 8 开始,有一个名为LocalDateTime的新库,它运行良好。 I think using it will be your best bet for this simple solution.我认为使用它是这个简单解决方案的最佳选择。

Here's an example of using LocalDateTime to get the current hour, minute, and second.下面是使用LocalDateTime获取当前小时、分钟和秒的示例。 There are similar methods for year, month, day, etc.年、月、日等也有类似的方法。

LocalDateTime now = LocalDateTime.now();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();

We can combine this with your current code to only have the print statement executed at the time you desire:我们可以将它与您当前的代码结合起来,只在您希望的时候执行打印语句:

@Configuration
@EnableAsync
@EnableScheduling
public class MyComponent {

    @Async
    @Scheduled(fixedDelay=5000)
    public void doSomething() {

        LocalDateTime now = LocalDateTime.now();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();

        if(hour == 10 &&
           minute == 34 &&
           second < 6) {

            System.out.println("Scheduled Task");

        }
 
    }

}

You'll compare hour to 10 and minute to 34 because these functions return a number from 0 to n-1 where n is the number of hours in a day or minutes in an hour.您将小时与 10 进行比较,将分钟与 34 进行比较,因为这些函数返回一个从 0 到 n-1 的数字,其中 n 是一天中的小时数或一小时中的分钟数。 You'll compare second to <6 so that no matter when this job starts (since it is executed once every 5 seconds) you'll always print at least once every day at 11:35am.您将 second 与 <6 进行比较,以便无论此作业何时开始(因为它每 5 秒执行一次),您将始终在每天上午 11:35 至少打印一次。


I'm definitely not trying to suggest that this is the best option, but it is definitely an option.我绝对不是要建议这是最好的选择,但这绝对是一个选择。 Making a new instance of LocalDateTime every 5 seconds isn't ideal, but if resources aren't a problem then it's fine.每 5 秒创建一个 LocalDateTime 的新实例并不理想,但如果资源不是问题,那就没问题了。

It's not the prettiest or best designed solution, but it works.这不是最漂亮或设计最好的解决方案,但它有效。 This is an option if you just want a quick solution and you don't care about the correct "practice" for accomplishing this task.如果您只想要一个快速的解决方案并且您不关心完成此任务的正确“实践”,那么这是一个选项。

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

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