• Quartz的线程池解析


    【org.quartz.core相关类图】

    可以看到核心类为QuartzScheduler

    【QuartzScheduler构造函数】

    复制代码
    public QuartzScheduler(QuartzSchedulerResources resources, long idleWaitTime, @Deprecated long dbRetryInterval)
        throws SchedulerException {
        this.resources = resources;
        if (resources.getJobStore() instanceof JobListener) {
            addInternalJobListener((JobListener)resources.getJobStore());
        }
    
    </span><span style="color: #ff0000;">this.schedThread = new QuartzSchedulerThread(this, resources);
    ThreadExecutor schedThreadExecutor = resources.getThreadExecutor();
    schedThreadExecutor.execute(this.schedThread);
    </span><span style="color: #0000ff;">if</span> (idleWaitTime &gt; 0<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.schedThread.setIdleWaitTime(idleWaitTime);
    }
    
    jobMgr </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ExecutingJobsManager();
    addInternalJobListener(jobMgr);
    errLogger </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ErrorLogger();
    addInternalSchedulerListener(errLogger);
    
    signaler </span>= <span style="color: #0000ff;">new</span> SchedulerSignalerImpl(<span style="color: #0000ff;">this</span>, <span style="color: #0000ff;">this</span><span style="color: #000000;">.schedThread);
    
    </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(shouldRunUpdateCheck()) 
        updateTimer </span>=<span style="color: #000000;"> scheduleUpdateCheck();
    </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        updateTimer </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    getLog().info(</span>"Quartz Scheduler v." + getVersion() + " created."<span style="color: #000000;">);
    

    }

    复制代码

     这里创建了一个QuartzSchedulerThread并在ThreadExecutor(默认DefaultThreadExecutor )中运行。这里的ThreadExecutor并非我们关心的,继续看QuartzSchedulerThread的run方法。

    【QuartzSchedulerThread#run()】

    run方法比较长,大意就是根据Trigger找出要运行的job然后在线程池中执行。下面是run方法的代码片段:

    复制代码
    JobRunShell shell = null;
    try {
        shell = qsRsrcs.getJobRunShellFactory().createJobRunShell(bndle);
        shell.initialize(qs);
    } catch (SchedulerException se) {
        qsRsrcs.getJobStore().triggeredJobComplete(triggers.get(i), bndle.getJobDetail(), CompletedExecutionInstruction.SET_ALL_JOB_TRIGGERS_ERROR);
        continue;
    }
    

    if (qsRsrcs.getThreadPool().runInThread(shell) == false) {
    // this case should never happen, as it is indicative of the
    // scheduler being shutdown or a bug in the thread pool or
    // a thread pool being used concurrently - which the docs
    // say not to do...
    getLog().error("ThreadPool.runInThread() return false!");
    qsRsrcs.getJobStore().triggeredJobComplete(triggers.get(i), bndle.getJobDetail(), CompletedExecutionInstruction.SET_ALL_JOB_TRIGGERS_ERROR);
    }

    复制代码

    这里的ThreadPool必须实现org.quartz.spi.ThreadPool接口。

    【ThreadPool实现类】

    ThreadPool的默认实现类是org.quartz.simpl.SimpleThreadPool。

    String tpClass = cfg.getStringProperty(PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());

    (from org.quartz.impl.StdSchedulerFactory#instantiate())

    这个线程池非常简单,都没有BlockingQueue:

    private LinkedList<WorkerThread> availWorkers = new LinkedList<WorkerThread>(); // SimpleThreadPool成员
    private LinkedList<WorkerThread> busyWorkers = new LinkedList<WorkerThread>(); // SimpleThreadPool成员

     

    如果与Spring集成,使用org.springframework.scheduling.quartz.SchedulerFactoryBean且配置了taskExecutor(java.util.concurrent.Executor的实现类),则会使用org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool。

    if (this.taskExecutor != null) {
        mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
                LocalTaskExecutorThreadPool.class.getName());
    }

    (from org.springframework.scheduling.quartz.SchedulerFactoryBean#initSchedulerFactory)

    也可以自定义实现类,并为quartzProperties配置org.quartz.threadPool.class参数。

    原文地址:https://www.cnblogs.com/chanedi/p/4169510.html
  • 相关阅读:
    异步解决方案
    踩过的坑:InteliIJ IDEA 打开的项目突然左侧目录结构消失了,如何处理?
    了解Katalon的安装及基本使用(for mac)
    GitLab如何创建分支及拉取代码
    mocha测试接口类型及测试报告收集
    linux下sourcetree回退已推送的代码
    nodejs开篇基础<①>
    nodejs运行的两种方式<小记>
    Django视图函数之FBV与CBV模式
    Django之views.py视图函数学习
  • 原文地址:https://www.cnblogs.com/jpfss/p/10856792.html
Copyright © 2020-2023  润新知