wuzeyu
2024-07-24 42beb426f466d79c55668c4d854e52613febb0da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.dy.common.schedulerTask;
 
import java.util.Properties;
 
import org.apache.logging.log4j.*;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
 
public class SchedulerTaskFactory {
    
    private static Logger log = LogManager.getLogger(SchedulerTaskFactory.class.getName()) ;
    
    private static Scheduler scheduler ;
    
    private static Properties pro ;
    
    static{
        pro = new Properties() ;
        //以下属性从quartz-all-2.1.7.jar得到,而且下面所有项目都要配置
        pro.put("org.quartz.scheduler.instanceName", "DefaultQuartzScheduler");
        pro.put("org.quartz.scheduler.rmi.export", "false");
        pro.put("org.quartz.scheduler.rmi.proxy", "false");
        pro.put("org.quartz.scheduler.wrapJobExecutionInUserTransaction", "false");
        pro.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        pro.put("org.quartz.threadPool.threadCount", "10");
        pro.put("org.quartz.threadPool.threadPriority", "5");
        pro.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
        pro.put("org.quartz.jobStore.misfireThreshold", "60000");
        pro.put("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");                
    }
 
    /**
     * 得到调度器唯一实例
     * @return
     */
    public static Scheduler getSingleScheduler(){
        if(scheduler == null){
            try {
                scheduler = new StdSchedulerFactory(pro).getScheduler();
            } catch (SchedulerException e) {
                log.error(e) ;
            }
        }
        return scheduler ;
    }
    
 
    /**
     * 得到调度器唯一实例
     * @param threadPoolMaxCount
     * @param threadPoolPriority
     * @return
     */
    public static Scheduler getSingleScheduler(Integer threadPoolMaxCount, Integer threadPoolPriority){
        if(scheduler == null){
            try {
                if(threadPoolMaxCount != null && threadPoolMaxCount.intValue() >= 0){
                    pro.put("org.quartz.threadPool.threadCount", "" + (threadPoolMaxCount==null?10:(threadPoolMaxCount<=0?10:threadPoolMaxCount)));
                }
                if(threadPoolPriority != null && threadPoolPriority.intValue() >= 0){
                    pro.put("org.quartz.threadPool.threadPriority", "" + (threadPoolPriority==null?5:(threadPoolPriority<=0?5:threadPoolPriority)));
                }
                scheduler = new StdSchedulerFactory(pro).getScheduler();
            } catch (SchedulerException e) {
                log.error(e) ;
            }
        }
        return scheduler ;
    }
 
    /**
     * 关闭调度器
     * @throws SchedulerException 
     */
    public static void shutdownScheduler() throws SchedulerException{
        scheduler.shutdown() ;
    }
 
}