/**
* @author shenzm
* @date 2019-7-24
* @description 作用
*/
@Service
@EnableScheduling
public class ScheduleService {
private static final Logger logger = LoggerFactory.getLogger(ScheduleService.class);
@Autowired
private Scheduler scheduler;
@Autowired
private TargetJob targetJob;
// 1. Seconds (秒)
// 2. Minutes (分)
// 3. Hours (时)
// 4. Day-of-Month (天)
// 5. Month (月)
// 6. Day-of-Week (周)
// 7. Year (年 可选字段)
//每天凌晨开始执行job, 如果探测到之前有同名的job则继续删除
@Scheduled(cron = "0 0 0 * * ?")
public void runJob(){
startJob("7:17","15:38",5, new QuartzJob(targetJob, "testSchedule"));
startJob("13:37","15:38",5, new QuartzJob(targetJob, "testSchedule2"));
}
public void startJob(String start, String end,int intervalSecond,QuartzJob quartzJob) {
try {
String jobPreffix = quartzJob.getTargetMethod();
String jobName = jobPreffix+"_jobName";
String jobGroup = jobPreffix+"_jobGroup";
String triggerName = jobPreffix+"_triggerName";
String triggerGroup = jobPreffix+"_triggerGroup";
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("target",quartzJob.getTarget());
jobDataMap.put("method",quartzJob.getTargetMethod());
JobDetail job = JobBuilder.newJob(quartzJob.getClass()).withIdentity(jobName, jobGroup).setJobData(jobDataMap).build();
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String format = dateFormat.format(date);
String preffix = format.substring(0, 10);
Trigger trigger = null;
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
if(scheduler.checkExists(jobKey)){
// 停止触发器
scheduler.pauseTrigger(triggerKey);
// 移除触发器
scheduler.unscheduleJob(triggerKey);
//删除job
scheduler.deleteJob(jobKey);
}
try {
Date startTime = dateFormat.parse(preffix+" "+ start);
Date endTime = dateFormat.parse(preffix+" "+ end);
trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerName, triggerGroup)
.startAt(startTime)
.endAt(endTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(intervalSecond).repeatForever())
.build();
} catch (ParseException e) {
e.printStackTrace();
}
try {
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("注册任务和触发器失败", e);
}
} catch (SchedulerException e) {
e.printStackTrace();
} finally {
}
}
}
/**
* @author shenzm
* @date 2019-7-24
* @description 作用
*/
public class QuartzJob implements Job {
private Object target;
private String targetMethod;
public QuartzJob(){}
public QuartzJob(Object target, String targetMethod) {
this.target = target;
this.targetMethod = targetMethod;
}
public Object getTarget() {
return target;
}
public String getTargetMethod() {
return targetMethod;
}
/**
* 无法通过构造器来进行参数的传递, 只有通过DataMap来进行参数的传递
* @param context
* @throws JobExecutionException
*/
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
target = jobDataMap.get("target");
targetMethod = jobDataMap.getString("method");
if(null != target && StringUtils.isNotEmpty(targetMethod)){
Class<?> clazz = target.getClass();
try {
Method method = clazz.getMethod(targetMethod, new Class[]{});
method.invoke(target,new Object[]{});
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
@Component
@EnableScheduling
public class TargetJob {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public void testSchedule() {
this.logger.info("哇被触发了哈哈哈哈哈");
}
public void testSchedule2() {
this.logger.info("哇被触发了哈哈哈哈哈222");
}
}