0,监控的意义
(1)能够查看有多少定时任务,用的什么执行策略,便于管理
(2)能够通过界面操作停止或启动某个定时任务,便于管理
(3)能够通过界面操作改变某个定时任务的执行策略,便于管理
1,pom.xml 文件添加jar
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添加
<bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
或者SpringBoot中
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,TaskServiceImpl
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
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", " "); 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 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 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 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 auto; 99 height: 16px; 100 border- 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=" 100px;">name</td> 117 <td style=" 100px;">group</td> 118 <td style=" 100px;">状 态</td> 119 <td >cron表达式</td> 120 <td style=" 100px;">描 述</td> 121 <td style=" 100px;">同步否</td> 122 <td >类路径</td> 123 <td style=" 100px;">spring id</td> 124 <td style=" 100px;">方法名</td> 125 <td style=" 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');