好记性不如烂博客之 Quartz How-To: Using Trigger Listeners

How-To: Using Trigger Listeners

 

Creating a TriggerListener

Implement the TriggerListener interface.

package foo;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;
import org.quartz.Trigger.CompletedExecutionInstruction;

public class MyTriggerListener implements TriggerListener {

    private String name;

    public MyTriggerListener(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public void triggerComplete(Trigger trigger, JobExecutionContext context,
            CompletedExecutionInstruction triggerInstructionCode) {
        // do something with the event
        
    }

    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        // do something with the event
    }

    public void triggerMisfired(Trigger trigger) {
        // do something with the event
    }

    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        // do something with the event
        return false;
    }

}

OR -

Extend TriggerListenerSupport.

package foo;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.listeners.TriggerListenerSupport;

public class MyOtherTriggerListener extends TriggerListenerSupport {

    private String name;

    public MyOtherTriggerListener(String name) {
        this.name = name;
    }
   
    public String getName() {
        return name;
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        // do something with the event
    }
}

Registering A TriggerListener With The Scheduler To Listen To All Triggers

scheduler.getListenerManager().addTriggerListener(myTriggerListener, allTriggers());

Registering A TriggerListener With The Scheduler To Listen To A Specific Trigger

scheduler.getListenerManager().addTriggerListener(myTriggerListener, triggerKeyEquals(triggerKey("myTriggerName", "myTriggerGroup")));

Registering A TriggerListener With The Scheduler To Listen To All Triggers In a Group

scheduler.getListenerManager().addTriggerListener(myTriggerListener, triggerGroupEquals("myTriggerGroup"));

 

posted @ 2013-05-31 16:47  大新博客  阅读(461)  评论(0编辑  收藏  举报