hjg
2023-11-17 3780c5e65b05bf23020810798babc6d20311fa79
提交 | 用户 | 时间
58d006 1 package com.mandi.quartz;
A 2
3 import java.util.Date;
4 import java.util.Map;
5 import java.util.Map.Entry;
6
7 import org.quartz.Job;
8 import org.quartz.JobBuilder;
9 import org.quartz.JobDataMap;
10 import org.quartz.JobDetail;
11 import org.quartz.Scheduler;
12 import org.quartz.SchedulerException;
13 import org.quartz.Trigger;
14
15
16 import org.quartz.SchedulerFactory;
17 import org.quartz.Trigger.TriggerState;
18 import org.quartz.TriggerBuilder;
19 import org.quartz.TriggerKey;
20 import org.quartz.impl.StdSchedulerFactory;
21
22 /** 
23  * @author mengly 
24  * @version 创建时间:2015年12月22日 下午2:52:02 
25  * 类说明 :简易启动quartz job。
26  */
27
28 public class Quartzjob {
29     private  static SchedulerFactory sf=null;//new StdSchedulerFactory();
30
31     static{
32         Quartzjob.sf=new StdSchedulerFactory();
33     }
34     public static SchedulerFactory instance(){
35         return Quartzjob.sf;
36     }
37     /**
38      * 
39      * @param jobName
40      * @param clz
41      * @param trigger 
42      * @author mengly 
43      * @version 创建时间:2016年3月1日 下午6:33:42
44      */
45     public static  void runJob(String jobName, Class<? extends Job> clz,Trigger trigger ) {  
46             try {  
47                 Scheduler sched = sf.getScheduler();  
48                 JobBuilder jb= JobBuilder.newJob(clz);
49                 jb=jb.withIdentity(jobName, jobName+"group");
50                 JobDetail jobDetail =jb.build();
51                 
52                 sched.scheduleJob(jobDetail, trigger);  
53                 // 启动  
54                 if (!sched.isShutdown()) {  
55                     sched.start();  
56                 }  
57             } catch (Exception e) {  
58                 e.printStackTrace();
59             }  
60         } 
61      
62         public static  void runJob(String jobName, Class<? extends Job> clz,Trigger trigger,Map<String, Object> params ) {  
63                 try {  
64                     Scheduler sched = sf.getScheduler();  
65                     JobBuilder jb= JobBuilder.newJob(clz);
66                     jb=jb.withIdentity(jobName, jobName+"group");
67                     JobDetail jobDetail =jb.build();
68                     JobDataMap jdm=jobDetail.getJobDataMap();
69                     for (Entry<String, Object> en : params.entrySet()) {
70                         jdm.put(en.getKey(), en.getValue());
71                     }
72                     sched.scheduleJob(jobDetail, trigger);  
73                     // 启动  
74                     if (!sched.isShutdown()) { 
75                         sched.start();  
76                     }  
77                 } catch (Exception e) {  
78                     e.printStackTrace();
79                 }  
80             } 
81      
82      
83         public static  void runJobNowOnce(String jobName, Class<? extends Job> clz) {  
84                 try {  
85                     Scheduler sched = sf.getScheduler();  
86                     JobBuilder jb= JobBuilder.newJob(clz);
87                     jb=jb.withIdentity(jobName, jobName+"group");
88                     JobDetail jobDetail =jb.build();
89                     
90                     @SuppressWarnings("rawtypes")
91                     TriggerBuilder tb=TriggerBuilder.newTrigger();
92                     tb.withIdentity(jobName+"trigger", jobName+"triggergroup").startNow().build();
93                     Trigger tr=tb.build();
94                     sched.scheduleJob(jobDetail, tr);  
95                     System.out.println("start to do...runjobnowonce");
96                     // 启动  
97                     if (!sched.isShutdown()) {  
98                         sched.start();  
99                     }  
100                 } catch (Exception e) {  
101                     e.printStackTrace();
102                 }  
103             } 
104         public static  void runJobNowOnce(String jobName, Class<? extends Job> clz,Map<String, Object> params) {  
105             try {  
106                 Scheduler sched = sf.getScheduler();  
107                 JobBuilder jb= JobBuilder.newJob(clz);
108                 jb=jb.withIdentity(jobName, jobName+"group");
109                 JobDetail jobDetail =jb.build();
110                 if(params!=null&&!params.isEmpty())
111                 {
112                     jobDetail.getJobDataMap().putAll(params);
113                 }
114                 @SuppressWarnings("rawtypes")
115                 TriggerBuilder tb=TriggerBuilder.newTrigger();
116                 tb.withIdentity(jobName+"trigger", jobName+"triggergroup").startNow().build();
117                 Trigger tr=tb.build();
118                 sched.scheduleJob(jobDetail, tr);  
119                 System.out.println("start to do...runjobnowonce");
120                 // 启动  
121                 if (!sched.isShutdown()) {  
122                     sched.start();  
123                 }  
124             } catch (Exception e) {  
125                 e.printStackTrace();
126             }  
127         } 
128         
129         
130         public static TriggerState getJobState(String triggername,String triggergroup){
131             try {
132                 Scheduler sched = sf.getScheduler();
133                 TriggerKey tk=TriggerKey.triggerKey(triggername, triggergroup);
134                 return sched.getTriggerState(tk);
135             } catch (SchedulerException e) {
136                 e.printStackTrace();
137             }  
138             return null;
139         }
140         
141         public static  void runJobOnce(String jobName, Class<? extends Job> clz,Date startDate) {  
142             try {  
143                 Scheduler sched = sf.getScheduler();  
144                 JobBuilder jb= JobBuilder.newJob(clz);
145                 jb=jb.withIdentity(jobName, jobName);
146                 JobDetail jobDetail =jb.build();
147                 TriggerBuilder tb=TriggerBuilder.newTrigger();
148                 tb.withIdentity(jobName+"trigger", jobName+"triggergroup").startAt(startDate).build();
149                 Trigger tr=tb.build();
150                 sched.scheduleJob(jobDetail, tr);  
151                 System.out.println("start to do...runjobnowonce");
152                 // 启动  
153                 if (!sched.isShutdown()) {  
154                     sched.start();  
155                 }  
156             } catch (Exception e) {  
157                 e.printStackTrace();
158             }  
159         } 
160 }