Browse Source

系统:系统任务和一次性任务

4.0
郑根木 2 years ago
parent
commit
bd7cf4313f
2 changed files with 24 additions and 11 deletions
  1. +13
    -5
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/oneTimeService/OneTimeServiceFactory.java
  2. +11
    -6
      smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/oneTimeService/OnetimeTaskHelper.java

+ 13
- 5
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/oneTimeService/OneTimeServiceFactory.java View File

@@ -1,6 +1,7 @@
package cc.smtweb.system.bpm.web.sys.oneTimeService; package cc.smtweb.system.bpm.web.sys.oneTimeService;


import cc.smtweb.framework.core.common.SwConsts; import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.exception.BizException; import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.exception.SwException; import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.session.UserSession; import cc.smtweb.framework.core.session.UserSession;
@@ -36,7 +37,7 @@ public class OneTimeServiceFactory {


private Map<String, Class<? extends BaseOneTimeHandler>> mapType = new HashMap<>(); private Map<String, Class<? extends BaseOneTimeHandler>> mapType = new HashMap<>();


private OnetimeTaskHelper tasksDao = new OnetimeTaskHelper();
private OnetimeTaskHelper taskHelper = null;


//服务器编号 //服务器编号
private int runServer = SwConsts.SysParam.machineId; private int runServer = SwConsts.SysParam.machineId;
@@ -60,7 +61,14 @@ public class OneTimeServiceFactory {
} }


public OnetimeTaskHelper getTasksHelper() { public OnetimeTaskHelper getTasksHelper() {
return tasksDao;
if (taskHelper == null) {
synchronized (OnetimeTaskHelper.class) {
if (taskHelper == null) {
taskHelper = new OnetimeTaskHelper(SwConsts.SysParam.machineId, DbEngine.getInstance().findDao(OnetimeTask.class));
}
}
}
return taskHelper;
} }


/** /**
@@ -74,7 +82,7 @@ public class OneTimeServiceFactory {
* @throws Exception * @throws Exception
*/ */
public void push(String type, String key, String name, long bizId, String inparams, UserSession us) { public void push(String type, String key, String name, long bizId, String inparams, UserSession us) {
if (tasksDao.isExistByKey(type, key)) {
if (taskHelper.isExistByKey(type, key)) {
throw new BizException("该任务已经存在,不能新增!"); throw new BizException("该任务已经存在,不能新增!");
} }
OnetimeTask task = new OnetimeTask(); OnetimeTask task = new OnetimeTask();
@@ -106,7 +114,7 @@ public class OneTimeServiceFactory {
* @throws Exception * @throws Exception
*/ */
public void restart(String type, String key) { public void restart(String type, String key) {
OnetimeTask task = tasksDao.findOneByTk(type, key);
OnetimeTask task = taskHelper.findOneByTk(type, key);
if (task != null && !task.isNew()) { if (task != null && !task.isNew()) {
schedule.schedule(new OneTimeSysService(task), 1L, TimeUnit.MILLISECONDS); schedule.schedule(new OneTimeSysService(task), 1L, TimeUnit.MILLISECONDS);
} }
@@ -117,7 +125,7 @@ public class OneTimeServiceFactory {
if (schedule != null) { if (schedule != null) {
stop(); stop();
} }
List<OnetimeTask> listService = tasksDao.findWaiting();
List<OnetimeTask> listService = taskHelper.findWaiting();
schedule = Executors.newScheduledThreadPool(max_thread_size); schedule = Executors.newScheduledThreadPool(max_thread_size);
for (OnetimeTask task : listService) { for (OnetimeTask task : listService) {
schedule.schedule(new OneTimeSysService(task), 10L, TimeUnit.SECONDS); schedule.schedule(new OneTimeSysService(task), 10L, TimeUnit.SECONDS);


+ 11
- 6
smtweb-framework/bpm/src/main/java/cc/smtweb/system/bpm/web/sys/oneTimeService/OnetimeTaskHelper.java View File

@@ -16,19 +16,24 @@ import java.util.List;
public final class OnetimeTaskHelper { public final class OnetimeTaskHelper {
//服务器编号 //服务器编号
private int runServer = SwConsts.SysParam.machineId; private int runServer = SwConsts.SysParam.machineId;
private EntityDao<OnetimeTask> dao = null;//DbEngine.getInstance().findDao(OnetimeTasks.class);
private EntityDao<OnetimeTask> dao;

public OnetimeTaskHelper(int runServer, EntityDao<OnetimeTask> dao) {
this.runServer = runServer;
this.dao = dao;
}


//获取所有等待中的任务,服务启动时执行一次 //获取所有等待中的任务,服务启动时执行一次
public List<OnetimeTask> findWaiting() { public List<OnetimeTask> findWaiting() {
return dao.queryWhere(" run_server=? and statu < ?", runServer, OneTimeStatu.FAILED.value);
return dao.queryWhere(" sot_run_server=? and sot_statu < ?", runServer, OneTimeStatu.FAILED.value);
} }


public OnetimeTask findOneByTk(String type, String key) { public OnetimeTask findOneByTk(String type, String key) {
return dao.queryEntityWhere(" task_type=? and task_key=?", type, key);
return dao.queryEntityWhere(" sot_type=? and sot_code=?", type, key);
} }


public boolean isExistByKey(String type, String key) { public boolean isExistByKey(String type, String key) {
return DbEngine.getInstance().isExists("select task_id from " + EntityHelper.getSchemaTableName(OnetimeTask.ENTITY_NAME) + " where task_type=? and task_key=?", type, key);
return DbEngine.getInstance().isExists("select sot_id from " + EntityHelper.getSchemaTableName(OnetimeTask.ENTITY_NAME) + " where sot_type=? and sot_code=?", type, key);
} }


public void updateTask(OnetimeTask task) { public void updateTask(OnetimeTask task) {
@@ -51,13 +56,13 @@ public final class OnetimeTaskHelper {
@Override @Override
public void work() { public void work() {
task.setRunServer(runServer); task.setRunServer(runServer);
dao.updateEntity(task, "begin_time,end_time,statu,info,process,run_server");
dao.updateEntity(task, "sot_begin_time,sot_end_time,sot_statu,sot_info,sot_process,sot_run_server");
} }
}); });
} }
} }


public void updatProcess(OnetimeTask task) { public void updatProcess(OnetimeTask task) {
dao.updateEntity(task, "process");
dao.updateEntity(task, "sot_process");
} }
} }

Loading…
Cancel
Save