0,監控的意義javascript
(1)可以查看有多少定時任務,用的什麼執行策略,便於管理css
(2)可以經過界面操做中止或啓動某個定時任務,便於管理html
(3)可以經過界面操做改變某個定時任務的執行策略,便於管理java
1,pom.xml 文件添加jarjquery
1 <!-- quartz監控 --> 2 <dependency> 3 <groupId>org.quartz-scheduler</groupId> 4 <artifactId>quartz</artifactId> 5 <version>2.2.1</version> 6 </dependency> 7 <dependency> 8 <groupId>org.quartz-scheduler</groupId> 9 <artifactId>quartz-jobs</artifactId> 10 <version>2.2.1</version> 11 </dependency>
2,applicationContext.xml添加ajax
<bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
或者SpringBoot中spring
1 @Bean 2 public SchedulerFactoryBean schedulerFactoryBean() { 3 4 return new SchedulerFactoryBean(); 5 }
3,Taskcontroller層數據庫
1 /** 2 * @author aisino-xxy 3 * @deprecated 定時任務監控 4 */ 5 @Controller 6 @RequestMapping(produces = "text/plain;charset=utf-8") 7 public class TaskController extends BaseController{ 8 private static Logger log = Logger.getLogger("order_log"); 9 10 @Autowired 11 private TaskService taskService; 12 13 /** 14 * 查詢全部的定時任務 15 * @param request 16 * @return 17 */ 18 @RequestMapping("/task/taskList.do") 19 public ModelAndView taskList(HttpServletRequest request) { 20 log.info(this.getUser().getUsername() + ",進入定時任務監控頁面"); 21 22 List<Map<String, Object>> taskList = taskService.getAllJobs(); 23 ModelAndView view = new ModelAndView(); 24 view.setViewName("/Contents/task/taskList.jsp"); 25 view.addObject("taskList", taskList); 26 return view; 27 } 28 29 30 /** 31 * 添加一個定時任務 32 * @param scheduleJob 33 * @return retObj 34 */ 35 @RequestMapping("/task/add.do") 36 @ResponseBody 37 public String addTask(HttpServletRequest request , ScheduleJob scheduleJob) { 38 RetObj retObj = new RetObj(); 39 retObj.setFlag(false); 40 try { 41 CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); 42 } catch (Exception e) { 43 retObj.setMsg("cron表達式有誤,不能被解析!"); 44 return JSON.toJSONString(retObj); 45 } 46 47 Object obj = null; 48 try { 49 if (StringUtils.isNotBlank(scheduleJob.getSpringId())) { 50 obj = SpringUtils.getBean(scheduleJob.getSpringId()); 51 } else { 52 Class clazz = Class.forName(scheduleJob.getBeanClass()); 53 obj = clazz.newInstance(); 54 } 55 } catch (Exception e) { 56 // do nothing......... 57 } 58 if (obj == null) { 59 retObj.setMsg("未找到目標類!"); 60 return JSON.toJSONString(retObj); 61 } else { 62 Class clazz = obj.getClass(); 63 Method method = null; 64 try { 65 method = clazz.getMethod(scheduleJob.getMethodName(), null); 66 } catch (Exception e) { 67 // do nothing..... 68 } 69 if (method == null) { 70 retObj.setMsg("未找到目標方法!"); 71 return JSON.toJSONString(retObj); 72 } 73 } 74 75 76 try { 77 taskService.addTask(scheduleJob); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 retObj.setFlag(false); 81 retObj.setMsg("保存失敗,檢查 name group 組合是否有重複!"); 82 return JSON.toJSONString(retObj); 83 } 84 85 retObj.setFlag(true); 86 return JSON.toJSONString(retObj); 87 } 88 89 90 91 /** 92 * 開啓/關閉一個定時任務 93 * @param request 94 * @param jobId 95 * @param cmd 96 * @return 97 */ 98 @RequestMapping("/task/changeJobStatus.do") 99 @ResponseBody 100 public String changeJobStatus(HttpServletRequest request, Long jobId, String cmd) { 101 RetObj retObj = new RetObj(); 102 retObj.setFlag(false); 103 try { 104 taskService.changeStatus(jobId, cmd); 105 } catch (Exception e) { 106 log.error(e.getMessage(), e); 107 retObj.setMsg("任務狀態改變失敗!"); 108 return JSON.toJSONString(retObj); 109 } 110 retObj.setFlag(true); 111 return JSON.toJSONString(retObj); 112 } 113 114 115 116 /** 117 * 修改定時任務的執行時間間隔 118 * @param request 119 * @param jobId 120 * @param cron 121 * @return 122 */ 123 @RequestMapping("/task/updateCron.do") 124 @ResponseBody 125 public String updateCron(HttpServletRequest request, Long jobId, String cron) { 126 RetObj retObj = new RetObj(); 127 retObj.setFlag(false); 128 try { 129 CronScheduleBuilder.cronSchedule(cron); 130 } catch (Exception e) { 131 retObj.setMsg("cron表達式有誤,不能被解析!"); 132 return JSON.toJSONString(retObj); 133 } 134 try { 135 taskService.updateCron(jobId, cron); 136 } catch (SchedulerException e) { 137 retObj.setMsg("cron更新失敗!"); 138 return JSON.toJSONString(retObj); 139 } 140 retObj.setFlag(true); 141 return JSON.toJSONString(retObj); 142 } 143 }
4,TaskServiceImplexpress
1 /** 2 * @author aisino-xxy 3 * 4 */ 5 @Service("taskService") 6 @Transactional(rollbackFor=Exception.class) 7 public class TaskServiceImpl implements TaskService{ 8 private static Logger log = Logger.getLogger("order_log"); 9 10 @Autowired 11 private SchedulerFactoryBean schedulerFactoryBean; 12 13 @Autowired 14 private TaskDao taskDao; 15 16 /** 17 * 查詢全部的定時任務 18 */ 19 @Override 20 public List<Map<String, Object>> getAllJobs() { 21 return taskDao.getAllJobs(); 22 } 23 24 /** 25 * 添加一個定時任務 26 */ 27 @Override 28 public void addTask(ScheduleJob job) { 29 job.setCreateTime(new Date()); 30 taskDao.addTask(job); 31 } 32 33 /** 34 * 更改任務狀態 35 * 36 * @throws SchedulerException 37 */ 38 @Override 39 public void changeStatus(Long jobId, String cmd) throws SchedulerException { 40 ScheduleJob job = getTaskById(jobId); 41 if (job == null) { 42 return; 43 } 44 if ("stop".equals(cmd)) { 45 deleteJob(job); 46 job.setJobStatus(ScheduleJob.STATUS_NOT_RUNNING); 47 } else if ("start".equals(cmd)) { 48 job.setJobStatus(ScheduleJob.STATUS_RUNNING); 49 addJob(job); 50 } 51 taskDao.updateJobStatusById(jobId,job.getJobStatus()); 52 } 53 54 55 /** 56 * 從數據庫中查詢job 57 */ 58 public ScheduleJob getTaskById(Long jobId) { 59 Map<String, Object> job = taskDao.getJobById(jobId); 60 return ScheduleJob.transMap2Bean(job); 61 } 62 63 64 /** 65 * 更改任務 cron表達式 66 * 67 * @throws SchedulerException 68 */ 69 @Override 70 public void updateCron(Long jobId, String cron) throws SchedulerException { 71 ScheduleJob job = getTaskById(jobId); 72 if (job == null) { 73 return; 74 } 75 job.setCronExpression(cron); 76 if (ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) { 77 updateJobCron(job); 78 } 79 taskDao.updateJobCronById(jobId, cron); 80 } 81 82 /** 83 * 添加任務 84 * 85 * @param scheduleJob 86 * @throws SchedulerException 87 */ 88 public void addJob(ScheduleJob job) throws SchedulerException { 89 if (job == null || !ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) { 90 return; 91 } 92 93 Scheduler scheduler = schedulerFactoryBean.getScheduler(); 94 log.debug(scheduler + "....................add.............."); 95 96 TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); 97 CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); 98 99 // 不存在,建立一個 100 if (null == trigger) { 101 Class clazz = ScheduleJob.CONCURRENT_IS.equals(job.getIsConcurrent()) ? QuartzJobFactory.class : QuartzJobFactoryDisallowConcurrentExecution.class; 102 JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build(); 103 jobDetail.getJobDataMap().put("scheduleJob", job); 104 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); 105 trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build(); 106 scheduler.scheduleJob(jobDetail, trigger); 107 } else { 108 // Trigger已存在,那麼更新相應的定時設置 109 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); 110 // 按新的cronExpression表達式從新構建trigger 111 trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); 112 // 按新的trigger從新設置job執行 113 scheduler.rescheduleJob(triggerKey, trigger); 114 } 115 } 116 117 118 119 /** 120 * 刪除一個job 121 * 122 * @param scheduleJob 123 * @throws SchedulerException 124 */ 125 public void deleteJob(ScheduleJob scheduleJob) throws SchedulerException { 126 Scheduler scheduler = schedulerFactoryBean.getScheduler(); 127 JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 128 scheduler.deleteJob(jobKey); 129 } 130 131 132 /** 133 * 當即執行job 134 * 135 * @param scheduleJob 136 * @throws SchedulerException 137 */ 138 public void runAJobNow(ScheduleJob scheduleJob) throws SchedulerException { 139 Scheduler scheduler = schedulerFactoryBean.getScheduler(); 140 JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 141 scheduler.triggerJob(jobKey); 142 } 143 144 /** 145 * 更新job時間表達式 146 * 147 * @param scheduleJob 148 * @throws SchedulerException 149 */ 150 public void updateJobCron(ScheduleJob scheduleJob) throws SchedulerException { 151 Scheduler scheduler = schedulerFactoryBean.getScheduler(); 152 TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup()); 153 CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); 154 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()); 155 trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); 156 scheduler.rescheduleJob(triggerKey, trigger); 157 } 158 159 @PostConstruct 160 public void init() throws Exception { 161 //Scheduler scheduler = schedulerFactoryBean.getScheduler(); 162 // 這裏獲取任務信息數據 163 List<Map<String, Object>> jobList = taskDao.getAllJobs(); 164 165 for (Map<String, Object> map : jobList) { 166 ScheduleJob job = ScheduleJob.transMap2Bean(map); 167 addJob(job); 168 } 169 } 170 }
5,model app
RetObj.java
1 /** 2 * @author aisino-xxy 3 * 返回值 4 */ 5 public class RetObj { 6 private boolean flag = true; 7 private String msg; 8 private Object obj; 9 10 set…… 11 get…… 12 }
ScheduleJob.java
1 /** 2 * @author aisino-xxy 3 * @Description: 計劃任務信息 4 */ 5 public class ScheduleJob { 6 public static final String STATUS_RUNNING = "1"; 7 public static final String STATUS_NOT_RUNNING = "0"; 8 public static final String CONCURRENT_IS = "1"; 9 public static final String CONCURRENT_NOT = "0"; 10 private Long jobId; 11 12 private Date createTime; 13 14 private Date updateTime; 15 /** 16 * 任務名稱 17 */ 18 private String jobName; 19 /** 20 * 任務分組 21 */ 22 private String jobGroup; 23 /** 24 * 任務狀態 是否啓動任務 25 */ 26 private String jobStatus; 27 /** 28 * cron表達式 29 */ 30 private String cronExpression; 31 /** 32 * 描述 33 */ 34 private String description; 35 /** 36 * 任務執行時調用哪一個類的方法 包名+類名 37 */ 38 private String beanClass; 39 /** 40 * 任務是否有狀態 41 */ 42 private String isConcurrent; 43 /** 44 * spring bean 45 */ 46 private String springId; 47 /** 48 * 任務調用的方法名 49 */ 50 private String methodName; 51 52 set…… 53 get…… 54 55 56 public static ScheduleJob transMap2Bean(Map<String, Object> map){ 57 ScheduleJob job = new ScheduleJob(); 58 try { 59 job.setJobId((Long) map.get("job_id")); 60 job.setJobName((String) map.get("job_name")); 61 job.setJobGroup((String) map.get("job_group")); 62 job.setJobStatus((String) map.get("job_status")); 63 job.setCronExpression((String) map.get("cron_expression")); 64 job.setDescription((String) map.get("description")); 65 job.setBeanClass((String) map.get("bean_class")); 66 job.setIsConcurrent((String) map.get("is_concurrent")); 67 job.setSpringId((String) map.get("spring_id")); 68 job.setMethodName((String) map.get("method_name")); 69 //job.setCreateTime(new Date((String) map.get("create_time"))); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 return null; 73 } 74 return job; 75 } 76 }
6,utils
QuartzJobFactory.java
1 /** 2 * 3 * @Description: 計劃任務執行處 無狀態 4 */ 5 public class QuartzJobFactory implements Job { 6 public final Logger log = Logger.getLogger(this.getClass()); 7 8 public void execute(JobExecutionContext context) throws JobExecutionException { 9 ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob"); 10 TaskUtils.invokMethod(scheduleJob); 11 } 12 }
QuartzJobFactoryDisallowConcurrentExecution.java
1 /** 2 * 3 * @Description: 若一個方法一次執行不完下次輪轉時則等待改方法執行完後才執行下一次操做 4 */ 5 @DisallowConcurrentExecution 6 public class QuartzJobFactoryDisallowConcurrentExecution implements Job { 7 public final Logger log = Logger.getLogger(this.getClass()); 8 9 public void execute(JobExecutionContext context) throws JobExecutionException { 10 ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob"); 11 TaskUtils.invokMethod(scheduleJob); 12 } 13 }
TaskUtils.java
1 public class TaskUtils { 2 public final static Logger log = Logger.getLogger(TaskUtils.class); 3 4 /** 5 * 經過反射調用scheduleJob中定義的方法 6 * 7 * @param scheduleJob 8 */ 9 public static void invokMethod(ScheduleJob scheduleJob) { 10 Object object = null; 11 Class clazz = null; 12 if (StringUtils.isNotBlank(scheduleJob.getSpringId())) { 13 object = SpringUtils.getBean(scheduleJob.getSpringId()); 14 } else if (StringUtils.isNotBlank(scheduleJob.getBeanClass())) { 15 try { 16 clazz = Class.forName(scheduleJob.getBeanClass()); 17 object = clazz.newInstance(); 18 } catch (Exception e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } 22 } 23 if (object == null) { 24 log.error("任務名稱 = [" + scheduleJob.getJobName() + "]---------------未啓動成功,請檢查是否配置正確!!!"); 25 return; 26 } 27 clazz = object.getClass(); 28 Method method = null; 29 try { 30 method = clazz.getDeclaredMethod(scheduleJob.getMethodName()); 31 } catch (NoSuchMethodException e) { 32 log.error("任務名稱 = [" + scheduleJob.getJobName() + "]---------------未啓動成功,方法名設置錯誤!!!"); 33 } catch (SecurityException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 if (method != null) { 38 try { 39 method.invoke(object); 40 } catch (IllegalAccessException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } catch (IllegalArgumentException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } catch (InvocationTargetException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 System.out.println("任務名稱 = [" + scheduleJob.getJobName() + "]----------啓動成功"); 52 } 53 }
SpringUtils.java
1 public final class SpringUtils implements BeanFactoryPostProcessor { 2 3 private static ConfigurableListableBeanFactory beanFactory; // Spring應用上下文環境 4 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 5 SpringUtils.beanFactory = beanFactory; 6 } 7 8 /** 9 * 獲取對象 10 * 11 * @param name 12 * @return Object 一個以所給名字註冊的bean的實例 13 * @throws org.springframework.beans.BeansException 14 * 15 */ 16 @SuppressWarnings("unchecked") 17 public static <T> T getBean(String name) throws BeansException { 18 return (T) beanFactory.getBean(name); 19 } 20 21 /** 22 * 獲取類型爲requiredType的對象 23 * 24 * @param clz 25 * @return 26 * @throws org.springframework.beans.BeansException 27 * 28 */ 29 public static <T> T getBean(Class<T> clz) throws BeansException { 30 @SuppressWarnings("unchecked") 31 T result = (T) beanFactory.getBean(clz); 32 return result; 33 } 34 35 /** 36 * 若是BeanFactory包含一個與所給名稱匹配的bean定義,則返回true 37 * 38 * @param name 39 * @return boolean 40 */ 41 public static boolean containsBean(String name) { 42 return beanFactory.containsBean(name); 43 } 44 45 /** 46 * 判斷以給定名字註冊的bean定義是一個singleton仍是一個prototype。 47 * 若是與給定名字相應的bean定義沒有被找到,將會拋出一個異常(NoSuchBeanDefinitionException) 48 * 49 * @param name 50 * @return boolean 51 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException 52 * 53 */ 54 public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { 55 return beanFactory.isSingleton(name); 56 } 57 58 /** 59 * @param name 60 * @return Class 註冊對象的類型 61 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException 62 * 63 */ 64 public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { 65 return beanFactory.getType(name); 66 } 67 68 /** 69 * 若是給定的bean名字在bean定義中有別名,則返回這些別名 70 * 71 * @param name 72 * @return 73 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException 74 * 75 */ 76 public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { 77 return beanFactory.getAliases(name); 78 } 79 }
7,taskList.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf8"%> 2 <!doctype html> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 7 <% 8 String path = request.getContextPath(); 9 String rootPath = request.getScheme() + "://" 10 + request.getServerName() + ":" + request.getServerPort() 11 + "/"; 12 String basePath = request.getScheme() + "://" 13 + request.getServerName() + ":" + request.getServerPort() 14 + path + "/"; 15 request.setAttribute("basePath", basePath); 16 request.setAttribute("rootPath", rootPath); 17 pageContext.setAttribute("newLineChar", "\n"); 18 %> 19 20 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 21 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 22 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 23 <script type="text/javascript" src="<%=basePath %>/Contents/common/jQuery/jquery-1.8.3.min.js"></script> 24 25 <style type="text/css"> 26 .list_table { 27 border: 1px solid #CCCCCC; 28 border-collapse: collapse; 29 color: #333333; 30 margin: 0 0 0; 31 width: 100%; 32 text-align: center; 33 } 34 35 .list_table tbody td { 36 border-top: 1px solid #CCCCCC; 37 margin: 0 0 0; 38 text-align: center; 39 } 40 41 .list_table th { 42 line-height: 1.2em; 43 vertical-align: top; 44 } 45 46 .list_table td { 47 line-height: 2em; 48 font-size: 12px; 49 vertical-align: central; 50 align: center; 51 } 52 53 .list_table td input { 54 width: 90%; 55 } 56 57 .list_table tbody tr:hover th,.list_table tbody tr:hover td { 58 background: #EEF0F2; 59 } 60 61 .list_table thead tr { 62 background: none repeat scroll 0 0 #09f; 63 color: #fff; 64 font-weight: bold; 65 border-bottom: 1px solid #CCCCCC; 66 border-right: 1px solid #CCCCCC; 67 } 68 69 .datagrid-mask { 70 background: #ccc; 71 } 72 73 .datagrid-mask-msg { 74 border-color: #95B8E7; 75 } 76 77 .datagrid-mask-msg { 78 background: #ffffff 79 center; 80 } 81 82 .datagrid-mask { 83 position: absolute; 84 left: 0; 85 top: 0; 86 width: 100%; 87 height: 100%; 88 opacity: 0.3; 89 filter: alpha(opacity = 30); 90 display: none; 91 } 92 93 .datagrid-mask-msg { 94 position: absolute; 95 top: 50%; 96 margin-top: -20px; 97 padding: 12px 5px 10px 30px; 98 width: auto; 99 height: 16px; 100 border-width: 2px; 101 border-style: solid; 102 display: none; 103 } 104 105 </style> 106 </head> 107 108 <title>定時任務監控</title> 109 <body class="bgray"> 110 <form id="addForm" method="post"> 111 112 <table class="list_table"> 113 <thead> 114 <tr> 115 <td>id</td> 116 <td style="width: 100px;">name</td> 117 <td style="width: 100px;">group</td> 118 <td style="width: 100px;">狀 態</td> 119 <td >cron表達式</td> 120 <td style="width: 100px;">描 述</td> 121 <td style="width: 100px;">同步否</td> 122 <td >類路徑</td> 123 <td style="width: 100px;">spring id</td> 124 <td style="width: 100px;">方法名</td> 125 <td style="width: 100px;">操做</td> 126 </tr> 127 </thead> 128 <tbody> 129 <c:forEach var="job" items="${taskList}"> 130 <tr> 131 <td>${job.job_id }</td> 132 <td>${job.job_name }</td> 133 <td>${job.job_group }</td> 134 <td>${job.job_status } 135 <c:choose> 136 <c:when test="${job.job_status=='1' }"> 137 <a href="javascript:;" 138 onclick="changeJobStatus('${job.job_id}','stop')">中止</a> 139 </c:when> 140 <c:otherwise> 141 <a href="javascript:;" 142 onclick="changeJobStatus('${job.job_id}','start')">開啓</a> 143 </c:otherwise> 144 </c:choose> 145 </td> 146 <td>${job.cron_expression }</td> 147 <td>${job.description }</td> 148 <td>${job.is_concurrent }</td> 149 <td>${job.bean_class }</td> 150 <td>${job.spring_id }</td> 151 <td>${job.method_name }</td> 152 <td><a href="javascript:;" onclick="updateCron('${job.job_id}')">更新cron</a></td> 153 </tr> 154 </c:forEach> 155 <tr> 156 <td>n</td> 157 <td><input type="text" name="jobName" id="jobName"></input></td> 158 <td><input type="text" name="jobGroup" id="jobGroup"></input></td> 159 <td>0<input type="hidden" name="jobStatus" value="0"></input></td> 160 <td><input type="text" name="cronExpression" 161 id="cronExpression"></input></td> 162 <td><input type="text" name="description" id="description"></input></td> 163 <td><select name="isConcurrent" id="isConcurrent"> 164 <option value="1">1</option> 165 <option value="0">0</option> 166 </select></td> 167 <td><input type="text" name="beanClass" id="beanClass"></input></td> 168 <td><input type="text" name="springId" id="springId"></input></td> 169 <td><input type="text" name="methodName" id="methodName"></input></td> 170 <td><input type="button" onclick="add()" value="保存" /></td> 171 </tr> 172 </tbody> 173 </table> 174 </form> 175 <script> 176 function validateAdd() { 177 if ($.trim($('#jobName').val()) == '') { 178 alert('name不能爲空!'); 179 $('#jobName').focus(); 180 return false; 181 } 182 if ($.trim($('#jobGroup').val()) == '') { 183 alert('group不能爲空!'); 184 $('#jobGroup').focus(); 185 return false; 186 } 187 if ($.trim($('#cronExpression').val()) == '') { 188 alert('cron表達式不能爲空!'); 189 $('#cronExpression').focus(); 190 return false; 191 } 192 if ($.trim($('#beanClass').val()) == '' && $.trim($('#springId').val()) == '') { 193 $('#beanClass').focus(); 194 alert('類路徑和spring id至少填寫一個'); 195 return false; 196 } 197 if ($.trim($('#methodName').val()) == '') { 198 $('#methodName').focus(); 199 alert('方法名不能爲空!'); 200 return false; 201 } 202 return true; 203 } 204 205 function add() { 206 if (validateAdd()) { 207 showWaitMsg(); 208 $.ajax({ 209 type : "POST", 210 async : false, 211 dataType : "JSON", 212 cache : false, 213 url : "${basePath}task/add.do", 214 data : $("#addForm").serialize(), 215 success : function(data) { 216 hideWaitMsg(); 217 if (data.flag) { 218 location.reload(); 219 } else { 220 alert(data.msg); 221 } 222 }//end-callback 223 });//end-ajax 224 } 225 } 226 227 function changeJobStatus(jobId, cmd) { 228 showWaitMsg(); 229 $.ajax({ 230 type : "POST", 231 async : false, 232 dataType : "JSON", 233 cache : false, 234 url : "${basePath}task/changeJobStatus.do", 235 data : { 236 jobId : jobId, 237 cmd : cmd 238 }, 239 success : function(data) { 240 hideWaitMsg(); 241 if (data.flag) { 242 location.reload(); 243 } else { 244 alert(data.msg); 245 } 246 }//end-callback 247 });//end-ajax 248 } 249 250 function updateCron(jobId) { 251 var cron = prompt("輸入cron表達式!", "") 252 if (cron) { 253 showWaitMsg(); 254 $.ajax({ 255 type : "POST", 256 async : false, 257 dataType : "JSON", 258 cache : false, 259 url : "${basePath}task/updateCron.do", 260 data : { 261 jobId : jobId, 262 cron : cron 263 }, 264 success : function(data) { 265 hideWaitMsg(); 266 if (data.flag) { 267 location.reload(); 268 } else { 269 alert(data.msg); 270 } 271 }//end-callback 272 });//end-ajax 273 } 274 } 275 276 function showWaitMsg(msg) { 277 if (msg) { 278 279 } else { 280 msg = '正在處理,請稍候...'; 281 } 282 var panelContainer = $("body"); 283 $("<div id='msg-background' class='datagrid-mask' style=\"display:block;z-index:10006;\"></div>").appendTo(panelContainer); 284 var msgDiv = $("<div id='msg-board' class='datagrid-mask-msg' style=\"display:block;z-index:10007;left:50%\"></div>").html(msg).appendTo( 285 panelContainer); 286 msgDiv.css("marginLeft", -msgDiv.outerWidth() / 2); 287 } 288 289 function hideWaitMsg() { 290 $('.datagrid-mask').remove(); 291 $('.datagrid-mask-msg').remove(); 292 } 293 </script> 294 </body> 295 </html>
8,SQL腳本
1 CREATE TABLE `task_schedule` ( 2 `job_id` bigint(11) NOT NULL AUTO_INCREMENT, 3 `job_name` varchar(150) DEFAULT NULL, 4 `job_group` varchar(150) DEFAULT NULL, 5 `cron_expression` varchar(150) DEFAULT NULL, 6 `bean_class` varchar(300) DEFAULT NULL, 7 `spring_id` varchar(150) DEFAULT NULL, 8 `method_name` varchar(150) DEFAULT NULL, 9 `job_status` varchar(6) DEFAULT NULL COMMENT '0沒啓動,1啓動', 10 `is_concurrent` varchar(6) DEFAULT NULL COMMENT '是否等待上個任務完成,0是等待 1是不等待;', 11 `description` varchar(600) DEFAULT NULL, 12 `create_time` timestamp NULL DEFAULT NULL, 13 `update_time` timestamp NULL DEFAULT NULL, 14 PRIMARY KEY (`job_id`) 15 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='新的定時任務系統表';
insert into `task_schedule` (`job_id`, `job_name`, `job_group`, `cron_expression`, `bean_class`, `spring_id`, `method_name`, `job_status`, `is_concurrent`, `description`, `create_time`, `update_time`) values('1','SyncNotifyToSas','SyncController','0 0/2 * * * ?','com.ao.task.job.ordersync.SyncController',NULL,'SyncNotifyToSas','0','1','異步通知給Sas','2017-12-19 16:19:34','2017-12-28 13:55:57');