• 定时任务--查数据库--注解实现


    spring MVC 项目:

    1.接口继承Job
    package .quartz;

    import org.quartz.Job;

    public interface Task extends Job{

    }


    2.定义一个注解类
    package .quartz;

    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 * * ?";

    }

    3.定义TaskManager

    package .quartz;

    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.annotation.Resource;
    import org.quartz.CronScheduleBuilder;
    import org.quartz.CronTrigger;
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.JobKey;
    import org.quartz.Scheduler;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.TriggerKey;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Lazy;
    import org.springframework.stereotype.Component;

    @Component
    @Lazy(value = false)
    public class TaskManager {

    @Autowired(required = false)
    private List<Task> taskList;

    @Resource(name = "quartzSchedulerNet")
    private Scheduler scheduler;

    @PostConstruct
    public void init() {
    try {
    if (taskList != null) {
    for (int i = 0; i < taskList.size(); i++) {
    Task task = taskList.get(i);
    String nameSuffix = task.getClass().getName();
    TriggerKey triggerKey = new TriggerKey("trigger_"
    + nameSuffix, "DEFAULT");
    Trigger exitTrigger = scheduler.getTrigger(triggerKey);
    if (exitTrigger != null)
    continue;
    TaskScheduled scheduledCron = task.getClass()
    .getAnnotation(TaskScheduled.class);

    JobKey jobKey = new JobKey("job_" + nameSuffix, "DEFAULT");
    JobDetail jobDetail = JobBuilder.newJob(task.getClass())
    .requestRecovery(true).withIdentity(jobKey).build();
    CronTrigger cronTrigger = TriggerBuilder
    .newTrigger()
    .withIdentity(triggerKey)
    .forJob(jobKey)
    .withSchedule(
    CronScheduleBuilder
    .cronSchedule(scheduledCron.cron()))
    .build();
    scheduler.scheduleJob(jobDetail, cronTrigger);
    }
    scheduler.start();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    4.配置quartzContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- =================================================================== -->
    <!-- SchedulerFactoryBean 总调度 需要将 体任务和触发器部署到调度器quartzScheduler上 -->
    <!-- =================================================================== -->
    <bean id="quartzSchedulerNet"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

    </bean>

    <bean class="..TaskManager" lazy-init="false" />

    </beans>


    5.编写业务实现类

    package com..;

    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.stereotype.Component;
    import..Task;
    import..TaskScheduled;

    @Component
    @TaskScheduled(cron="0 0/5 * * * ?")//5分钟推送一次
    public class dangAnTask implements Task {

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
    System.out.println("推送数据===================---------------------==========定时任务轮询");
    //业务代码
    }

    }

    springboot 项目:

    package com.dangAn.Utils;
    
    import org.quartz.JobExecutionException;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import java.io.UnsupportedEncodingException;
    
    /**
     * 
     *
     * @Author 
     * @Date 
     * @Class 
     * Description
     **/
    @Component
    @EnableScheduling//可以在启动类上注解也可以在当前文件
    public class Task {
    //    @Scheduled(cron = "0 0 12 * * ?")//每天中午12点触发 
    //    @Scheduled(cron = "0 0 */2 * * ?")//两小时
        @Scheduled(cron = "0 0/2 * * * ?")//两分钟
        public void dangAnSendData() throws JobExecutionException, UnsupportedEncodingException {
    //业务逻辑
    }
  • 相关阅读:
    BZOJ 2957: 楼房重建
    模积和(bzoj 2956)
    Four-tuples(2018山东省赛 F)
    Best Rational Approximation( 法里数列)
    K
    Now Loading!!!(ZOJ Problem Set
    Treasure Map(Southeast USA ICPC 2017)
    三角形的内点
    小b和灯泡
    不降的数字
  • 原文地址:https://www.cnblogs.com/lifan12589/p/13098064.html
Copyright © 2020-2023  润新知