定时任务优化

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) {

View File

@@ -20,8 +20,6 @@ package org.dromara.maxkey.autoconfigure;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.listener.DynamicGroupsListenerAdapter;
import org.dromara.maxkey.listener.ListenerAdapter;
import org.dromara.maxkey.listener.ListenerParameter;
import org.dromara.maxkey.listener.ReorgDeptListenerAdapter;
import org.dromara.maxkey.listener.SessionListenerAdapter;
import org.dromara.maxkey.persistence.service.ConnectorsService;
@@ -29,6 +27,7 @@ import org.dromara.maxkey.persistence.service.GroupsService;
import org.dromara.maxkey.persistence.service.OrganizationsService;
import org.dromara.maxkey.provision.thread.ProvisioningRunner;
import org.dromara.maxkey.provision.thread.ProvisioningRunnerThread;
import org.dromara.maxkey.schedule.ScheduleAdapterBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
@@ -46,13 +45,12 @@ public class MaxKeyMgtListenerConfig {
public String sessionListenerAdapter(
Scheduler scheduler,
SessionManager sessionManager) throws SchedulerException {
ListenerAdapter.addListener(
scheduler,
SessionListenerAdapter.class,
"0 0/10 * * * ?",//10 minutes
new ListenerParameter().add("sessionManager",sessionManager).build()
);
new ScheduleAdapterBuilder()
.setScheduler(scheduler)
.setCron("0 0/10 * * * ?")
.setJobClass(SessionListenerAdapter.class)
.setJobData("sessionManager",sessionManager)
.build();
logger.debug("Session ListenerAdapter inited .");
return "sessionListenerAdapter";
}
@@ -61,12 +59,12 @@ public class MaxKeyMgtListenerConfig {
public String reorgDeptListenerAdapter(
Scheduler scheduler,
OrganizationsService organizationsService) throws SchedulerException {
ListenerAdapter.addListener(
scheduler,
ReorgDeptListenerAdapter.class,
"0 0/30 * * * ?",//30 minutes
new ListenerParameter().add("organizationsService",organizationsService).build()
);
new ScheduleAdapterBuilder()
.setScheduler(scheduler)
.setCron("0 0/30 * * * ?")
.setJobClass(ReorgDeptListenerAdapter.class)
.setJobData("organizationsService",organizationsService)
.build();
logger.debug("ReorgDept ListenerAdapter inited .");
return "reorgDeptListenerAdapter";
}
@@ -77,13 +75,13 @@ public class MaxKeyMgtListenerConfig {
GroupsService groupsService,
@Value("${maxkey.job.cron.schedule}") String cronSchedule
) throws SchedulerException {
ListenerAdapter.addListener(
scheduler,
DynamicGroupsListenerAdapter.class,
cronSchedule,
new ListenerParameter().add("groupsService",groupsService).build()
);
new ScheduleAdapterBuilder()
.setScheduler(scheduler)
.setCron(cronSchedule)
.setJobClass(DynamicGroupsListenerAdapter.class)
.setJobData("groupsService",groupsService)
.build();
logger.debug("DynamicGroups ListenerAdapter inited .");
return "dynamicGroupsListenerAdapter";
}

View File

@@ -20,12 +20,13 @@ package org.dromara.maxkey.listener;
import java.io.Serializable;
import org.dromara.maxkey.persistence.service.GroupsService;
import org.dromara.maxkey.schedule.ScheduleAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DynamicGroupsListenerAdapter extends ListenerAdapter implements Job , Serializable {
public class DynamicGroupsListenerAdapter extends ScheduleAdapter implements Job , Serializable {
static final Logger logger = LoggerFactory.getLogger(DynamicGroupsListenerAdapter.class);
private static final long serialVersionUID = 8831626240807856084L;
@@ -54,7 +55,7 @@ public class DynamicGroupsListenerAdapter extends ListenerAdapter implements Jo
}
@Override
void init(JobExecutionContext context){
protected void init(JobExecutionContext context){
super.init(context);
if(groupsService == null) {
groupsService = getParameter("groupsService",GroupsService.class);

View File

@@ -1,78 +0,0 @@
/*
* Copyright [2022] [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.listener;
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.JobExecutionContext;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ListenerAdapter {
private static final Logger logger = LoggerFactory.getLogger(ListenerAdapter.class);
JobExecutionContext context;
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 int jobStatus = JOBSTATUS.STOP;
void init(JobExecutionContext context){
this.context = context;
}
@SuppressWarnings("unchecked")
public <T> T getParameter(String name, Class<T> requiredType) {
return (T) context.getMergedJobDataMap().get(name);
}
public static void addListener(
Scheduler scheduler ,
Class <? extends Job> jobClass,
String cronSchedule,
JobDataMap jobDataMap
) throws SchedulerException {
String identity = jobClass.getSimpleName();
logger.debug("Cron {} , Job schedule {} ", cronSchedule , identity );
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity(identity, identity + "Group")
.build();
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronSchedule);
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity("trigger" + identity, identity + "TriggerGroup")
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
}
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright [2022] [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.listener;
import org.quartz.JobDataMap;
public class ListenerParameter {
JobDataMap parameters ;
public ListenerParameter() {
parameters = new JobDataMap();
}
public ListenerParameter add(String key , Object value) {
parameters.put(key, value);
return this;
}
public JobDataMap build() {
return this.parameters;
}
}

View File

@@ -18,13 +18,14 @@ package org.dromara.maxkey.listener;
import java.io.Serializable;
import org.dromara.maxkey.persistence.service.OrganizationsService;
import org.dromara.maxkey.schedule.ScheduleAdapter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReorgDeptListenerAdapter extends ListenerAdapter implements Job , Serializable {
public class ReorgDeptListenerAdapter extends ScheduleAdapter implements Job , Serializable {
static final Logger _logger = LoggerFactory.getLogger(ReorgDeptListenerAdapter.class);
private static final long serialVersionUID = 4782358765969474833L;

View File

@@ -21,6 +21,7 @@ import java.util.Date;
import org.dromara.maxkey.authn.session.Session;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.schedule.ScheduleAdapter;
import org.dromara.maxkey.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
@@ -28,7 +29,7 @@ import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SessionListenerAdapter extends ListenerAdapter implements Job , Serializable {
public class SessionListenerAdapter extends ScheduleAdapter implements Job , Serializable {
static final Logger logger = LoggerFactory.getLogger(SessionListenerAdapter.class);
private static final long serialVersionUID = 4782358765969474833L;
@@ -81,7 +82,7 @@ public class SessionListenerAdapter extends ListenerAdapter implements Job , S
}
@Override
void init(JobExecutionContext context){
protected void init(JobExecutionContext context){
super.init(context);
if(sessionManager == null) {
sessionManager = getParameter("sessionManager",SessionManager.class);