فهرست منبع

添加定时任务

hym 4 سال پیش
والد
کامیت
19f87077c8

+ 8 - 0
meter-reading-sishui/read-sqlSever/pom.xml

@@ -80,6 +80,14 @@
             <groupId>com.microsoft.sqlserver</groupId>
             <artifactId>mssql-jdbc</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.quartz-scheduler</groupId>
+            <artifactId>quartz</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 76 - 0
meter-reading-sishui/read-sqlSever/src/main/java/com/zcxk/config/QuartzManager.java

@@ -0,0 +1,76 @@
+package com.zcxk.config;
+import com.zcxk.job.TestJob;
+import org.quartz.*;
+import org.quartz.impl.JobDetailImpl;
+import org.quartz.impl.StdSchedulerFactory;
+
+import javax.print.attribute.standard.JobName;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+
+import static java.lang.Thread.sleep;
+
+
+/**
+ * @Description: 定时任务管理类
+ * Created by zhangxb on 2017/2/10.
+ */
+public class QuartzManager {
+    private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
+    private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
+    private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
+
+    /**
+     * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
+     *
+     * @param jobName
+     *            任务名
+     * @param cls
+     *            任务
+     * @param time
+     *            时间设置,参考quartz说明文档
+     *
+     * @Title: QuartzManager.java
+     */
+    @SuppressWarnings("unchecked")
+    public static void addJob(String jobName, Class cls, String time,Object params) {
+        try {
+            Scheduler sched = gSchedulerFactory.getScheduler();
+            JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME )// 任务名称和组构成任务key
+                    .build();
+            jobDetail.getJobDataMap().put("params", params);
+            // 触发器
+            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, JOB_GROUP_NAME)// 触发器key
+                    .startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
+                    .withSchedule(CronScheduleBuilder.cronSchedule(time)).startNow().build();
+            sched.scheduleJob(jobDetail, trigger);
+            // 启动
+            if (!sched.isShutdown()) {
+                sched.start();
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+    @SuppressWarnings("unchecked")
+    public static void deleteJob(String jobName) {
+        JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
+        try {
+            Scheduler sched = gSchedulerFactory.getScheduler();
+            sched.deleteJob(jobKey);
+        } catch (SchedulerException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void main(String[] args) {
+        QuartzManager.addJob("test", TestJob.class,"*/5 * * * * ?",new HashMap<>());
+        try {
+            sleep(30000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        QuartzManager.deleteJob("test");
+    }
+}

+ 15 - 0
meter-reading-sishui/read-sqlSever/src/main/java/com/zcxk/job/TestJob.java

@@ -0,0 +1,15 @@
+package com.zcxk.job;
+
+import org.apache.commons.lang3.time.DateUtils;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+
+import java.util.Date;
+
+public class TestJob implements Job {
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        System.out.println("欢迎使用yyblog,这是一个定时任务  --小卖铺的老爷爷!"+ new Date());
+    }
+}