parent
f5c4ad557c
commit
e1b6e32c72
@ -0,0 +1,34 @@
|
||||
package ${basePackage}.action.ajax.system;
|
||||
|
||||
import ${basePackage}.frame.schedule.Scheduler;
|
||||
import ${basePackage}.module.system.ent.Schedule;
|
||||
import ${basePackage}.module.system.req.ScheduleFindRequest;
|
||||
import ${basePackage}.module.system.rsp.ScheduleFindResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ScheduleAjax {
|
||||
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
|
||||
// public TaskSqlCreateResponse create(TaskSqlCreateRequest request) {
|
||||
// return taskSqlManager.create(request, LocalData.getToken());
|
||||
// }
|
||||
//
|
||||
// public TaskSqlDeleteResponse delete(TaskSqlDeleteRequest request) {
|
||||
// return taskSqlManager.delete(request, LocalData.getToken());
|
||||
// }
|
||||
//
|
||||
// public TaskSqlUpdateResponse update(TaskSqlUpdateRequest request) {
|
||||
// return taskSqlManager.update(request, LocalData.getToken());
|
||||
// }
|
||||
|
||||
public ScheduleFindResponse find(ScheduleFindRequest request) {
|
||||
ScheduleFindResponse response = new ScheduleFindResponse();
|
||||
List<Schedule> schedules = scheduler.taskList();
|
||||
response.setResult(schedules);
|
||||
return response;
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.example.config;
|
||||
|
||||
import com.example.frame.auth.LocalData;
|
||||
import com.example.frame.schedule.RunTask;
|
||||
import com.example.frame.schedule.Scheduler;
|
||||
import com.example.frame.utils.LogUtil;
|
||||
import com.example.module.system.ent.TaskSql;
|
||||
import com.example.module.system.mgr.TaskSqlManager;
|
||||
import com.example.module.system.req.TaskSqlFindRequest;
|
||||
import com.example.module.system.rsp.TaskSqlFindResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.type.filter.AssignableTypeFilter;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Configuration
|
||||
public class TaskConfig {
|
||||
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
@Autowired
|
||||
private TaskSqlManager taskSqlManager;
|
||||
|
||||
@Bean
|
||||
public Scheduler registerScheduler() {
|
||||
return new Scheduler();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void registryTask() {
|
||||
{// 扫描类任务
|
||||
String aPackage = this.getClass().getPackage().getName();
|
||||
Pattern compile = Pattern.compile("(.*)\\.config");
|
||||
Matcher matcher = compile.matcher(aPackage);
|
||||
if (matcher.find()) {
|
||||
DefaultListableBeanFactory simpleBeanDefinitionRegistry = new DefaultListableBeanFactory();
|
||||
ClassPathBeanDefinitionScanner classPathBeanDefinitionScanner = new ClassPathBeanDefinitionScanner(simpleBeanDefinitionRegistry);
|
||||
classPathBeanDefinitionScanner.resetFilters(false);
|
||||
classPathBeanDefinitionScanner.addIncludeFilter(new AssignableTypeFilter(RunTask.class));
|
||||
classPathBeanDefinitionScanner.setBeanNameGenerator(new BeanNameGenerator() {
|
||||
@Override
|
||||
public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
|
||||
String beanClassName = beanDefinition.getBeanClassName();
|
||||
try {
|
||||
Class<?> aClass = Class.forName(beanClassName);
|
||||
Object instance = aClass.newInstance();
|
||||
RunTask task = (RunTask) instance;
|
||||
scheduler.createOrRepeat(task);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LogUtil.i("registry task " + beanClassName);
|
||||
return beanClassName;
|
||||
}
|
||||
});
|
||||
classPathBeanDefinitionScanner.scan(matcher.group(1));
|
||||
}
|
||||
}
|
||||
|
||||
{// 实例化SQL任务
|
||||
TaskSqlFindRequest taskSqlFindRequest = new TaskSqlFindRequest();
|
||||
taskSqlFindRequest.setValid(true);
|
||||
taskSqlFindRequest.setPageSize(0);
|
||||
TaskSqlFindResponse taskSqlFindResponse = taskSqlManager.find(taskSqlFindRequest, LocalData.getSysToken());
|
||||
for (TaskSql taskSql : taskSqlFindResponse.getResult()) {
|
||||
taskSqlManager.createOrRepeat(taskSql);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package ${basePackage}.config;
|
||||
|
||||
import ${basePackage}.frame.auth.LocalData;
|
||||
import ${basePackage}.frame.schedule.RunDelayRepeatTask;
|
||||
import ${basePackage}.module.system.ent.TaskSql;
|
||||
import ${basePackage}.module.system.mgr.TaskSqlManager;
|
||||
import ${basePackage}.module.system.req.TaskSqlFindRequest;
|
||||
import ${basePackage}.module.system.rsp.TaskSqlFindResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* Sql任务注册配置
|
||||
*/
|
||||
@Configuration
|
||||
public class TaskSqlConfig {
|
||||
@Autowired
|
||||
private ScheduleConfig scheduleConfig;
|
||||
@Autowired
|
||||
private TaskSqlManager taskSqlManager;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void ss() {
|
||||
TaskSqlFindRequest taskSqlFindRequest = new TaskSqlFindRequest();
|
||||
taskSqlFindRequest.setValid(true);
|
||||
taskSqlFindRequest.setPageSize(0);
|
||||
TaskSqlFindResponse taskSqlFindResponse = taskSqlManager.find(taskSqlFindRequest, LocalData.getSysToken());
|
||||
for (TaskSql taskSql : taskSqlFindResponse.getResult()) {
|
||||
if ("DelayRepeat".equals(taskSql.getTaskType())) {
|
||||
scheduleConfig.createOrRepeat(new RunDelayRepeatTask() {
|
||||
@Override
|
||||
public Duration interval() {
|
||||
String typeValue = taskSql.getTypeValue();
|
||||
return Duration.ofSeconds(Integer.parseInt(typeValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String taskId() {
|
||||
return String.valueOf(taskSql.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package ${basePackage}.frame.schedule;
|
||||
|
||||
import ${basePackage}.module.system.ent.Schedule;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public class Scheduler extends ThreadPoolTaskScheduler {
|
||||
|
||||
private Map<String, TaskWrapper> taskMap;
|
||||
|
||||
public Scheduler() {
|
||||
taskMap = new HashMap<>();
|
||||
setPoolSize(4);
|
||||
initialize();
|
||||
}
|
||||
|
||||
public boolean createOrRepeat(RunTask task) {
|
||||
if (taskMap.containsKey(task.taskId())) {
|
||||
ScheduledFuture<?> scheduledFuture = taskMap.get(task.taskId()).future;
|
||||
scheduledFuture.cancel(false);
|
||||
}
|
||||
taskMap.put(task.taskId(), new TaskWrapper(
|
||||
task.taskId(),
|
||||
task.taskName(),
|
||||
true,
|
||||
(Class<RunTask>) task.getClass(),
|
||||
task.schedule(this)
|
||||
));
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean remove(String taskId) {
|
||||
if (taskMap.containsKey(taskId)) {
|
||||
ScheduledFuture<?> scheduledFuture = taskMap.get(taskId).future;
|
||||
scheduledFuture.cancel(false);
|
||||
taskMap.remove(taskId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean stop(String taskId) {
|
||||
if (taskMap.containsKey(taskId)) {
|
||||
taskMap.get(taskId).run = false;
|
||||
ScheduledFuture<?> scheduledFuture = taskMap.get(taskId).future;
|
||||
scheduledFuture.cancel(false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Schedule> taskList() {
|
||||
List<Schedule> result = new ArrayList<>();
|
||||
for (TaskWrapper taskWrapper : taskMap.values()) {
|
||||
Schedule schedule = new Schedule();
|
||||
schedule.setId(taskWrapper.taskId);
|
||||
schedule.setName(taskWrapper.taskName);
|
||||
schedule.setRun(taskWrapper.run);
|
||||
result.add(schedule);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class TaskWrapper {
|
||||
String taskId;
|
||||
String taskName;
|
||||
boolean run;
|
||||
Class<RunTask> target;
|
||||
ScheduledFuture<?> future;
|
||||
|
||||
public TaskWrapper(String taskId, String taskName, boolean run, Class<RunTask> target, ScheduledFuture<?> future) {
|
||||
this.taskId = taskId;
|
||||
this.taskName = taskName;
|
||||
this.run = run;
|
||||
this.target = target;
|
||||
this.future = future;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package ${basePackage}.module.system.ent;
|
||||
|
||||
/**
|
||||
* Schedule - 调度任务
|
||||
*
|
||||
* @author author
|
||||
* @version 0.0.1
|
||||
* @since 2020-05-24
|
||||
*/
|
||||
public class Schedule {
|
||||
|
||||
/**
|
||||
* ID - 任务ID
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* NAME - 任务名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* run - 运行状态
|
||||
*/
|
||||
private boolean run;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean isRun() {
|
||||
return run;
|
||||
}
|
||||
|
||||
public void setRun(boolean run) {
|
||||
this.run = run;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package ${basePackage}.module.system.req;
|
||||
|
||||
import ${basePackage}.frame.base.BaseFindRequest;
|
||||
import ${basePackage}.frame.validation.Dict;
|
||||
|
||||
/**
|
||||
* TaskSqlRequest - SQL任务查询
|
||||
*
|
||||
* @author author
|
||||
* @version 0.0.1
|
||||
* @since 2020-05-24
|
||||
*/
|
||||
public class ScheduleFindRequest extends BaseFindRequest {
|
||||
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
|
||||
/**
|
||||
* 详细注释
|
||||
*/
|
||||
private String taskNote;
|
||||
|
||||
/**
|
||||
* 任务类型
|
||||
*/
|
||||
@Dict(name = "TASK_TYPE")
|
||||
private String taskType;
|
||||
|
||||
/**
|
||||
* 是否有效
|
||||
*/
|
||||
private Boolean valid;
|
||||
|
||||
public String getTaskName() {
|
||||
return this.taskName;
|
||||
}
|
||||
|
||||
public void setTaskName(String taskName) {
|
||||
this.taskName = taskName;
|
||||
}
|
||||
|
||||
public String getTaskNote() {
|
||||
return this.taskNote;
|
||||
}
|
||||
|
||||
public void setTaskNote(String taskNote) {
|
||||
this.taskNote = taskNote;
|
||||
}
|
||||
|
||||
public String getTaskType() {
|
||||
return this.taskType;
|
||||
}
|
||||
|
||||
public void setTaskType(String taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
|
||||
public Boolean getValid() {
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
public void setValid(Boolean valid) {
|
||||
this.valid = valid;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package ${basePackage}.module.system.rsp;
|
||||
|
||||
import ${basePackage}.frame.base.BaseFindResponse;
|
||||
import ${basePackage}.module.system.ent.Schedule;
|
||||
|
||||
/**
|
||||
* TaskSqlFindResponse - 任务
|
||||
*
|
||||
* @author author
|
||||
* @version 0.0.1
|
||||
* @since 2020-05-24
|
||||
*/
|
||||
public class ScheduleFindResponse extends BaseFindResponse<Schedule> {
|
||||
}
|
Loading…
Reference in new issue