Spring动态配置计时器触发时间的实例代码-创新互联

Spring中添加计时器的时候根据业务需求可能会需要动态处理触发时间;

我们提供的服务有:成都网站建设、做网站、微信公众号开发、网站优化、网站认证、耿马ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的耿马网站制作公司
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.scheduling.Trigger; 
import org.springframework.scheduling.TriggerContext; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.scheduling.annotation.SchedulingConfigurer; 
import org.springframework.scheduling.config.ScheduledTaskRegistrar; 
import org.springframework.scheduling.support.CronTrigger; 
import org.springframework.stereotype.Component; 
import java.util.Date; 
@Component 
@EnableScheduling 
public class SchedulerTest implements SchedulingConfigurer { 
  private final static Logger logger = LoggerFactory.getLogger(SchedulerTest.class); 
  public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { 
    // 注册计时任务到Scheduling接口 
    scheduledTaskRegistrar.addTriggerTask(new Runnable() { 
      public void run() { 
        logger.info("task job is running..."); 
      } 
    }, new Trigger() { 
      public Date nextExecutionTime(TriggerContext triggerContext) { 
        CronTrigger cronTrigger = new CronTrigger("时间表达式"); 
        Date nextExec = cronTrigger.nextExecutionTime(triggerContext); 
        return nextExec; 
      } 
    }); 
  } 
} 

网站栏目:Spring动态配置计时器触发时间的实例代码-创新互联
网页链接:http://myzitong.com/article/didpdc.html