【Spring】Quartz定时器动态设置定时时间

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()方法。完成以上步骤就可动态设置定时时间。



你可能感兴趣的:(Java)