代码优化调整

This commit is contained in:
inrgihc
2024-07-25 22:07:05 +08:00
parent 815528b817
commit 9cba0c17a6
52 changed files with 146 additions and 505 deletions

View File

@@ -165,7 +165,7 @@ public class LogAdviceAspect {
String description = getParsedDescription(logAnnotation.description(), ctx);
if (null == throwable) {
SystemLogEntity systemLogEntity = SystemLogEntity.builder()
.type(LogTypeEnum.OPERRATE_LOG.getValue())
.type(LogTypeEnum.OPERATE_LOG.getValue())
.username(this.getUsernameFromToken())
.ipAddress(ServletUtils.getIpAddr())
.moduleName(moduleName)
@@ -179,7 +179,7 @@ public class LogAdviceAspect {
systemLogDAO.insert(systemLogEntity);
} else {
SystemLogEntity systemLogEntity = SystemLogEntity.builder()
.type(LogTypeEnum.OPERRATE_LOG.getValue())
.type(LogTypeEnum.OPERATE_LOG.getValue())
.username(this.getUsernameFromToken())
.ipAddress(ServletUtils.getIpAddr())
.moduleName(moduleName)

View File

@@ -33,7 +33,7 @@ import com.gitee.dbswitch.admin.model.response.AssignmentInfoResponse;
import com.gitee.dbswitch.admin.model.response.AssignmentsDataResponse;
import com.gitee.dbswitch.admin.type.JobStatusEnum;
import com.gitee.dbswitch.admin.type.ScheduleModeEnum;
import com.gitee.dbswitch.admin.util.EasyexcelUtils;
import com.gitee.dbswitch.admin.util.ExcelUtils;
import com.gitee.dbswitch.admin.util.PageUtils;
import com.gitee.dbswitch.common.converter.ConverterFactory;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
@@ -376,6 +376,6 @@ public class AssignmentService {
assignmentsDataResponse.setRunStatus(JobStatusEnum.of(status).getName());
assignmentsDataResponses.add(assignmentsDataResponse);
}
EasyexcelUtils.write(response, AssignmentsDataResponse.class, assignmentsDataResponses, "任务管理", "任务管理列表");
ExcelUtils.write(response, AssignmentsDataResponse.class, assignmentsDataResponses, "任务管理", "任务管理列表");
}
}

View File

@@ -13,8 +13,8 @@ import com.gitee.dbswitch.admin.model.response.OnlineSqlDataResponse.ColumnItem;
import com.gitee.dbswitch.admin.model.response.OnlineSqlDataResponse.SqlInput;
import com.gitee.dbswitch.admin.model.response.OnlineSqlDataResponse.SqlResult;
import com.gitee.dbswitch.admin.model.response.SchemaTableDataResponse;
import com.gitee.dbswitch.admin.util.ExecuteSqlUtils;
import com.gitee.dbswitch.admin.util.ExecuteSqlUtils.ScriptExecuteResult;
import com.gitee.dbswitch.admin.util.DBSqlUtils;
import com.gitee.dbswitch.admin.util.DBSqlUtils.ScriptExecuteResult;
import com.gitee.dbswitch.admin.util.PageUtils;
import com.gitee.dbswitch.common.entity.CloseableDataSource;
import com.gitee.dbswitch.common.type.ProductTypeEnum;
@@ -155,7 +155,7 @@ public class MetaDataService {
List<SqlResult> results = new ArrayList<>(statements.size());
for (String sql : statements) {
try {
ScriptExecuteResult result = ExecuteSqlUtils.execute(connection, sql, page, size);
ScriptExecuteResult result = DBSqlUtils.execute(connection, sql, page, size);
summaries.add(SqlInput.builder().sql(sql).summary(result.getResultSummary()).build());
if (CollectionUtils.isNotEmpty(result.getResultHeader())) {
results.add(

View File

@@ -16,6 +16,8 @@ import com.gitee.dbswitch.admin.entity.AssignmentTaskEntity;
import com.gitee.dbswitch.admin.execution.ExecuteJobTaskRunnable;
import com.gitee.dbswitch.admin.type.JobStatusEnum;
import com.gitee.dbswitch.admin.type.ScheduleModeEnum;
import com.gitee.dbswitch.common.event.EventSubscriber;
import com.gitee.dbswitch.common.event.ListenedEvent;
import com.gitee.dbswitch.common.event.TaskEventHub;
import com.gitee.dbswitch.common.util.UuidUtils;
import com.google.common.collect.Sets;
@@ -35,21 +37,14 @@ import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ScheduleService {
private static final String evenName = "start";
static {
TaskEventHub.init(5);
}
public class ScheduleService implements InitializingBean {
/**
* @Bean是一个方法级别上的注解Bean的ID为方法名字。
@@ -65,30 +60,27 @@ public class ScheduleService {
@Resource
private AssignmentJobDAO assignmentJobDAO;
private TaskEventHub manualRunEvenHub = new TaskEventHub("manualRun");
private TaskEventHub taskEventBus = new TaskEventHub("manualRun", 5);
private Map<String, ExecuteJobTaskRunnable> taskRunnableMap = new ConcurrentHashMap<>();
@EventListener(ApplicationReadyEvent.class)
public void registerEventListener() {
manualRunEvenHub.listen(
evenName,
(event) -> {
event.checkArgs(Long.class, String.class);
Long taskId = (Long) event.getArgs()[0];
Integer schedule = ScheduleModeEnum.MANUAL.getValue();
String jobKey = (String) event.getArgs()[1];
ExecuteJobTaskRunnable taskRunnable
= new ExecuteJobTaskRunnable(taskId, schedule, jobKey);
taskRunnableMap.put(jobKey, taskRunnable);
try {
taskRunnable.run();
} finally {
taskRunnableMap.remove(jobKey);
}
return null;
}
);
public void afterPropertiesSet() throws Exception {
taskEventBus.registerSubscriber(new EventSubscriber(this::manualRunTask));
}
private void manualRunTask(ListenedEvent event) {
event.checkArgs(Long.class, String.class);
Long taskId = (Long) event.getArgs()[0];
String jobKey = (String) event.getArgs()[1];
Integer schedule = ScheduleModeEnum.MANUAL.getValue();
ExecuteJobTaskRunnable taskRunnable
= new ExecuteJobTaskRunnable(taskId, schedule, jobKey);
taskRunnableMap.put(jobKey, taskRunnable);
try {
taskRunnable.run();
} finally {
taskRunnableMap.remove(jobKey);
}
}
public void scheduleTask(Long taskId, ScheduleModeEnum scheduleMode) {
@@ -111,9 +103,21 @@ public class ScheduleService {
AssignmentTaskEntity task = assignmentTaskDAO.getById(taskId);
if (ScheduleModeEnum.MANUAL == scheduleMode) {
manualRunEvenHub.notify(evenName, taskId, jobKeyName);
taskEventBus.notifyEvent(taskId, jobKeyName);
} else {
scheduleCron(jobBuilder.storeDurably(true).build(), triggerKey, task.getCronExpression());
Scheduler scheduler = schedulerFactoryBean.getScheduler();
JobDetail jobDetail = jobBuilder.storeDurably(true).build();
Trigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule(task.getCronExpression()))
.build();
try {
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (SchedulerException e) {
log.error("Quartz schedule task by expression failed, taskId: {}.",
jobDetail.getJobDataMap().get(JobExecutorService.TASK_ID), e);
throw new RuntimeException(e);
}
task.setJobKey(jobKeyName);
assignmentTaskDAO.updateById(task);
@@ -175,21 +179,4 @@ public class ScheduleService {
}
}
private void scheduleCron(JobDetail jobDetail, TriggerKey triggerKey, String cronExpression) {
Scheduler scheduler = schedulerFactoryBean.getScheduler();
Trigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
.build();
try {
scheduler.scheduleJob(jobDetail, cronTrigger);
} catch (SchedulerException e) {
log.error("Quartz schedule task by expression failed, taskId: {}.",
jobDetail.getJobDataMap().get(JobExecutorService.TASK_ID), e);
throw new RuntimeException(e);
}
}
}

View File

@@ -18,7 +18,7 @@ import lombok.Getter;
public enum LogTypeEnum {
ACCESS_LOG(1, "访问日志"),
OPERRATE_LOG(2, "操作日志"),
OPERATE_LOG(2, "操作日志"),
;
private int value;

View File

@@ -18,7 +18,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@UtilityClass
public final class ExecuteSqlUtils {
public final class DBSqlUtils {
@Data
@Builder

View File

@@ -8,15 +8,13 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.admin.util;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.gitee.dbswitch.admin.common.exception.DbswitchException;
import com.gitee.dbswitch.admin.common.response.ResultCode;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
/**
* excel工具类
@@ -24,7 +22,7 @@ import com.gitee.dbswitch.admin.common.response.ResultCode;
* @author Li Zemin
* @since 2024/4/23 16:12
*/
public final class EasyexcelUtils {
public final class ExcelUtils {
public static <T> void write(HttpServletResponse response, Class<T> clazz, List<T> list, String fileName,
String sheetName) {