我在SetUp里面初始化添加了一个定时任务到SysTask中,但是启动的时候,SysTask类中的Dao注入为空,求解
初始化添加的任务
if (i == 0){
//在这里添加后台更新的任务,直接后台更新,前台不可见
//定时任务示例
Sys_task task = new Sys_task();
task.setDisabled(true);
task.setName("后台更新");
task.setJobClass("cn.ikeeper.modules.task.sys.UpdateToDB");
task.setCron("0 */1 * * * ?"); //每隔一分钟执行一次
task.setData("{\"Job\":\"update to DB\"}");
task.setNote("UpdateToDB");
dao.insert(task);
}
任务类
@IocBean
public class UpdateToDB extends SysTask {
private static final Log log = Logs.getLog(UpdateToDB.class);
@Inject
private Dao dao;
@Inject
private TbDevInstService tbDevInstService;
@Inject
private TbWdkzqExtService tbWdkzqExtService;
@Inject
private TbIotTaskService tbIotTaskService;
@Override
public void execute(Sys_task task, JobDataMap data) throws JobExecutionException {
log.debug("这是后台定时任务,用来更新数据库表的");
Sql sql = Sqls.create("SELECT wd.id FROM tb_wdkzq_ext wd, tb_iot_task iot WHERE wd.id=iot.instId AND iot.actionCode='readMeterInfo' AND iot.opAt - wd.readAt >= 60");
dao.execute(sql);
List<Integer> ids = sql.getList(Integer.class);
//List<Tb_wdkzq_ext> wdkzqExts = tbWdkzqExtService.query(Cnd.where("id","in", ids));
List<Tb_iot_task> iotTasks = tbIotTaskService.query(Cnd.where("instId","in", ids));
int i = 0;
for (Tb_iot_task iot : iotTasks){
NutMap map = getJson(iot.getRspjson());
if (map == null || map.size() == 0){
continue;
}
map.addv("opAt", iot.getOpAt());
log.debug("输出日志,这里在进行更新操作,第"+i+"次更新");
updateToDB(iot.getInstid(), map);
i++;
}
}
//rspJson格式化为NutMap
private NutMap getJson(String respJson){
int i1 = respJson.indexOf("Success");
int i2 = respJson.indexOf("body");
if (i1 * i2 < 0){
return null;
}
Object body = Mapl.cell(Json.fromJson(respJson), "body");
return Json.fromJson(NutMap.class, Json.toJson(body));
}
private void updateToDB(Integer id, NutMap map){
Tb_wdkzq_ext wdkzqExt = new Tb_wdkzq_ext();
wdkzqExt.setId(id);//设置更新的设备id
wdkzqExt.setAvgt(BigDecimal.valueOf(Double.valueOf(map.getString("avgTempr"))));//设置平均室温
wdkzqExt.setCurrt(BigDecimal.valueOf(Double.valueOf(map.getString("currTempr"))));//设置当前室温
wdkzqExt.setSett(BigDecimal.valueOf(Double.valueOf(map.getString("settingTempr"))));//设置设定温度
wdkzqExt.setSumworktime(Integer.parseInt(map.getString("workTimes")));//设置累计工作时间,单位:分钟
wdkzqExt.setSumopentime(Integer.parseInt(map.getString("openTapTimes")));//设置累计开阀时间,单位:分钟
wdkzqExt.setReadat((Date) map.get("opAt"));//设置读取设备的时间
//更新数据库
tbWdkzqExtService.update(wdkzqExt);
}
}
继承的SysTask类
public abstract class SysTask implements Job {
private static final Log log = Logs.get();
@Inject
protected Dao dao;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
String taskId = context.getJobDetail().getKey().getName();
log.debug("============================="+data);
log.debug("-----------------------------"+dao);//此处dao为空
execute(dao.fetch(Sys_task.class, taskId), data);
dao.update(Sys_task.class, Chain.make("exeAt", new Date()).add("exeResult", "执行成功"), Cnd.where("id", "=", taskId));
}
abstract void execute(Sys_task task, JobDataMap data) throws JobExecutionException;
}
另外,为什么我用GitHub总是等不上,点击登录后跳转到Github登录,然后返回社区之后无论点刷新还是登录,都没反应了