Administrator
2023-02-15 ffffbd25e21205601cb9ae60e93ebb2e31b9607e
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.mandi.quartz;
 
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
 
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
 
 
import org.quartz.SchedulerFactory;
import org.quartz.Trigger.TriggerState;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
 
/** 
 * @author mengly 
 * @version 创建时间:2015年12月22日 下午2:52:02 
 * 类说明 :简易启动quartz job。
 */
 
public class Quartzjob {
    private  static SchedulerFactory sf=null;//new StdSchedulerFactory();
 
    static{
        Quartzjob.sf=new StdSchedulerFactory();
    }
    public static SchedulerFactory instance(){
        return Quartzjob.sf;
    }
    /**
     * 
     * @param jobName
     * @param clz
     * @param trigger 
     * @author mengly 
     * @version 创建时间:2016年3月1日 下午6:33:42
     */
    public static  void runJob(String jobName, Class<? extends Job> clz,Trigger trigger ) {  
            try {  
                Scheduler sched = sf.getScheduler();  
                JobBuilder jb= JobBuilder.newJob(clz);
                jb=jb.withIdentity(jobName, jobName+"group");
                JobDetail jobDetail =jb.build();
                
                sched.scheduleJob(jobDetail, trigger);  
                // 启动  
                if (!sched.isShutdown()) {  
                    sched.start();  
                }  
            } catch (Exception e) {  
                e.printStackTrace();
            }  
        } 
     
        public static  void runJob(String jobName, Class<? extends Job> clz,Trigger trigger,Map<String, Object> params ) {  
                try {  
                    Scheduler sched = sf.getScheduler();  
                    JobBuilder jb= JobBuilder.newJob(clz);
                    jb=jb.withIdentity(jobName, jobName+"group");
                    JobDetail jobDetail =jb.build();
                    JobDataMap jdm=jobDetail.getJobDataMap();
                    for (Entry<String, Object> en : params.entrySet()) {
                        jdm.put(en.getKey(), en.getValue());
                    }
                    sched.scheduleJob(jobDetail, trigger);  
                    // 启动  
                    if (!sched.isShutdown()) { 
                        sched.start();  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();
                }  
            } 
     
     
        public static  void runJobNowOnce(String jobName, Class<? extends Job> clz) {  
                try {  
                    Scheduler sched = sf.getScheduler();  
                    JobBuilder jb= JobBuilder.newJob(clz);
                    jb=jb.withIdentity(jobName, jobName+"group");
                    JobDetail jobDetail =jb.build();
                    
                    @SuppressWarnings("rawtypes")
                    TriggerBuilder tb=TriggerBuilder.newTrigger();
                    tb.withIdentity(jobName+"trigger", jobName+"triggergroup").startNow().build();
                    Trigger tr=tb.build();
                    sched.scheduleJob(jobDetail, tr);  
                    System.out.println("start to do...runjobnowonce");
                    // 启动  
                    if (!sched.isShutdown()) {  
                        sched.start();  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();
                }  
            } 
        public static  void runJobNowOnce(String jobName, Class<? extends Job> clz,Map<String, Object> params) {  
            try {  
                Scheduler sched = sf.getScheduler();  
                JobBuilder jb= JobBuilder.newJob(clz);
                jb=jb.withIdentity(jobName, jobName+"group");
                JobDetail jobDetail =jb.build();
                if(params!=null&&!params.isEmpty())
                {
                    jobDetail.getJobDataMap().putAll(params);
                }
                @SuppressWarnings("rawtypes")
                TriggerBuilder tb=TriggerBuilder.newTrigger();
                tb.withIdentity(jobName+"trigger", jobName+"triggergroup").startNow().build();
                Trigger tr=tb.build();
                sched.scheduleJob(jobDetail, tr);  
                System.out.println("start to do...runjobnowonce");
                // 启动  
                if (!sched.isShutdown()) {  
                    sched.start();  
                }  
            } catch (Exception e) {  
                e.printStackTrace();
            }  
        } 
        
        
        public static TriggerState getJobState(String triggername,String triggergroup){
            try {
                Scheduler sched = sf.getScheduler();
                TriggerKey tk=TriggerKey.triggerKey(triggername, triggergroup);
                return sched.getTriggerState(tk);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }  
            return null;
        }
        
        public static  void runJobOnce(String jobName, Class<? extends Job> clz,Date startDate) {  
            try {  
                Scheduler sched = sf.getScheduler();  
                JobBuilder jb= JobBuilder.newJob(clz);
                jb=jb.withIdentity(jobName, jobName);
                JobDetail jobDetail =jb.build();
                TriggerBuilder tb=TriggerBuilder.newTrigger();
                tb.withIdentity(jobName+"trigger", jobName+"triggergroup").startAt(startDate).build();
                Trigger tr=tb.build();
                sched.scheduleJob(jobDetail, tr);  
                System.out.println("start to do...runjobnowonce");
                // 启动  
                if (!sched.isShutdown()) {  
                    sched.start();  
                }  
            } catch (Exception e) {  
                e.printStackTrace();
            }  
        } 
}