use of org.quartz.SimpleScheduleBuilder in project BRFS by zhangnianli.
the class QuartzBaseSchedulers method addTask.
@Override
public boolean addTask(T task) throws Exception {
if (!checkTask(task)) {
return false;
}
// 1.设置job的名称及执行的class
Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(task.getClassInstanceName());
String taskName = task.getTaskName();
String taskGroup = task.getTaskGroupName();
JobBuilder jobBuilder = JobBuilder.newJob(clazz).withIdentity(taskName, taskGroup);
// 2.设置任务需要的数据
Map<String, String> tmp = task.getTaskContent();
if (tmp != null && !tmp.isEmpty()) {
JobDataMap jobData = new JobDataMap();
jobData.putAll(tmp);
jobBuilder.usingJobData(jobData);
}
// 3.生成jobDetail
JobDetail jobDetail = jobBuilder.build();
// 4.判断触发器的类型 0 cron任务,1 simple任务
int taskType = task.getTaskKind();
String cycleContent = task.getCycleContent();
Trigger trigger = null;
if (taskType == 0) {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cycleContent);
trigger = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(cronScheduleBuilder).build();
} else if (taskType == 1) {
String[] cycles = StringUtils.getSplit(cycleContent, ",");
if (cycles == null || cycles.length == 0) {
throw new NullPointerException("simple trigger cycle time is empty !!! content : " + cycleContent);
}
if (cycles.length != 2) {
throw new NullPointerException("simple trigger cycle time is error !!! content : " + cycleContent);
}
long interval = Long.valueOf(cycles[0]);
int repeateCount = Integer.valueOf(cycles[1]);
SimpleScheduleBuilder sSched = SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(interval).withRepeatCount(repeateCount);
trigger = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).startNow().withSchedule(sSched).build();
}
if (trigger == null || jobDetail == null) {
return false;
}
Scheduler scheduler = this.ssf.getScheduler(this.instanceName);
scheduler.scheduleJob(jobDetail, trigger);
return true;
}
use of org.quartz.SimpleScheduleBuilder in project motech by motech.
the class MotechSchedulerServiceImpl method scheduleRepeatingJob.
private void scheduleRepeatingJob(RepeatingSchedulableJob job, boolean update) {
logObjectIfNotNull(job);
validateRepeatingSchedulableJob(job);
MotechEvent motechEvent = job.getMotechEvent();
DateTime jobStartTime = job.getStartDate();
DateTime jobEndTime = job.getEndDate();
Integer repeatIntervalInSeconds = job.getRepeatIntervalInSeconds();
Integer jobRepeatCount = job.getRepeatCount();
if (null == jobRepeatCount) {
jobRepeatCount = MAX_REPEAT_COUNT;
}
JobId jobId = new RepeatingJobId(motechEvent);
JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
jobDetail.getJobDataMap().put(EVENT_METADATA, createMetadataForMisfireSchedulableJob(job, motechEvent));
try {
if (scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME)) != null) {
unscheduleJob(jobId);
}
} catch (SchedulerException e) {
throw new MotechSchedulerException(format("Schedule or reschedule the job: %s.\n%s", jobId, e.getMessage()), "scheduler.error.cantRescheduleJob", Arrays.asList(jobId.value(), e.getMessage()), e);
}
ScheduleBuilder scheduleBuilder;
if (!job.isUseOriginalFireTimeAfterMisfire()) {
SimpleScheduleBuilder simpleSchedule = simpleSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withRepeatCount(jobRepeatCount);
simpleSchedule = setMisfirePolicyForSimpleTrigger(simpleSchedule, schedulerSettings.getProperty("scheduler.repeating.trigger.misfire.policy"));
scheduleBuilder = simpleSchedule;
} else {
if (job.getRepeatCount() != null) {
final double half = 0.5;
jobEndTime = new DateTime((long) (job.getStartDate().getMillis() + repeatIntervalInSeconds * MILLISECOND * (job.getRepeatCount() + half)));
}
scheduleBuilder = CalendarIntervalScheduleBuilder.calendarIntervalSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withMisfireHandlingInstructionFireAndProceed();
}
Trigger trigger = buildJobDetail(job, DateUtil.toDate(jobStartTime), DateUtil.toDate(jobEndTime), jobId, jobDetail, scheduleBuilder);
scheduleJob(jobDetail, trigger, update);
}
use of org.quartz.SimpleScheduleBuilder in project BRFS by zhangnianli.
the class DefaultBaseSchedulers method addTask.
@Override
public boolean addTask(SumbitTaskInterface task) throws ParamsErrorException {
// 1.检查任务的有效性
checkTask(task);
// 2.线程池处于暂停时,不提交任务
if (this.pausePoolFlag) {
LOG.warn("Thread pool is paused !!!");
return false;
}
try {
Scheduler scheduler = this.ssf.getScheduler(this.instanceName);
// 当线程池不处于运行时,将不添加任务
if (!isNormal()) {
LOG.warn("Thread pool is not normal");
return false;
}
// 当线程池满了也不会添加任务
if (this.poolSize <= getSumbitTaskCount()) {
LOG.warn("thread pool is full !!!");
return false;
}
// 1.设置job的名称及执行的class
Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(task.getClassInstanceName());
String taskName = task.getTaskName();
String taskGroup = task.getTaskGroupName();
JobBuilder jobBuilder = JobBuilder.newJob(clazz).withIdentity(taskName, taskGroup);
// 2.设置任务需要的数据
Map<String, String> tmp = task.getTaskContent();
if (tmp != null && !tmp.isEmpty()) {
JobDataMap jobData = new JobDataMap();
jobData.putAll(tmp);
jobBuilder.usingJobData(jobData);
}
// 3.生成jobDetail
JobDetail jobDetail = jobBuilder.build();
// 4.判断触发器的类型 0 cron任务,1 simple任务
int taskType = task.getTaskKind();
String cycleContent = task.getCycleContent();
Trigger trigger = null;
if (taskType == 0) {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cycleContent);
trigger = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(cronScheduleBuilder).build();
} else if (taskType == 1) {
String[] cycles = BrStringUtils.getSplit(cycleContent, ",");
if (cycles == null || cycles.length == 0) {
throw new NullPointerException("simple trigger cycle time is empty !!! content : " + cycleContent);
}
if (cycles.length != 5) {
throw new NullPointerException("simple trigger cycle time is error !!! content : " + cycleContent);
}
long interval = Long.parseLong(cycles[0]);
int repeateCount = Integer.parseInt(cycles[1]);
repeateCount = repeateCount - 1;
long delayTime = Long.parseLong(cycles[2]);
boolean rightNow = Boolean.valueOf(cycles[3]);
boolean cycleFlag = Boolean.valueOf(cycles[4]);
SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule();
builder.withIntervalInMilliseconds(interval);
if (cycleFlag) {
builder.repeatForever();
} else if (repeateCount >= 0) {
builder.withRepeatCount(repeateCount);
} else {
LOG.warn("repeated count is zero !!!!");
return false;
}
TriggerBuilder trigBuilder = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(builder);
if (!rightNow && delayTime > 0) {
long current = System.currentTimeMillis() + delayTime;
Date date = new Date(current);
trigBuilder.startAt(date);
} else {
trigBuilder.startNow();
}
trigger = trigBuilder.build();
}
if (trigger == null || jobDetail == null) {
LOG.warn(" create task message is null");
return false;
}
scheduler.scheduleJob(jobDetail, trigger);
return true;
} catch (NumberFormatException | ClassNotFoundException | ParseException | SchedulerException e) {
LOG.error("add task {}", e);
}
return false;
}
use of org.quartz.SimpleScheduleBuilder in project pancm_project by xuwujing.
the class QuartzJdbcTest method startSchedule.
/**
* 开始一个simpleSchedule()调度
*/
public static void startSchedule() {
try {
// 1、创建一个JobDetail实例,指定Quartz
JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1_1", "jGroup1").build();
// 触发器类型
SimpleScheduleBuilder builder = SimpleScheduleBuilder.repeatSecondlyForTotalCount(5);
// CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
// 2、创建Trigger
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1_1", "tGroup1").startNow().withSchedule(builder).build();
// 3、创建Scheduler
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
// 4、调度执行
scheduler.scheduleJob(jobDetail, trigger);
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭调度器
scheduler.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
Aggregations