数据拉取之Quartz实现动态job(一)

目录

前言

在业务中我们经常会使用定时任务去处理一些需求。而一般的应用场景主要为数据同步,数据推送,定时处理一些业务数据等。而java传统的做法为

  • TimerTask
    java自带的java.util.Timer类,使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行

  • ScheduledExecutorService
    jdk自带的一个类;是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响

  • Spring Task
    Spring3.0以后自带的task,以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多

  • Quartz
    可以让你的程序在指定时间执行,也可以按照某个频度执行

传统做法

TimerTask

先上代码

  public static void main(String[] args) {
        //1 创建定时任务
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("task :"+new Date());
            }
        };

        //2 创建定时器
        Timer timer = new Timer();

        //3 开始执行(每隔3s打印当前时间)
        timer.schedule(task,10,3000);

    }

而其调用只需要一个Timer类,主要用来维护定时任务,以及TimerTask 类用来执行具体任务的类,而任务类实现Runnable类,并且重写了run方法

I 源码分析之Timer

public class Timer {
    /**
     * 任务队列,存放待执行的任务
     */
    private final TaskQueue queue = new TaskQueue();

    /**
     * TimerThread线程,负责管理待执行的任务,并且以quene为参数构造
     */
    private final TimerThread thread = new TimerThread(queue);

而TaskQuene中维护了一个数组,调用schedule时,用来添加相应元素,并且根据执行时间的先后对数组元素进行排序,从而确定最先开始执行的任务

class TaskQueue {
    /**
     * 数组,保存元素
     */
    private TimerTask[] queue = new TimerTask[128];

TimerThread线程在对象初始化时,执行run方法

  public void run() {
        try {
            mainLoop();
        } finally {
            // Someone killed this Thread, behave as if Timer cancelled
            synchronized(queue) {
                newTasksMayBeScheduled = false;
                queue.clear();  // Eliminate obsolete references
            }
        }
    }

该线程中mainLoop方法,可以发现,当队列为空时,让queue去休息区区等待,并且跳出循环。
如果队列不为空,得到任务队列的首元素,并判断是否达到执行时间,如果没有,则进行休眠
private void mainLoop() {
        while (true) {
            try {
                TimerTask task;
                boolean taskFired;
                synchronized(queue) {
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && newTasksMayBeScheduled)
                        queue.wait();
                    if (queue.isEmpty())
                        break; // Queue is empty and will forever remain; die

                    // Queue nonempty; look at first evt and do the right thing
                    long currentTime, executionTime;
                    task = queue.getMin();
                    synchronized(task.lock) {
                        if (task.state == TimerTask.CANCELLED) {
                            queue.removeMin();
                            continue;  // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis();
                        executionTime = task.nextExecutionTime;
                        if (taskFired = (executionTime<=currentTime)) {
                            if (task.period == 0) { // Non-repeating, remove
                                queue.removeMin();
                                task.state = TimerTask.EXECUTED;
                            } else { // Repeating task, reschedule
                                queue.rescheduleMin(
                                  task.period<0 ? currentTime   - task.period
                                                : executionTime + task.period);
                            }
                        }
                    }
                    if (!taskFired) // Task hasn't yet fired; wait
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired)  // Task fired; run it, holding no locks
                    task.run();
            } catch(InterruptedException e) {
            }
        }
    }

timer执行scheduler方法,会向队列添加元素,并唤醒TaskQueue队列上的线程,这时候TimerThread会被唤醒,继续执行mainLoop

   public void schedule(TimerTask task, long delay, long period) {
        if (delay < 0)
            throw new IllegalArgumentException("Negative delay.");
        if (period <= 0)
            throw new IllegalArgumentException("Non-positive period.");
        sched(task, System.currentTimeMillis()+delay, -period);
    }

 private void sched(TimerTask task, long time, long period) {
        if (time < 0)
            throw new IllegalArgumentException("Illegal execution time.");

        // Constrain value of period sufficiently to prevent numeric
        // overflow while still being effectively infinitely large.
        if (Math.abs(period) > (Long.MAX_VALUE >> 1))
            period >>= 1;

        synchronized(queue) {
            if (!thread.newTasksMayBeScheduled)
                throw new IllegalStateException("Timer already cancelled.");

            synchronized(task.lock) {
                if (task.state != TimerTask.VIRGIN)
                    throw new IllegalStateException(
                        "Task already scheduled or cancelled");
                task.nextExecutionTime = time;
                task.period = period;
                task.state = TimerTask.SCHEDULED;
            }

            queue.add(task);
            if (queue.getMin() == task)
                queue.notify();
        }
    }

可以发现通过timer和timer task可以实现一个简单的定时任务。并且线程与队列的巧妙结合很值得借鉴。但是如果任务过多时,会生成很多资源,这个比较耗资源,同时线程的调度也非常占资源。所以基本很少用该方式

ScheduledExecutorService

先上代码

 public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(()->System.out.println("task: "+new Date()), 0, 3, TimeUnit.SECONDS);
    }

该方式是基于线程池的方式进行创建的,而线程池主要有四个
I newSingleThreadExecutor:单线程池,同时只有一个线程在跑。
II newCachedThreadPool() :回收型线程池,可以重复利用之前创建过的线程,运行线程最大数是Integer.MAX_VALUE。
III newFixedThreadPool() :固定大小的线程池,跟回收型线程池类似,只是可以限制同时运行的线程数量
IV newSingleThreadScheduledExecutor():可以实现循环或延迟任务

而对比timer,可以知道
ScheduledExecutorService支持多个任务并发执行 后者是单线程,如果程序运行报错时,timer会停止所有任务的运行,同时timer时间间隔是依赖于系统的时间,而ScheduledExecutorService是基于时间的延迟

ThreadPoolExecutor一些参数配置,如
corePoolSize: 线程池中的核心线程数,如果当前线程数为corePoolSize,继续提交的任务被保存到阻塞队列中,等待被执行
maximumPoolSize: 线程池中允许的最大线程数。如果当前阻塞队列满了,且继续提交任务,则创建新的线程执行任务,前提是当前线程数小于maximumPoolSize
keepAliveTime:线程空闲时的存活时间,即当线程没有任务执行时,继续存活的时间

Spring Task

先上代码
任务执行类

@Component
public class SpringTaskDemo {

    @Scheduled(cron = "0/5 * * * * *")
    public void scheduledTest() {
        System.out.println("Scheduled: "+System.currentTimeMillis());
    }

   //固定速率
    @Scheduled(fixedRate = 5000)
    public void fixedRate() {
        System.out.println("fixedRate: "+System.currentTimeMillis());
    }
    //延迟速率
    @Scheduled(fixedDelay = 5000)
    public void fixedDelay() {
        System.out.println("fixedDelay: "+System.currentTimeMillis());
    }

}

在线cron生成:http://qqe2.com/cron/index
在启动类上加上@EnableScheduling,并且运行
而这里的任务是同步执行的,如果某个任务延迟比较严重,会阻塞当前线程,所以一般采取异步方式执行,即通过线程池方式去执行任务

@Configuration
@EnableAsync
public class AsyncConfig {

    private int corePoolSize = 10;
    private int maxPoolSize = 50;
    private int queueCapacity = 10;
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }

}

新增如上配置类,同时启动类加上@Async注解

Quartz

笔者工作中遇到任务量少,复杂性低的时候,采用了第二种与第三种方式,如用ScheduledExecutorService去异步发送邮件和执行非关键路径业务,用Spring Task去做一些简单的任务,如某张表的置顶过期时间到了定时处理,定时处理一些日志数据等。而这些之前都是耦合在主体业务服务包中。而如果有很多不同类型的业务数据需要从其他系统远程拉取,显然使用更高级的Quzrtz更符合场景(主要从精确度和功能 、任务类的数量、对异常的处理等角度对比)

设计job表

原则上应该有两张表,sys_token(用来保存token),sys_job(记录每隔任务配置信息),客户端通过OAuth2 密码模式获取授权和返回access_token保存在sys_token表中,job中有token的更新和业务job,这里简化业务只要sys_job表和利用restTemple拉取文章数据(后面博文将介绍和实战服务端推送与客户端集成xxl-job拉取)

DROP TABLE IF EXISTS `sys_job_detail`;
CREATE TABLE `sys_job_detail`  (
  `id` int(11) NOT NULL COMMENT '主键id',
  `job_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '定时任务名称',
  `job_group` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '定时任务组名',
  `job_class_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '定时任务类包位置',
  `job_cron` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '定时任务cron表达式',
  `job_type` int(11) NULL DEFAULT NULL COMMENT '定时任务启动/停止   0停止  1启动',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '定时任务创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

INSERT INTO `sys_job_detail` VALUES (1, 'test', 'group', 'com.slycmiaoxi.job.job.bpArticleJob', '0 47 21 * * ? *', 1, NULL);

SET FOREIGN_KEY_CHECKS = 1;

其核心思想就是通过反射获取表中启动的任务记录,通过Scheduler操作该类

码上有戏

不多说,先上代码

定时任务模块图
如图所示:通过start模块为启动类,启动加载job模块下CronScheduleJob(从数据库读取记录和反射获取并添加相应job类),bpArticleJob为具体业务类,BaseJob为实现job接口的类,业务job只要实现该方法即可扩展

读取任务

@Component
@Slf4j
public class CronScheduleJob {
    @Autowired
    private ISysJobDetailService jobService;

    public void scheduleJobsRun() throws SchedulerException {
        List<SysJobDetail> dwTdJobDetails = jobService.findInitJob();
        dwTdJobDetails.forEach(dwTdJobDetail -> {
            try {
                jobService.addJob(dwTdJobDetail, false);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("初始化定时失败!{}", e.getMessage());
            }
        });
    }

}

最核心的就是就是addJob方法了

@Override
    public void addJob(SysJobDetail dwTdJobDetailModel, boolean isCheck) throws SchedulerException, ClassNotFoundException {
        scheduler.start();
        System.out.println(dwTdJobDetailModel.toString());
        JobDetail jobDetail = JobBuilder
                .newJob(ClassUtil.getJobClass(dwTdJobDetailModel.getJobClassName()))
                .withIdentity(dwTdJobDetailModel.getJobClassName(), dwTdJobDetailModel.getJobGroup())
                .build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
                .cronSchedule(dwTdJobDetailModel.getJobCron());
        CronTrigger cronTrigger = TriggerBuilder
                .newTrigger()
                .withIdentity(dwTdJobDetailModel.getJobClassName(), dwTdJobDetailModel.getJobGroup())
                .withSchedule(scheduleBuilder)
                .build();
        if (isCheck) {
            if (scheduler.getJobGroupNames().contains(dwTdJobDetailModel.getJobGroup()) ||
                    !jobDetailHandle(dwTdJobDetailModel.getJobClassName(),
                            dwTdJobDetailModel.getJobGroup(), "addJob")) {
                jobExistsException();
            }
        }
        scheduler.scheduleJob(jobDetail, cronTrigger);
        log.info("定时任务" + dwTdJobDetailModel.getJobName() + "开始");
    }
原理分析

在分析add方法之前,首先需要了解Quzrtz的几个核心对象

Scheduler   -- 核心调度器
Job         -- 任务
JobDetail   -- 任务描述
Trigger     -- 触发器

I 调度器 Scheduler 通过SchedulerFactory工厂模式创建,是实际执行任务调度的控制器
II Job位于org.quartz包下,任何一个基于Quartz都必须实现该接口,即execute()方法
III 触发器 Trigger 用于定义任务调度的时间规则,有SimpleTrigger,CronTrigger,DateIntervalTrigger等
IV jobdetail: 记录job的一些信息(如key,class等用于唯一定位一个job任务)

其相应关系如下图所示
在这里插入图片描述
springboot项目启动时会执行实现了CommandLineRunner接口的类StartupRunner,然后从数据库读取到 List《SysJobDetail》,然后依次遍历,启动scheduler,初始化JobDetail和Trigger,通过scheduler.scheduleJob(jobDetail, cronTrigger)唤起job
,而其中通过jobKey区分每一个任务

jobDetail

JobDetail jobDetail = JobBuilder
                .newJob(ClassUtil.getJobClass(dwTdJobDetailModel.getJobClassName()))
                .withIdentity(dwTdJobDetailModel.getJobClassName(), dwTdJobDetailModel.getJobGroup())
                .build();

通过build模式,将反射后的类初始化给jobDetail的Jobclass,同时classname与group组合保证唯一key赋给jobKey

CronTrigger

CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
                .cronSchedule(dwTdJobDetailModel.getJobCron());
        CronTrigger cronTrigger = TriggerBuilder
                .newTrigger()
                .withIdentity(dwTdJobDetailModel.getJobClassName(), dwTdJobDetailModel.getJobGroup())
                .withSchedule(scheduleBuilder)
                .build();

同样方式保证CronTrigger的key唯一,
最后唤起相应线程 scheduler.scheduleJob(jobDetail, cronTrigger);

简单说明

I 可在bpArticleJob中写自己的业务方法,或者如果需要扩展多个业务,只需表中配置下和新增一个xxxjob即可

II 通过Scheduler提供的api操作job进行任务的停止,暂停和重启等

源码地址

代码地址

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值