Scheduling tasks to run once, using the Spring task namespace

前端 未结 4 1868
暗喜
暗喜 2020-12-25 15:26

I\'m setting up a scheduled tasks scheme in spring, using the task namespace.

I want to schedule most tasks to fire according to a cron expression, and some to fire

4条回答
  •  忘掉有多难
    2020-12-25 15:31

    If you don't need an initial delay, you can make it run 'just once' on startup as follows:

    
         
        
    
    

    (Of course, if you think your code is going to run for longer than 3E8 years, you may need a different approach...)

    If you need an initial delay, you can configure it as follows (I'm testing with Spring 3.1.1) - this doesn't require any additional dependencies and you don't have to write your own trigger, but you do have to configure the PeriodicTrigger provided by Spring:

     
         
         
         
     
     
         
     
    

    Spring 3.2 appears to support the "initial-delay" attribute directly, but I haven't tested this; I'd guess this works:

    
        
    
    

提交回复
热议问题