com.github.lontime.extquartz.provider.AbstractProvider Maven / Gradle / Ivy
package com.github.lontime.extquartz.provider;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import com.github.lontime.base.commonj.components.AbstractComponentLifecycle;
import com.github.lontime.base.commonj.components.ComponentShutdownPriority;
import com.github.lontime.base.commonj.utils.DateHelper;
import com.github.lontime.base.commonj.utils.LoggerHelper;
import com.github.lontime.extquartz.common.JobModel;
import com.github.lontime.extquartz.common.TriggerAndJobModel;
import com.github.lontime.extquartz.common.TriggerModel;
import com.github.lontime.extquartz.container.SchedulerContainer;
import org.quartz.CronTrigger;
import org.quartz.DailyTimeIntervalTrigger;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
/**
* AbstractProvider.
* @author lontime
* @since 1.0
*/
public abstract class AbstractProvider extends AbstractComponentLifecycle implements Provider {
private SchedulerContainer container;
public AbstractProvider() {
this.container = new SchedulerContainer();
}
@Override
public int getShutdownPriority() {
return ComponentShutdownPriority.LIB_LOW.getValue();
}
@Override
public void initialize() {
container.initialize();
}
@Override
public void start() {
container.start();
}
@Override
public void stop() {
container.stop();
}
@Override
public void destroy() {
container.destroy();
}
@Override
public Scheduler getScheduler(String name) {
return container.getScheduler(name);
}
@Override
public void close() {
reentrantStopAndRemoveHooklet();
}
@Override
public List getAllJobs(String name, GroupMatcher matcher) {
final List jobModels = new ArrayList<>();
try {
final Scheduler scheduler = getScheduler(name);
final Set jobKeys = scheduler.getJobKeys(matcher);
for (JobKey jobKey : jobKeys) {
jobModels.add(createJob(scheduler, jobKey));
}
} catch (Exception e) {
LoggerHelper.warnv(e, "getAllJobs is error!");
}
return jobModels;
}
@Override
public List getAllTriggers(String name, GroupMatcher matcher) {
final List models = new ArrayList<>();
try {
final Scheduler scheduler = getScheduler(name);
final Set triggerKeys = scheduler.getTriggerKeys(matcher);
for (TriggerKey triggerKey : triggerKeys) {
final Trigger trigger = scheduler.getTrigger(triggerKey);
final JobKey jobKey = trigger.getJobKey();
final TriggerAndJobModel model = new TriggerAndJobModel();
model.setJobName(jobKey.getName());
model.setJobGroupName(jobKey.getGroup());
model.setTriggerName(triggerKey.getName());
model.setTriggerGroupName(triggerKey.getGroup());
model.setNextFireTime(DateHelper.dateToString(trigger.getNextFireTime()));
model.setEndFireTime(DateHelper.dateToString(trigger.getEndTime()));
model.setFinalFireTime(DateHelper.dateToString(trigger.getFinalFireTime()));
model.setPreviousFireTime(DateHelper.dateToString(trigger.getPreviousFireTime()));
model.setStartTime(DateHelper.dateToString(trigger.getStartTime()));
model.setMisfireInstruction(trigger.getMisfireInstruction());
model.setPriority(trigger.getPriority());
model.setDesc(trigger.getDescription());
if (trigger instanceof CronTrigger) {
model.setKind("Cron");
model.setCronExpression(((CronTrigger) trigger).getCronExpression());
} else if (trigger instanceof SimpleTrigger) {
model.setKind("Simple");
model.setRepeatCount(((SimpleTrigger) trigger).getRepeatCount());
model.setRepeatIntervalMs(((SimpleTrigger) trigger).getRepeatInterval());
} else if (trigger instanceof DailyTimeIntervalTrigger) {
model.setKind("DailyTime");
model.setRepeatCount(((DailyTimeIntervalTrigger) trigger).getRepeatCount());
model.setIntervalUnit(((DailyTimeIntervalTrigger) trigger).getRepeatIntervalUnit().name());
model.setRepeatInterval(((DailyTimeIntervalTrigger) trigger).getRepeatInterval());
} else {
model.setKind("Unknown");
}
models.add(model);
}
} catch (Exception e) {
LoggerHelper.warnv(e, "getAllTriggers is error!");
}
return models;
}
private JobModel createJob(Scheduler scheduler, JobKey jobKey) throws SchedulerException {
final JobModel model = new JobModel();
final List extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
final List triggerModels = new ArrayList<>();
model.setJobName(jobKey.getName());
model.setJobGroupName(jobKey.getGroup());
for (Trigger trigger : triggers) {
final TriggerKey triggerKey = trigger.getKey();
final TriggerModel triggerModel = new TriggerModel();
triggerModel.setTriggerName(triggerKey.getName());
triggerModel.setTriggerGroupName(triggerKey.getGroup());
triggerModel.setNextFireTime(DateHelper.dateToString(trigger.getNextFireTime()));
triggerModel.setEndFireTime(DateHelper.dateToString(trigger.getEndTime()));
triggerModel.setFinalFireTime(DateHelper.dateToString(trigger.getFinalFireTime()));
triggerModel.setPreviousFireTime(DateHelper.dateToString(trigger.getPreviousFireTime()));
triggerModel.setStartTime(DateHelper.dateToString(trigger.getStartTime()));
triggerModel.setMisfireInstruction(trigger.getMisfireInstruction());
triggerModel.setPriority(trigger.getPriority());
triggerModel.setDesc(trigger.getDescription());
if (trigger instanceof CronTrigger) {
triggerModel.setKind("Cron");
triggerModel.setCronExpression(((CronTrigger) trigger).getCronExpression());
} else if (trigger instanceof SimpleTrigger) {
triggerModel.setKind("Simple");
triggerModel.setRepeatCount(((SimpleTrigger) trigger).getRepeatCount());
triggerModel.setRepeatIntervalMs(((SimpleTrigger) trigger).getRepeatInterval());
} else if (trigger instanceof DailyTimeIntervalTrigger) {
triggerModel.setKind("DailyTime");
triggerModel.setRepeatCount(((DailyTimeIntervalTrigger) trigger).getRepeatCount());
triggerModel.setIntervalUnit(((DailyTimeIntervalTrigger) trigger).getRepeatIntervalUnit().name());
triggerModel.setRepeatInterval(((DailyTimeIntervalTrigger) trigger).getRepeatInterval());
} else {
triggerModel.setKind("Unknown");
}
triggerModels.add(triggerModel);
}
model.setTriggers(triggerModels);
return model;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy