代码重构:增强代码可读性和维护性
重构QuartzJobFactory类以提高代码清晰度和可维护性。调整代码格式,优化if条件语句,并精简方法参数对齐。通过这些改进,增强了代码的可读性和逻辑流程。此外,确保代码注释详细,提升文档质量。
This commit is contained in:
parent
955e840eb2
commit
4feba7099f
|
@ -36,6 +36,7 @@ public class QuartzJobFactory implements Job {
|
|||
private IIntegrationTaskLogDao integrationTaskLogDao;
|
||||
@Resource
|
||||
private ISysApplicationDatabaseDao sysApplicationDatabaseDao;
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
//当前开始时间
|
||||
|
@ -57,25 +58,24 @@ public class QuartzJobFactory implements Job {
|
|||
String integrationTaskLivingId = jobDetails.getString("integrationTaskLivingId");
|
||||
IntegrationTaskLivingEntity integrationTaskLivingEntity = iIntegrationTaskCacheableService.getIntegrationTaskLivingEntity(integrationTaskLivingId);
|
||||
|
||||
if(integrationTaskEntity == null || integrationTaskEntity.getId() == null || "".equals(integrationTaskEntity.getId())){
|
||||
if (integrationTaskEntity == null || integrationTaskEntity.getId() == null || "".equals(integrationTaskEntity.getId())) {
|
||||
logger.error("当前任务不存在,请联系管理员");
|
||||
return;
|
||||
}
|
||||
if(integrationTaskLivingEntity == null || integrationTaskLivingEntity.getId() == null || "".equals(integrationTaskLivingEntity.getId())){
|
||||
if (integrationTaskLivingEntity == null || integrationTaskLivingEntity.getId() == null || "".equals(integrationTaskLivingEntity.getId())) {
|
||||
logger.error("当前任务实例不存在,请联系管理员");
|
||||
return;
|
||||
}
|
||||
if(integrationTaskLivingEntity.getOldStartTime() != null ){
|
||||
if (integrationTaskLivingEntity.getOldStartTime() != null) {
|
||||
oldStartTime = integrationTaskLivingEntity.getOldStartTime();
|
||||
}
|
||||
|
||||
//判断任务是否启用,存在不启用情况,需要手动把任务关闭
|
||||
if(integrationTaskEntity.getTaskStatus() != null && !"1".equals(integrationTaskEntity.getTaskStatus())){
|
||||
saveLivingAndDetail(integrationTaskLivingEntity,null,
|
||||
"4","4","当前任务已停止,不执行任务",startTime,startTime,new Date());
|
||||
if (integrationTaskEntity.getTaskStatus() != null && !"1".equals(integrationTaskEntity.getTaskStatus())) {
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, null, "4", "4", "当前任务已停止,不执行任务", startTime, startTime, new Date());
|
||||
JSONObject stopJson = new JSONObject();
|
||||
stopJson.put("jobName",integrationTaskEntity.getTaskName());
|
||||
stopJson.put("jobCode",integrationTaskEntity.getTaskCode());
|
||||
stopJson.put("jobName", integrationTaskEntity.getTaskName());
|
||||
stopJson.put("jobCode", integrationTaskEntity.getTaskCode());
|
||||
QuartzJobFactoryUtils.pauseTrigger(stopJson);
|
||||
return;
|
||||
}
|
||||
|
@ -103,9 +103,8 @@ public class QuartzJobFactory implements Job {
|
|||
iIntegrationTaskCacheableService.updateIntegrationTaskLivingEntity(integrationTaskLivingEntity);
|
||||
//判断插件是否启用
|
||||
SysApplicationPluginEntity sysApplicationPluginEntity = iIntegrationTaskCacheableService.getSysApplicationPluginEntity(integrationTaskEntity.getTaskPlugin());
|
||||
if(sysApplicationPluginEntity == null || sysApplicationPluginEntity.getPluginCode() == null || !"1".equals(sysApplicationPluginEntity.getPluginStatus())){
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity,
|
||||
"2","3","插件未启用,不执行任务",startTime,startTime,new Date());
|
||||
if (sysApplicationPluginEntity == null || sysApplicationPluginEntity.getPluginCode() == null || !"1".equals(sysApplicationPluginEntity.getPluginStatus())) {
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity, "2", "3", "插件未启用,不执行任务", startTime, startTime, new Date());
|
||||
return;
|
||||
}
|
||||
String sourceCode = null;
|
||||
|
@ -114,68 +113,62 @@ public class QuartzJobFactory implements Job {
|
|||
sysApplicationDatabaseEntity.setAppId(sysApplicationPluginEntity.getAppId());
|
||||
sysApplicationDatabaseEntity.setSts("Y");
|
||||
List<SysApplicationDatabaseEntity> sysApplicationDatabaseEntities = sysApplicationDatabaseDao.queryBase(sysApplicationDatabaseEntity);
|
||||
if(sysApplicationDatabaseEntities != null && sysApplicationDatabaseEntities.size() == 1){
|
||||
if (sysApplicationDatabaseEntities != null && sysApplicationDatabaseEntities.size() == 1) {
|
||||
sourceCode = sysApplicationDatabaseEntities.get(0).getSourceCode();
|
||||
}
|
||||
|
||||
//查找插件
|
||||
PluginBaseEntity pluginBaseEntity = PluginUtils.getPluginsById(sysApplicationPluginEntity.getPluginCode());
|
||||
if(pluginBaseEntity == null ){
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity,
|
||||
"2","3","插件未找到,不执行任务",startTime,startTime,new Date());
|
||||
if (pluginBaseEntity == null) {
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity, "2", "3", "插件未找到,不执行任务", startTime, startTime, new Date());
|
||||
return;
|
||||
}
|
||||
//调用插件
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("startTime",oldStartTime);
|
||||
req.put("endTime",startTime);
|
||||
req.put("sourceCode",sourceCode);
|
||||
req.put("startTime", oldStartTime);
|
||||
req.put("endTime", startTime);
|
||||
req.put("sourceCode", sourceCode);
|
||||
try {
|
||||
JsonResultEntity jsonResultEntity = pluginBaseEntity.executeBusiness(req);
|
||||
if(jsonResultEntity.isFlag()){
|
||||
if (jsonResultEntity.isFlag()) {
|
||||
//写日志,修改任务实例
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity,
|
||||
"2","2",jsonResultEntity.getMsg(),startTime,startTime,new Date());
|
||||
}else {
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity, "2", "2", jsonResultEntity.getMsg(), startTime, startTime, new Date());
|
||||
} else {
|
||||
//写日志,修改任务实例
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity,
|
||||
"3","3",jsonResultEntity.getMsg(),startTime,startTime,new Date());
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity, "3", "3", jsonResultEntity.getMsg(), startTime, startTime, new Date());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity,
|
||||
"3","3","插件执行失败,请联系管理员",startTime,startTime,new Date());
|
||||
logger.error("插件执行失败:"+e.getMessage());
|
||||
saveLivingAndDetail(integrationTaskLivingEntity, integrationTaskLogEntity, "3", "3", "插件执行失败,请联系管理员", startTime, startTime, new Date());
|
||||
logger.error("插件执行失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Author lvleigang
|
||||
* @Description 保存任务日志
|
||||
* @Date 3:07 下午 2024/5/7
|
||||
* @param integrationTaskLivingEntity 任务实例
|
||||
* @param livingtype 1、执行中2、待执行3、错误 4、当前任务已停止
|
||||
* @param detailtype 1、执行中2、执行成功 3、执行失败 4\当前任务已停止
|
||||
* @param livingtype 1、执行中2、待执行3、错误 4、当前任务已停止
|
||||
* @param detailtype 1、执行中2、执行成功 3、执行失败 4\当前任务已停止
|
||||
* @param msg
|
||||
* @return void
|
||||
* @Author lvleigang
|
||||
* @Description 保存任务日志
|
||||
* @Date 3:07 下午 2024/5/7
|
||||
**/
|
||||
private void saveLivingAndDetail(IntegrationTaskLivingEntity integrationTaskLivingEntity,
|
||||
IntegrationTaskLogEntity integrationTaskLogEntity,
|
||||
String livingtype,String detailtype, String msg,Date oldStartTime,Date startTime,Date endTime) {
|
||||
private void saveLivingAndDetail(IntegrationTaskLivingEntity integrationTaskLivingEntity, IntegrationTaskLogEntity integrationTaskLogEntity, String livingtype, String detailtype, String msg, Date oldStartTime, Date startTime, Date endTime) {
|
||||
Long interval = (endTime.getTime() - startTime.getTime()) / (1000); // 计算间隔天数
|
||||
integrationTaskLivingEntity.setTaskStatus(livingtype);//待执行
|
||||
integrationTaskLivingEntity.setOldStartTime(oldStartTime);
|
||||
integrationTaskLivingEntity.setStartTime(startTime);
|
||||
integrationTaskLivingEntity.setEndTime(endTime);
|
||||
integrationTaskLivingEntity.setDiffTime(interval.toString());
|
||||
integrationTaskLivingEntity.setRunNum(integrationTaskLivingEntity.getRunNum()+1);
|
||||
integrationTaskLivingEntity.setRunNum(integrationTaskLivingEntity.getRunNum() + 1);
|
||||
integrationTaskLivingEntity.setModify_time(new Date());
|
||||
// iIntegrationTaskCacheableService.updateIntegrationTaskLivingEntity(integrationTaskLivingEntity);
|
||||
if(integrationTaskLogEntity != null ){
|
||||
iIntegrationTaskCacheableService.updateIntegrationTaskLivingEntity(integrationTaskLivingEntity);
|
||||
if (integrationTaskLogEntity != null) {
|
||||
integrationTaskLogEntity.setEndTime(endTime);
|
||||
integrationTaskLogEntity.setDiffTime(interval.toString());
|
||||
integrationTaskLogEntity.setTaskStatus(detailtype);//执行成功
|
||||
integrationTaskLogEntity.setRemark(msg);
|
||||
// integrationTaskLogDao.update(integrationTaskLogEntity);
|
||||
integrationTaskLogDao.update(integrationTaskLogEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue