import org.quartz.*;//引入quartz
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("QuartzScheduler")//把当前类注入到spring中
public class QuartzScheduler {
@Autowired
private Scheduler scheduler;
private static String JOB_GROUP_NAME = "JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_NAME";
/**
* 添加一个调度任务
* @param jobname 任务名称
* @param clnm 任务执行类名称
* @param cronExpression 任务周期表达式
*/
public void addJob(String jobname,Class clnm,String cronExpression){
try{
//定义一个trigger
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobname, TRIGGER_GROUP_NAME) //定义name/group
.startNow()//一旦加入scheduler,立即生效
.withSchedule(
CronScheduleBuilder.cronSchedule(cronExpression))