码迷,mamicode.com
首页 > 其他好文 > 详细

Quartz--Listener

时间:2014-12-17 20:56:22      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:quartz   listener   

监听器方便在Trigger执行完毕,Trigger执行错误等地方进行监听处理。

下面代码举个SchedulerListener的例子: 在Trigger最后一次执行的时候监听

<span style="font-family:Microsoft YaHei;font-size:18px;">import javax.annotation.Resource;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerListener;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;



/**
 * 
 * @ClassName: SchedulerTriggerListener 
 * @Description: Trigger监听器 
 * @author zhuyb 
 * @date 2014-12-15 下午7:39:28 
 *
 */
@Service("schedulerTriggerListenerService")
public class SchedulerTriggerListenerServiceImpl implements SchedulerListener {

	private static Logger logger = LoggerFactory.getLogger(SchedulerTriggerListenerServiceImpl.class);
	
	
	@Resource
	private SchedulerTriggersService schedulerTriggersService;
    
	@Resource
	private Scheduler scheduler;

	@Override
	public void jobScheduled(Trigger trigger) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void jobUnscheduled(TriggerKey triggerKey) {
		// TODO Auto-generated method stub
		
	}

    /**
     * 
     * @Title: triggerFinalized 
     * @Description: 轮询删除已被Quartz删除的内容 
     * @param @param trigger     
     * @author zhuyb
     * @throws
     */
	@Override
	public void triggerFinalized(Trigger trigger) {
		try {
			if (trigger.getNextFireTime() == null) {
			    logger.info("Name="+trigger.getKey().getName()+ ", Group=" + trigger.getKey().getGroup());
				int i = schedulerTriggersService.deleteByNotExistInTriggers();
				logger.info("删除行数:" + i);
			}
		} catch (Exception e) {
		    logger.error("监听器错误:",e);
		}
	}

	@Override
	public void triggerPaused(TriggerKey triggerKey) {
		
	}


	@Override
	public void triggersPaused(String triggerGroup) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void triggerResumed(TriggerKey triggerKey) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void triggersResumed(String triggerGroup) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void jobAdded(JobDetail jobDetail) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void jobDeleted(JobKey jobKey) {
	}


	@Override
	public void jobPaused(JobKey jobKey) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void jobsPaused(String jobGroup) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void jobResumed(JobKey jobKey) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void jobsResumed(String jobGroup) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void schedulerError(String msg, SchedulerException cause) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void schedulerInStandbyMode() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void schedulerStarted() {
	}


	@Override
	public void schedulerStarting() {
		
	}


	@Override
	public void schedulerShutdown() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void schedulerShuttingdown() {
		
	}


	@Override
	public void schedulingDataCleared() {
		// TODO Auto-generated method stub
		
	}

</span>
其实可以不必实现上述接口(有一堆你不需要的方法要重写), 可以用SchedulerListenerSupport替代,只需要实现需要的方法。


监听注册:

        @Resource
	private Scheduler scheduler;
	@Resource
	private SchedulerTriggerListenerServiceImpl schedulerTriggerListener;
        scheduler.getListenerManager().addSchedulerListener(schedulerTriggerListener);
以上就是一个监听器在Spring的完整集成。


具体很多其他的监听功能看业务需求吧

官方API差不多够用了:

bubuko.com,布布扣



Quartz--Listener

标签:quartz   listener   

原文地址:http://blog.csdn.net/wenniuwuren/article/details/41986167

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!