模仿Cron4jPlugin插件写的 , 直接上 石马
package com.momathink.common.kit; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import it.sauronsoftware.cron4j.Scheduler; /** * Cron4j 工具 * * @author dufuzhong 2018-06-05 10:38:29 */ public class Cron4jKit { private static final Map<String, MyScheduler> MAP = new ConcurrentHashMap<>(); /** * 添加一个定时任务 * @param configName 任务名称 * @param cron 表达式调试任务 * @param daemon 表示调试线程是否设置为守护线程,默认值为 true,守护线程会在 tomcat 关闭时自动关闭 * @param runnable 执行的任务 * @return */ public static MyScheduler put(String configName, String cron, boolean daemon, Runnable runnable) { MyScheduler scheduler = new MyScheduler(configName, cron, daemon, runnable); MAP.put(configName, scheduler); start(configName); return scheduler; } public static MyScheduler get(String configName) { return MAP.get(configName); } /** * 启动指定任务 * @param configName */ public static void start(String configName) { final MyScheduler scheduler = get(configName); if (scheduler != null && !scheduler.getScheduler().isStarted()) { scheduler.getScheduler().start(); } } /** * 启动全部任务 */ public static void start() { for (final String configName : MAP.keySet()) { start(configName); } } /** * 任务停止 */ public static void stop(String configName) { final MyScheduler scheduler = get(configName); if (scheduler != null && scheduler.getScheduler().isStarted()) { scheduler.getScheduler().stop(); } } /** * 任务全部停止 */ public static void stop() { for (final String configName : MAP.keySet()) { stop(configName); } } /** * 删除任务 * @param configName */ public static void remove(String configName) { stop(configName); MAP.remove(configName); } /** * 删除全部任务 */ public static void remove() { stop(); MAP.clear(); } /** * 立即执行 * @param configName */ public static void run(String configName) { final MyScheduler scheduler = get(configName); if (scheduler != null) { scheduler.getRunnable().run(); } } /** * 立即执行全部任务 */ public static void run() { for (final String configName : MAP.keySet()) { run(configName); } } /** * 反射Runnable对象 * @param className * @return */ public static Runnable newRunnable(String className) { try { Object obj = Class.forName(className).newInstance(); if(obj instanceof Runnable) { return (Runnable) obj; } throw new IllegalStateException(className + "必须声明Runnable接口"); } catch (Exception e) { throw new IllegalStateException(e); } } @Override public String toString() { return MAP.toString(); } public static class MyScheduler { String configName; String cron; boolean daemon; Runnable runnable; Scheduler scheduler; public MyScheduler(String configName, String cron, boolean daemon, Runnable runnable) { if (configName == null) { throw new IllegalStateException("configName 不能为null"); } this.configName = configName; if (cron == null) { throw new IllegalStateException("cron 不能为null"); } this.cron = cron; this.daemon = daemon; if (runnable == null) { throw new IllegalStateException("runnable 不能为null"); } this.runnable = runnable; this.scheduler = new Scheduler(); scheduler.schedule(cron, runnable); scheduler.setDaemon(daemon); } public Scheduler getScheduler() { return scheduler; } public Runnable getRunnable() { return runnable; } @Override public String toString() { return "MyScheduler [configName=" + configName + ", cron=" + cron + ", daemon=" + daemon + ", runnable=" + runnable + ", scheduler=" + scheduler + "]"; } } //测试: public static void main(String[] args) throws Exception { System.out.println("Cron4jKit: 开始装载每分钟执行一次的任务"); final String configName = "main"; Cron4jKit.put(configName, "* * * * *", true, () -> System.out.println("Cron4jKit: main 任务运行 一次")); //Cron4jKit.put(className, "* * * * *", true, Cron4jKit.newRunnable(className)); Cron4jKit.run(configName); System.out.println("Cron4jKit: 装载完毕等待异步执行"); Thread.sleep(121000); Cron4jKit.stop(configName); Cron4jKit.stop(configName); System.out.println("Cron4jKit: 完毕"); } }
[object Object]