web.xml
<!-- 系统定时任务 --> <servlet> <servlet-name>SystemTimerManage</servlet-name> <servlet-class>com.systimer.SystemTimerManage</servlet-class> <load-on-startup>4</load-on-startup> </servlet>
SystemTimerManage.java
package com.systimer; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServlet; import org.apache.log4j.Logger; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import com.common.util.CommFun; import com.common.util.QuartzManager; /** * 系统配置定时任务 * @author ronghui * */ public class SystemTimerManage extends HttpServlet{ private static final long serialVersionUID = 1L; private static final Logger LOGGER = Logger.getLogger(SystemTimerManage.class); private static String SYSTEM_JOBGROUP = "SYSTEM_JOBGROUP"; private static String SYSTEM_TRIGGERGROUP = "SYSTEM_TRIGGERGROUP"; private static JdbcTemplate jdbcTemplate = null; @Override public void init() { WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); jdbcTemplate = wac.getBean(JdbcTemplate.class); List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from t_sys_timer where enable = 1"); for(Map<String, Object> sysTimer:list){ final String frequency = CommFun.nvl(sysTimer.get("FREQUENCY")); final String id = CommFun.nvl(sysTimer.get("ID")); final String name = CommFun.nvl(sysTimer.get("NAME")); LOGGER.info("启动任务:"+name); QuartzManager.addJob(id,SYSTEM_JOBGROUP,id,SYSTEM_TRIGGERGROUP,SystemJob.class, frequency,sysTimer); } } @Override public void destroy() { LOGGER.info("停止所有任务"); QuartzManager.shutdownJobs(); } /** * 添加单个任务 * @param: * @author: 戎辉 * @Time: 2020年4月16日 20:14:47 */ public static void schedule(String id){ cancel(id); Map<String, Object> sysTimer = jdbcTemplate.queryForMap("select * from t_sys_timer where id ="+id); final String frequency = CommFun.nvl(sysTimer.get("FREQUENCY")); final String name = CommFun.nvl(sysTimer.get("NAME")); LOGGER.info("启动任务:"+name); QuartzManager.addJob(id.toString(),SYSTEM_JOBGROUP,id.toString(),SYSTEM_TRIGGERGROUP,SystemJob.class, frequency,sysTimer); } /** * 取消单个任务 * @param: * @author: 戎辉 * @Time: 2020年4月16日 20:14:47 */ public static void cancel(String id){ try { QuartzManager.removeJob(id,SYSTEM_JOBGROUP,id,SYSTEM_TRIGGERGROUP); } catch (Exception e) { e.printStackTrace(); } } }
SystemJob.java
package com.systimer; import java.lang.reflect.Method; import java.util.Map; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import com.common.util.CommFun; /** * 系统定时任务 Job * 根据反射执行方法 * @author ronghui * */ public class SystemJob implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { @SuppressWarnings("unchecked") Map<String, Object> sysTimer = (Map<String, Object>)context.getMergedJobDataMap().get("param"); final String classPath = CommFun.nvl(sysTimer.get("CLASS_PATH")); final String methodName = CommFun.nvl(sysTimer.get("METHOD")); try { Class<?> forName = Class.forName(classPath); Object bean = null; try { WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); bean = wac.getBean(forName); } catch (NoSuchBeanDefinitionException e) { bean = forName.newInstance(); } Method declaredMethod = forName.getDeclaredMethod(methodName); declaredMethod.invoke(bean); } catch (Exception e) { e.printStackTrace(); } } }