AppStartListener

This commit is contained in:
chenjianxing 2020-06-24 13:24:51 +08:00
parent c0faacae36
commit b94eb966f6
2 changed files with 24 additions and 30 deletions

View File

@ -1,30 +1,15 @@
package io.metersphere.listeners;
import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.Schedule;
import io.metersphere.base.mapper.ScheduleMapper;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.job.QuartzManager;
import io.metersphere.job.sechedule.ApiTestJob;
import io.metersphere.service.ScheduleService;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* 服务启动完成后打印日志
*/
@Component
public class AppStartListener implements ApplicationListener<ApplicationReadyEvent> {
// private static boolean started = false;
@Resource
private ScheduleService scheduleService;
@ -39,21 +24,7 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
e.printStackTrace();
}
List<Schedule> Schedules = scheduleService.getEnableSchedule();
scheduleService.startEnableSchedules();
Schedules.forEach(schedule -> {
try {
if (schedule.getEnable()) {
LogUtil.error("初始化任务:" + JSON.toJSONString(schedule));
QuartzManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getGroup()),
new TriggerKey(schedule.getKey(), schedule.getGroup()), Class.forName(schedule.getJob()), schedule.getValue(),
QuartzManager.getDefaultJobDataMap(schedule.getResourceId(), schedule.getValue(), schedule.getUserId()));
}
Thread.sleep(1*60*1000);
} catch (Exception e) {
LogUtil.error("应用启动,初始化任务失败", e);
e.printStackTrace();
}
});
}
}

View File

@ -1,8 +1,13 @@
package io.metersphere.service;
import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.Schedule;
import io.metersphere.base.domain.ScheduleExample;
import io.metersphere.base.mapper.ScheduleMapper;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.job.QuartzManager;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -54,4 +59,22 @@ public class ScheduleService {
example.createCriteria().andEnableEqualTo(true);
return scheduleMapper.selectByExample(example);
}
public void startEnableSchedules() {
List<Schedule> Schedules = getEnableSchedule();
Schedules.forEach(schedule -> {
try {
if (schedule.getEnable()) {
LogUtil.error("初始化任务:" + JSON.toJSONString(schedule));
QuartzManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getGroup()),
new TriggerKey(schedule.getKey(), schedule.getGroup()), Class.forName(schedule.getJob()), schedule.getValue(),
QuartzManager.getDefaultJobDataMap(schedule.getResourceId(), schedule.getValue(), schedule.getUserId()));
}
Thread.sleep(1*60*1000);
} catch (Exception e) {
LogUtil.error("初始化任务失败", e);
e.printStackTrace();
}
});
}
}