@Component
public class MyScheduler {
int i =0;
public void run(){
i++;
// 任务逻辑
System.out.println("第"+(i)+"次开始执行操作... " +"时间:【"
+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").format(new Date())
+ "】");
}
}
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" ...... xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<task:scheduled-tasks>
<task:scheduled ref="myScheduler" method="run" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
@Service
@Lazy(false)
@EnableScheduling
public class MyScheduler extends BaseService implements SchedulingConfigurer{
public static String cron = "0/2 * * * * ?";
int i =0;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
i++;
// 需要实现的任务逻辑
logger.info("第"+(i)+"次开始执行操作... " +"时间:【"
+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").format(new Date())
+ "】");
}
}, new Trigger(){
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
//任务触发,可修改任务的执行周期
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}
<task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/> <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有