Spring Scheduled注解是如何工作的 [英] Spring Scheduled annotation how does it work

查看:54
本文介绍了Spring Scheduled注解是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 中创建了一个函数.该函数应该在每天午夜运行

I have created a function in java.That function should run on every day mid night

//My function this function is within UpdateService Class
@Scheduled(cron = "0 0 0 * * ?")
public static void UpdateFn() {
    try {
        System.out.println("-----------Background Task Running----------------");
        //code to update some data every day
        System.out.println("-----------Background Task Ending----------------");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//My xml configuration
 <task:annotation-driven />
    <bean id="UpdateTask" class="com.ss.utility.UpdateService"></bean>
  </beans>

但我没有按预期工作.有时执行有时不执行.对此有任何解决方案.

But i not working as expected.Sometime it executed and sometime not.Any solution for this.

Spring 版本是 4

Spring version is 4

推荐答案

您不应该为此使用静态方法.尝试使用以下代码:

You shoundn't use static method for this. Try to use following code:

@Scheduled(cron = "0 0 0 * * ?")
public void UpdateFn() {
    try {
        System.out.println("-----------Background Task Running----------------");
        //code to update some data every day
        System.out.println("-----------Background Task Ending----------------");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这篇关于Spring Scheduled注解是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆