简体   繁体   English

Spring Scheduled注释如何工作

[英]Spring Scheduled annotation how does it work

I have created a function in java.That function should run on every day mid night 我在Java中创建了一个函数,该函数应该每天午夜运行

//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 version is 4 春季版是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();
    }
}

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

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