任务调用类:
1 package business.tools.service; 2 3 import java.util.ArrayList; 4 import java.util.Calendar; 5 import java.util.Date; 6 import java.util.List; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.quartz.CronTrigger; 11 import org.quartz.Job; 12 import org.quartz.JobDetail; 13 import org.quartz.Scheduler; 14 import org.quartz.SchedulerException; 15 import org.quartz.SchedulerFactory; 16 import org.quartz.impl.StdSchedulerFactory; 17 18 import business.tools.model.SalesDataMonitor; 19 import framework.corenew.DateFormatException; 20 import framework.corenew.MyString; 21 import framework.corenew.QDateTime; 22 import framework.corenew.UniversalManager; 23 24 public class SalesDataMonitorManager extends UniversalManager{ 25 private transient final static Log log = LogFactory.getLog(SalesDataMonitorManager.class); 26 private static SchedulerFactory sf ; 27 public static synchronized SchedulerFactory getSf() { 28 if(sf==null){ 29 sf = new StdSchedulerFactory(); 30 } 31 return sf; 32 } 33 34 35 /** 36 * 保存销售数据范围时设定自动任务 37 * @param salesDataMonitor 38 * @throws Exception 39 */ 40 public void saveSalesMonitorJob(SalesDataMonitor salesDataMonitor) throws Exception{ 41 if(salesDataMonitor==null){ 42 return; 43 } 44 if("1".equals(salesDataMonitor.getType())){//如果type==1的不是监控范围数据 45 return; 46 } 47 48 List<String> formartTimes = getFormatTime(salesDataMonitor);//对象中包含的任务时间 49 if(formartTimes==null||formartTimes.size()==0){ 50 return; 51 } 52 53 removeJobs(salesDataMonitor.getId(), "salesDataMonitorGroup");//删除任务 54 55 for(String formartTime:formartTimes){ 56 String jobName = getJobName(salesDataMonitor, formartTime); 57 Job job = (Job)Class.forName("business.webservice.SalesDataMonitorJob").newInstance(); 58 Scheduler sched = getSf().getScheduler(); 59 JobDetail jobDetail = new JobDetail(jobName, "salesDataMonitorGroup", job.getClass()); 60 jobDetail.getJobDataMap().put("salesDataMonitorId", salesDataMonitor.getId()); 61 //触发器 62 CronTrigger trigger = new CronTrigger(jobName, "salesDataMonitorGroup"); 63 trigger.setCronExpression(formartTime);//触发器时间设定 64 sched.scheduleJob(jobDetail,trigger); 65 //启动 66 if(!sched.isShutdown()) 67 sched.start(); 68 69 log.info("===============================任务添加成功!"); 70 } 71 72 } 73 74 /** 75 * 拼出任务执行时间 76 * @param startTime 77 * @param endTime 78 * @param interval 79 * @return 80 * @throws DateFormatException 81 */ 82 private List<String> getFormatTime(SalesDataMonitor salesDataMonitor) throws Exception{ 83 List<String> strList = new ArrayList<String>(); 84 85 String catchDataType = salesDataMonitor.getCatchDataType();//抓取类型 86 if("1".equals(catchDataType)){ 87 String weekDayStr = "";//周几的字符串 88 String weekDays = salesDataMonitor.getWeekDays(); 89 if(MyString.isNoEmpty(weekDays)){ 90 String[] weekDaysArr = weekDays.split("~"); 91 for(int i=0;i<weekDaysArr.length;i++){ 92 if(MyString.isNoEmpty(weekDaysArr[i])){ 93 weekDayStr+=weekDaysArr[i]; 94 if(i<weekDaysArr.length-1){ 95 weekDayStr+=","; 96 } 97 } 98 99 } 100 } 101 102 String hoursAndMinutes = salesDataMonitor.getHoursAndMinutes(); 103 if(MyString.isNoEmpty(hoursAndMinutes)){ 104 String[] hoursAndMinutesArr = hoursAndMinutes.split("~"); 105 for(int i=0;i<hoursAndMinutesArr.length;i++){ 106 String[] hourAndMinuteArr = hoursAndMinutesArr[i].split(":"); 107 String hourStr = hourAndMinuteArr[0]; 108 String minuteStr = hourAndMinuteArr[1]; 109 110 StringBuffer formartTime = new StringBuffer(); 111 formartTime.append("0 "); 112 formartTime.append(minuteStr+" "); 113 formartTime.append(hourStr+" ? * "); 114 formartTime.append(weekDayStr);//周的表示 用MON-SUN或者1-7,不过注意:数字1是周日,2是周一,依次向下推 115 116 117 strList.add(formartTime.toString()); 118 119 } 120 } 121 }else if("2".equals(catchDataType)){ 122 try{ 123 String fixedTimes = salesDataMonitor.getFixedTimes(); 124 if(MyString.isNoEmpty(fixedTimes)){ 125 String[] fixedTimesArr = fixedTimes.split("~"); 126 for(int i=0;i<fixedTimesArr.length;i++){ 127 Date fixedTime = QDateTime.stringToDate(fixedTimesArr[i], "yyyy-MM-dd HH:mi"); 128 int compared = compareTimeToNow(fixedTime); 129 if(compared==1){ 130 throw new Exception("您填写的时间已过期,请核对!"); 131 } 132 String yearStr = QDateTime.dateToString(fixedTime, "yyyy"); 133 String monthStr = QDateTime.dateToString(fixedTime, "MM"); 134 String dateStr = QDateTime.dateToString(fixedTime, "dd"); 135 136 String hourStr = QDateTime.dateToString(fixedTime, "HH"); 137 String minStr = QDateTime.dateToString(fixedTime, "mi"); 138 139 StringBuffer formartTime = new StringBuffer(); 140 formartTime.append("0 ") ; 141 formartTime.append(minStr+" "); 142 formartTime.append(hourStr+" "); 143 formartTime.append(dateStr+" "); 144 formartTime.append(monthStr+" ? "); 145 formartTime.append(yearStr); 146 147 strList.add(formartTime.toString()); 148 149 } 150 } 151 }catch(Exception e){ 152 e.printStackTrace(); 153 } 154 } 155 156 log.info("自动任务触发时间:======"+strList.toString()); 157 return strList; 158 } 159 /** 160 * 一个日期和当前时间比较 161 * @param time 162 * @return 163 * @throws DateFormatException 164 */ 165 private int compareTimeToNow(Date time) throws DateFormatException{ 166 167 Calendar inTime = Calendar.getInstance(); 168 inTime.setTime(time); 169 170 Calendar nowTime = Calendar.getInstance(); 171 String currentTimeStr = QDateTime.dateToString(new Date(), "yyyy-MM-dd HH:mi"); 172 Date currentTime = QDateTime.stringToDate(currentTimeStr, "yyyy-MM-dd HH:mi"); 173 nowTime.setTime(currentTime); 174 175 int compared = nowTime.compareTo(inTime); 176 return compared; 177 } 178 179 180 private String getJobName(SalesDataMonitor salesDataMonitor,String formartTime) throws Exception{ 181 //拼出任务名字 182 StringBuffer jobName = new StringBuffer(); 183 jobName.append(salesDataMonitor.getId());//id 184 jobName.append("φ"); 185 jobName.append(salesDataMonitor.getBusinessType());//国际国内 186 jobName.append("φ"); 187 jobName.append(salesDataMonitor.getCarrierCode());//航空公司 188 jobName.append("φ"); 189 jobName.append(salesDataMonitor.getClasses());//舱位 190 jobName.append("φ"); 191 jobName.append(salesDataMonitor.getDepArea()+"-"+salesDataMonitor.getArrArea());//区域 192 jobName.append("φ"); 193 jobName.append(salesDataMonitor.getIsArrival());//是限定还是排除 194 jobName.append("φ"); 195 jobName.append(salesDataMonitor.getDeparture()+"-"+salesDataMonitor.getArrival());//限定和排除的机场三字码 196 jobName.append("φ"); 197 jobName.append(salesDataMonitor.getTourDateBegins()+"-"+salesDataMonitor.getTourDateEnds());//旅行时间 198 jobName.append("φ"); 199 jobName.append(salesDataMonitor.getEtdzBegins()+"-"+salesDataMonitor.getEtdzEnds());//出票时间 200 jobName.append("φ"); 201 jobName.append(salesDataMonitor.getCatchDataType());//抓取类型 202 jobName.append("φ"); 203 jobName.append(formartTime);//任务时间 204 205 return jobName.toString(); 206 } 207 208 public void removeJobs(String objectId,String groupName) throws SchedulerException{ 209 if(!MyString.isNoEmpty(objectId)){ 210 return; 211 } 212 Scheduler sched = getSf().getScheduler(); 213 String[] jobNames = sched.getJobNames(groupName); 214 if(jobNames==null||jobNames.length==0){ 215 return; 216 } 217 for(String jobName :jobNames){ 218 if(jobName.startsWith(objectId)){ 219 removeJob(jobName);//清除任务 220 } 221 } 222 223 224 } 225 /** 226 * 删除任务 227 * @param jobName 228 * @throws SchedulerException 229 */ 230 public static void removeJob(String jobName) 231 throws SchedulerException{ 232 Scheduler sched = getSf().getScheduler(); 233 sched.pauseTrigger(jobName,"salesDataMonitorGroup");//停止触发器 234 sched.unscheduleJob(jobName,"salesDataMonitorGroup");//移除触发器 235 sched.deleteJob(jobName,"salesDataMonitorGroup");//删除任务 236 log.info("删除任务成功!"); 237 } 238 }
任务类:Job
1 package business.webservice; 2 3 4 import java.util.Date; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.quartz.Job; 9 import org.quartz.JobExecutionContext; 10 import org.quartz.JobExecutionException; 11 12 import business.tools.service.BookinfoManager; 13 import framework.corenew.QDateTime; 14 import framework.corenew.SpringUtil; 15 16 public class SalesDataMonitorJob implements Job{ 17 private transient final Log log = LogFactory.getLog(SalesDataMonitorJob.class); 18 19 public void execute(JobExecutionContext jobEC) throws JobExecutionException { 20 String currentTime = QDateTime.dateToString(new Date(), "yyyy-MM-dd HH:mi:ss"); 21 log.info("===================调用SalesDataMonitorJob时间 :"+currentTime+"====================="); 22 23 String salesDataMonitorId = (String)jobEC.getJobDetail().getJobDataMap().get("salesDataMonitorId"); 24 25 try { 26 BookinfoManager bookinfoManager = (BookinfoManager) SpringUtil.getBean("bookinfoManager"); 27 bookinfoManager.saveGroupBookInfoBySalesDataMonitor(salesDataMonitorId); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 32 33 } 34 35 36 }
时间的格式拼法详见下章
时间: 2024-10-22 06:49:40