public void toggleTrigger(String triggerName,String group ){
if( StringUtil.clean(triggerName)==null )
return;
String cronExpression = null,description;
JobInterval jobInterval = jobIntervalManager.get( triggerName );
if( jobInterval != null ){
jobInterval.setEnabled( Boolean.valueOf( !jobInterval.isEnabled() ) );
cronExpression = StringUtil.clean(jobInterval.getCronExpression());
}
Trigger target = null;
try {
target = scheduler.getTrigger(TriggerKey.triggerKey(triggerName,group));
} catch (SchedulerException e) {
log.warn("获取定时任务[{}]异常:{}",triggerName,e.getMessage());
throw new ApplicationException( e );
}
if( target == null )
throw new ApplicationException( "找不到相关定时任务["+triggerName+"]" );
description = target.getDescription();
boolean enabled = false;
try {
TriggerState state = scheduler.getTriggerState( target.getKey() );
if( target instanceof CronTrigger){
cronExpression = ((CronTrigger)target).getCronExpression();
}
if( TriggerState.PAUSED.equals(state) ){
scheduler.resumeTrigger( target.getKey() );//启动定时器
enabled = true;
}else if( TriggerState.NORMAL.equals(state) ){
scheduler.pauseTrigger(target.getKey());//暂停定时器
}
} catch (SchedulerException e) {
e.printStackTrace();
}
if( jobInterval == null ){
jobInterval = new JobInterval();
jobInterval.setCronExpression(cronExpression);
jobInterval.setTriggerName(triggerName);
jobInterval.setDescription(description);
}
jobInterval.setEnabled( enabled );
jobIntervalManager.save(jobInterval);
}