init project
This commit is contained in:
94
hd/nladmin-system/src/test/java/day03/QuartzManager.java
Normal file
94
hd/nladmin-system/src/test/java/day03/QuartzManager.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package day03;
|
||||
|
||||
|
||||
import org.quartz.*;
|
||||
import org.quartz.impl.StdSchedulerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @Title:Quartz管理类
|
||||
*
|
||||
* @Description:
|
||||
*
|
||||
* @Copyright:
|
||||
* @author zz 2008-10-8 14:19:01
|
||||
* @version 1.00.000
|
||||
*
|
||||
*/
|
||||
public class QuartzManager {
|
||||
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
|
||||
|
||||
private static String HM_JOB = "HM_JOB";
|
||||
private static String HM_TRIGGER = "HM_TRIGGER";
|
||||
|
||||
/**
|
||||
* 增加任务
|
||||
* @param jobClass 任务实现类
|
||||
* @param jobName 任务名称
|
||||
* @param interval 间隔时间
|
||||
* @param data 数据
|
||||
*/
|
||||
public static void addJob(Class<? extends Job> jobClass, String jobName, int interval, Map<String, Object> data) {
|
||||
try {
|
||||
Scheduler sched = gSchedulerFactory.getScheduler();
|
||||
|
||||
JobDetail jobDetail = JobBuilder.newJob(jobClass)
|
||||
.withIdentity(jobName, HM_JOB)//任务名称和组构成任务key
|
||||
.build();
|
||||
jobDetail.getJobDataMap().putAll(data);
|
||||
// 触发器
|
||||
SimpleTrigger trigger = TriggerBuilder.newTrigger()
|
||||
.withIdentity(jobName, HM_TRIGGER)//触发器key
|
||||
.startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
|
||||
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
|
||||
.withIntervalInSeconds(interval)
|
||||
.repeatForever())
|
||||
.build();
|
||||
|
||||
sched.scheduleJob(jobDetail, trigger);
|
||||
// 启动
|
||||
if (!sched.isShutdown()) {
|
||||
sched.start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除任务
|
||||
*
|
||||
* @param jobName 任务名称
|
||||
*/
|
||||
public static void removeJob(String jobName) {
|
||||
try {
|
||||
Scheduler sched = gSchedulerFactory.getScheduler();
|
||||
sched.deleteJob(new JobKey(jobName, HM_JOB));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("id", "1");
|
||||
addJob(TestJob.class, "aaa", 1, map);
|
||||
|
||||
map.clear();
|
||||
map.put("id", "2");
|
||||
addJob(TestJob.class, "bbb", 1, map);
|
||||
|
||||
Thread.sleep(3000);
|
||||
removeJob("aaa");
|
||||
System.out.println("main end----------");
|
||||
Thread.sleep(3000);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
19
hd/nladmin-system/src/test/java/day03/TestJob.java
Normal file
19
hd/nladmin-system/src/test/java/day03/TestJob.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package day03;
|
||||
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
|
||||
public class TestJob implements Job {
|
||||
|
||||
public void execute(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
|
||||
String id = jobexecutioncontext.getJobDetail().getJobDataMap().getString("id");
|
||||
System.out.println("threadId: " + Thread.currentThread().getId() + ", id: " + id);
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user