init project

This commit is contained in:
ldj_willow
2022-06-27 09:19:14 +08:00
parent bdd517b52f
commit bca0d47c98
1151 changed files with 134747 additions and 0 deletions

View 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);
}
}

View 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();
}
}
}