1

我在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>

但我没有按预期工作。有时它执行,有时没有。任何解决方案。

春季版是4

4

1 回答 1

1

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();
    }
}
于 2016-02-03T09:44:22.123 回答