use of org.quartz.JobBuilder 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.JobBuilder in project kernel by exoplatform.
the class JobSchedulerServiceImpl method addPeriodJob.
public void addPeriodJob(JobInfo jinfo, PeriodInfo pinfo, JobDataMap jdatamap) throws Exception {
int repeat = pinfo.getRepeatCount();
Date start = pinfo.getStartTime();
JobInfo jobinfo = getJobInfo(jinfo);
if (start == null)
start = new Date();
if (repeat <= 0)
repeat = SimpleTrigger.REPEAT_INDEFINITELY;
else
repeat = repeat - 1;
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(repeat).withIntervalInMilliseconds(pinfo.getRepeatInterval())).startAt(start).endAt(pinfo.getEndTime()).build();
@SuppressWarnings("unchecked") JobBuilder jb = JobBuilder.newJob(jobinfo.getJob()).withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withDescription(jinfo.getDescription());
JobDetail job = jdatamap == null ? jb.build() : jb.usingJobData(jdatamap).build();
scheduleJob(job, trigger);
}
use of org.quartz.JobBuilder in project Dempsy by Dempsy.
the class Container method startEvictionThread.
private void startEvictionThread() throws SchedulerException {
// when done in parallel.
synchronized (StdSchedulerFactory.class) {
if (0 == evictionCycleTime || null == evictionTimeUnit) {
LOGGER.warn("Eviction Thread cannot start with zero cycle time or null TimeUnit {} {}", evictionCycleTime, evictionTimeUnit);
return;
}
if (prototype != null && prototype.isEvictionSupported()) {
final JobBuilder jobBuilder = JobBuilder.newJob(EvictionCheckJob.class);
final JobDetail jobDetail = jobBuilder.build();
jobDetail.getJobDataMap().put(EVICTION_CHECK_JOB_NAME, this);
final Trigger trigger = QuartzHelper.getSimpleTrigger(evictionTimeUnit, (int) evictionCycleTime, true);
evictionScheduler = StdSchedulerFactory.getDefaultScheduler();
evictionScheduler.scheduleJob(jobDetail, trigger);
evictionScheduler.start();
}
}
}
use of org.quartz.JobBuilder in project Dempsy by Dempsy.
the class OutputQuartzHelper method getJobDetail.
/**
* Gets the job detail.
*
* @param outputInvoker the output invoker
* @return the job detail
*/
public JobDetail getJobDetail(final OutputInvoker outputInvoker) {
final JobBuilder jobBuilder = JobBuilder.newJob(OutputJob.class);
final JobDetail jobDetail = jobBuilder.build();
jobDetail.getJobDataMap().put(OUTPUT_JOB_NAME, outputInvoker);
return jobDetail;
}
Aggregations