我每天早上都会从后端 Spring-MVC
java 应用程序向 Android 应用程序发送推送通知。为此,我创建了一个 cron 作业,并在 WebConfig(@EnableScheduling
) 中初始化了一个 bean。这个 bean 每天早上都会发送通知。
但是如果用户没有阅读它,那么只有我必须在晚上的特定时间发送另一个通知。否则我不应该发送任何东西。如何编写Cron expressen或调度程序或设置计时器以在特定时间仅发送一次时间,仅限当天?
最佳答案
仅启动一次 cron
进程没有多大意义......
模式 0 0 小时-分钟 * * ?
会将任务编程为一小时一分钟,但每天:
0 0 15-45 * * ? // will execute task at 15:45
但要实现这一点,请查看 this answer that shows how to use a Timer to create a thread that runs when needed :
private static class MyTimeTask extends TimerTask
{
public void run()
{
//write your code here
}
}
public static void main(String[] args) {
//the Date and time at which you want to execute
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormatter .parse("2012-07-06 13:05:45");
//Now create the time and schedule it
Timer timer = new Timer();
timer.schedule(new MyTimeTask(), date);
}
关于java - 如何编写一个cron表达式或设置一个在Spring中只执行一次的计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30842713/