首页 > 代码库 > Schedule(1)

Schedule(1)

创建Scheduler一般都是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package scheduler;
import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class SchedulerTest {
    @Test
    public void test1(){
        try {
            Scheduler sc=new StdSchedulerFactory().getDefaultScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

这种情况下使用的是默认调度器,在quartz.properties中已经把属性全部设置好了,看代码

1
2
3
4
5
6
7
8
9
10
11
12
13
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
 
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10//Thread数量
org.quartz.threadPool.threadPriority: 5//Thread优先级
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
 
org.quartz.jobStore.misfireThreshold: 60000
 //将schedule相关信息保存在ram中,轻量级速度快,遗憾的是重启应用时会丢失
org.quartz.jobStore.class: org.quartz.simpl.RAMJobSto

我们可以自己配置相关的信息,比如thread的数量

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package scheduler;
import java.util.Properties;
//两个quartz包不可以少
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class SchedulerTest {
    private static SchedulerFactory scf=new StdSchedulerFactory();
    static{
        Properties pr=new Properties();
        //将thread数量设置为50
        pr.put("org.quartz.threadPool.threadCount", "50");
        try {
            ((StdSchedulerFactory) scf).initialize(pr);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
 
    }
}

运行之后,会发现已经thread数量已经变化

1
2
3
4
5
6
Scheduler class: ‘org.quartz.core.QuartzScheduler‘ - running locally.
 NOT STARTED.
 Currently in standby mode.
 Number of jobs executed: 0
 Using thread pool ‘org.quartz.simpl.SimpleThreadPool‘ - with 50 threads.
 Using job-store ‘org.quartz.simpl.RAMJobStore‘ - which does not support persistence. and is not clustered.

Schedule(1)