quartz 可配置的定時服務

2021-07-24 02:09:12 字數 4252 閱讀 7398

一、可配置的定時服務先設計資料庫表結構

create table `job` (

`id` char(36) not null,

`name` varchar(36) not null comment '任務名',

`bean_name` varchar(100) not null comment 'spring bean name',

`execute_exp` varchar(200) not null comment '執行的表示式',

`state` int(11) not null default '1' comment '1有效2無效',

`remark` varchar(100) null default '1' comment '描述資訊',

`create_time` datetime not null,

primary key (`id`)

)

二、定義quartzmanager 管理器 可以使得注入可用

public class quartzmanager 

inputstream is = null;

try catch (exception e1)

}/**

* 新增任務

* * @param jobname

* @param triggername

* @param jobclass

* @param time

* @return

* @throws schedulerexception

* @throws parseexception

*/public static jobdetail addjob(string jobid, string triggername, string beanname, class<? extends job> jobclass,

string time) throws schedulerexception, parseexception

public static list<? extends trigger> gettriggerkeys(jobkey jobkey) throws schedulerexception

/*** 觸發乙個任務

* * @param jobkey

* @throws schedulerexception

*/public static void triggerjob(jobkey jobkey) throws schedulerexception

/*** 得到乙個觸發器的任務狀態

* * @param triggerkey

* @return

* @throws schedulerexception

*/public static triggerstate gettriggerstate(triggerkey triggerkey) throws schedulerexception

/*** 移除任務

* * @param jobname

* @param triggername

* @param jobclass

* @param time

* @return

* @throws schedulerexception

* @throws parseexception

*/public static void removejob(jobkey jobkey) throws schedulerexception

public static void clearall() throws schedulerexception

}

三、定義介面類

public inte***ce iquartzbasejobservice
四、每個新的beanname 實現 iquartzbasejobservice走自己的邏輯

五、考慮到每個介面只實現,自己的業務不能加日誌等資訊,需要定義乙個類來完成這事

public class quartzspringbeanjob implements org.quartz.job 

boolean success = true;

string message = "";

// 檢查當前job是否可以執行

ijobservice jobservice = springcontextholder.getbean("jobservice");

job jobbean = jobservice.findunique(jobid);

if (jobbean == null || jobbean.getstate() != booleanenum.true.getvalue()) catch (schedulerexception e)

return;

}ijoblogservice joblogservice = springcontextholder.getbean("joblogservice");

// 查詢上一次是否已經執行成功

boolean existsdoing = joblogservice.existsuncomplete(jobid);

// 生成job日誌

joblog joblog = new joblog();

joblog.setid(guidkeygenerator.getuuidkey());

joblog.setjobid(jobid);

joblog.setstarttime(new date());

if(!existsdoing) else

joblogservice.create(joblog);

if(existsdoing)

try

} catch (exception e)

// 更新日誌狀態

joblog newjoblog = new joblog();

newjoblog.setid(joblog.getid());

newjoblog.setstate(success ? joblogstateenum.success.getvalue() : joblogstateenum.error.getvalue());

newjoblog.setendtime(new date());

newjoblog.setmessage(message);

joblogservice.update(newjoblog, joblog);

}

六、一般spring 啟動就想執行監聽

@service

public class jobtaskservice implements ijobtaskservice, initalizer

private void startall() catch (exception e) }}

七、考慮到新增修改刪除,所以我把我用的方法列

public static void removejob(string jobname) throws schedulerexception catch (exception e)   

} public static void modifyjobtime(string triggername,

string triggergroupname, string time)

string oldtime = trigger.getcronexpression();

if (!oldtime.equalsignorecase(time))

} catch (exception e)

} public static void clearall() throws schedulerexception /**

* 觸發乙個任務

* * @param jobkey

* @throws schedulerexception

*/public static void triggerjob(jobkey jobkey) throws schedulerexception

八、總結一下 

trigger 就是個觸發器 jobdetail 就是那個類要作這件事情

scheduler 乙個工廠物件返回,作什麼事幾點作就可以了。

scheduler.start();

成了

可配置更改的定時任務

任務是配置乙個可以配置更改的定時任務用來跑批量任務進行統計。不能用整合的,只能用原生的quartz的寫法。首先是定義實體類 public class pushconfig extends basepojo 排程容器設定jobdetail和trigger jobdetail.getjobdatamap...

springmvc配置quartz定時任務

springmvc配置quartz定時任務 job1 class com.rionsoft.web.quartz.szzzptquartztask queryworkercheckondetail class org.springframework.scheduling.quartz.methodi...

quartz定時任務配置

參考 quartz是乙個強大的企業級任務排程框架,spring中繼承並簡化了quartz,下面就看看在spring中怎樣配置quartz 首先我們來寫乙個被排程的類 package com.kay.quartz public class quartzjob spring的配置檔案 work 10,1...