定时任务优化

This commit is contained in:
shimingxy
2024-07-15 08:55:33 +08:00
parent e1ac754186
commit 7300224acc
9 changed files with 194 additions and 150 deletions

View File

@@ -0,0 +1,47 @@
/*
* Copyright [2024] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.maxkey.schedule;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ScheduleAdapter {
private static final Logger _logger = LoggerFactory.getLogger(ScheduleAdapter.class);
JobExecutionContext context;
protected int jobStatus = JOBSTATUS.STOP;
public static final class JOBSTATUS{
public static final int STOP = 0;
public static final int RUNNING = 1;
public static final int ERROR = 2;
public static final int FINISHED = 3;
}
protected void init(JobExecutionContext context){
this.context = context;
}
@SuppressWarnings("unchecked")
public <T> T getParameter(String name, Class<T> requiredType) {
_logger.trace("requiredType {}",requiredType);
return (T) context.getMergedJobDataMap().get(name);
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright [2024] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.maxkey.schedule;
import org.apache.commons.lang3.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ScheduleAdapterBuilder {
private static final Logger _logger = LoggerFactory.getLogger(ScheduleAdapterBuilder.class);
Scheduler scheduler ;
String cron;
Class <? extends Job> jobClass;
JobDataMap jobDataMap;
String identity ;
public void addListener(
Scheduler scheduler ,
Class <? extends Job> jobClass,
String cronSchedule,
JobDataMap jobDataMap
) throws SchedulerException {
this.cron = cronSchedule;
this.scheduler = scheduler;
this.jobClass = jobClass;
this.jobDataMap = jobDataMap;
this.build();
}
public ScheduleAdapterBuilder setIdentity(String identity) {
this.identity = identity;
return this;
}
public ScheduleAdapterBuilder setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
return this;
}
public ScheduleAdapterBuilder setJobDataMap(JobDataMap jobDataMap) {
this.jobDataMap = jobDataMap;
return this;
}
public ScheduleAdapterBuilder setJobData(String key,Object data) {
if(this.jobDataMap == null) {
jobDataMap = new JobDataMap();
}
this.jobDataMap.put(key, data);
return this;
}
public ScheduleAdapterBuilder setCron(String cron) {
this.cron = cron;
return this;
}
public ScheduleAdapterBuilder setJobClass(Class <? extends Job> jobClass) {
this.jobClass = jobClass;
return this;
}
public void build() throws SchedulerException {
if(StringUtils.isBlank(identity)) {
identity = jobClass.getSimpleName();
}
_logger.debug("Job schedule {} ,Cron {} ", identity ,cron);
JobDetail jobDetail =
JobBuilder.newJob(jobClass)
.withIdentity(identity, identity + "Group")
.build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
CronTrigger cronTrigger =
TriggerBuilder.newTrigger()
.withIdentity("trigger" + identity, identity + "TriggerGroup")
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
}

View File

@@ -456,12 +456,9 @@ public final class WebContext {
* @return
*/
public static boolean captchaValid(String captcha) {
if (captcha == null || !captcha
.equals(WebContext.getSession().getAttribute(
WebConstants.KAPTCHA_SESSION_KEY).toString())) {
return false;
}
return true;
return (captcha != null &&
captcha.equals(WebContext.getSession().getAttribute(
WebConstants.KAPTCHA_SESSION_KEY).toString()));
}
/**
@@ -493,7 +490,7 @@ public final class WebContext {
String message = code;
try {
message = getApplicationContext().getMessage(
code.toString(),
code,
filedValues,
getLocale());
} catch (Exception e) {