Spring定時器動態設置定時時間
Spring Quartz定時器動態設置定時時間
1、配置Spring配置文件(frams-quartz-context.xml):
doTimingSendTimer
1 0 0 1 1 ? 2099
2、設置定時時間Action(TimingSendTimerAction)
/**
*
* 功能描述:定時下發
*
* 創建人:Gansuper
*
* 創建時間:2014-11-13 19:47
*
*/
public class TimingSendTimerAction {
/**
* Service注入
*/
@Autowired
@Qualifier("timingSendTimerService")
private TimingSendTimerService timingSendTimerService;
/**
* 功能描述:執行定時下發操作
*
* 輸入參數:
*
* 輸出參數:
*
* 創建時間:2014-11-13 19:56
*/
public void doTimingSendTimer(){
System.out.println("定時下發開始!");
this.timingSendTimerService.doTimingSendTimer();
System.out.println("定時下發完成!");
}
}
3、設置定時時間Service接口實現類
/**
* 功能說明:設置下發定時時間service接口實現類
*
* 創建人:Gansuper
*
* 創建時間:2014-11-13 19:59
*
*/
@Service("setTimingSendTimeService")
public class SetTimingSendTimeServiceImpl extends BaseServiceImpl implements SetTimingSendTimeService{
/**
* workOrderInfoDAO
*/
private TWorkOrderInfoDAO workOrderInfoDAO;
/**
* Scheduler
*/
private Scheduler scheduler;
/**
* 設置下發定時時間
*/
@Override
public void setTimingSendTime() {
try {
if (scheduler == null || workOrderInfoDAO == null) {
scheduler = (Scheduler) SpringContextUtil.getBean("strQuartz");
workOrderInfoDAO = (TWorkOrderInfoDAO) SpringContextUtil.getBean("workOrderInfoDAO");
}
CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("timingSendTimerTrigger", Scheduler.DEFAULT_GROUP);
String sendTime = this.workOrderInfoDAO.queryTimingSendTime();//獲取數據庫中的需設定的時間值
if (sendTime == null) {
return ;
}
sendTime = sendTime.substring(0, sendTime.lastIndexOf("."));
String[] sendTimes = sendTime.split(" ")[1].split(":");
String pushTime = sendTimes[2] + " " + sendTimes[1] + " " + sendTimes[0]+ " * * ? ";
trigger.setCronExpression(pushTime);//設置定時器觸發時間
scheduler.rescheduleJob("timingSendTimerTrigger",Scheduler.DEFAULT_GROUP, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
}
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public TWorkOrderInfoDAO getWorkOrderInfoDAO() {
return workOrderInfoDAO;
}
public void setWorkOrderInfoDAO(TWorkOrderInfoDAO workOrderInfoDAO) {
this.workOrderInfoDAO = workOrderInfoDAO;
}
}
4、在前台輸入需設置的時間值,並通過相關操作保存到數據庫相應的表中。在保存定時時間成功之後,需調用setTimingSendTime()方法。完成以上步驟就可動態設置定時時間。