用到 : quartz-2.2.3.jar
spring web :
1.定义一个注解类: import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface TaskScheduled { /** * 触发时间 * * @return */ public String cron() default "0 0 23 * * ?"; } 2.继承Job import org.quartz.Job; public interface Task extends Job{ } 3.实现Task,做业务处理 import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.stereotype.Component; import com.Task; import com.TaskScheduled; @Component @TaskScheduled(cron="0 0/30 * * * ?")//30分钟轮循一次 public class textTask implements Task { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("推送数据===================---------------------==========定时任务轮询"); }
spring boot :
import org.quartz.JobExecutionException; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * * * @Author * @Date * @Class * Description **/ @Component @EnableScheduling//可以在启动类上注解也可以在当前文件 public class textTask { // @Scheduled(cron = "0 0 12 * * ?")//每天中午12点触发 // @Scheduled(cron = "0 0 */2 * * ?")//两小时 @Scheduled(cron = "0 0/2 * * * ?")//两分钟 public void execute() throws JobExecutionException { System.out.println(" "+"推送数据===================---------------------==========定时任务轮询"); }