zoukankan      html  css  js  c++  java
  • Quartz JobListener 任务监听器

    Quartz  Scheduler 可以对Job(任务)建立一个监听器,分别对任务执行  《之前, 之后, 取消》 3个阶段进行监听。 

    实现监听器需要实现JobListener接口,然后注册到Scheduler上就可以了。

    一:首先写一个监听器实现类

     1 package com.gary.operation.jobdemo.example1;
     2 
     3 import org.quartz.JobExecutionContext;
     4 import org.quartz.JobExecutionException;
     5 import org.quartz.JobListener;
     6 
     7 public class MyJobListener implements JobListener {
     8 
     9     @Override
    10     public String getName() {
    11         return "MyJobListener";
    12     }
    13 
    14     /**
    15      * (1)
    16      * 任务执行之前执行
    17      * Called by the Scheduler when a JobDetail is about to be executed (an associated Trigger has occurred).
    18      */
    19     @Override
    20     public void jobToBeExecuted(JobExecutionContext context) {
    21         System.out.println("MyJobListener.jobToBeExecuted()");
    22     }
    23 
    24     /**
    25      * (2)
    26      * 这个方法正常情况下不执行,但是如果当TriggerListener中的vetoJobExecution方法返回true时,那么执行这个方法.
    27      * 需要注意的是 如果方法(2)执行 那么(1),(3)这个俩个方法不会执行,因为任务被终止了嘛.
    28      * Called by the Scheduler when a JobDetail was about to be executed (an associated Trigger has occurred),
    29      * but a TriggerListener vetoed it's execution.
    30      */
    31     @Override
    32     public void jobExecutionVetoed(JobExecutionContext context) {
    33         System.out.println("MyJobListener.jobExecutionVetoed()");
    34     }
    35 
    36     /**
    37      * (3)
    38      * 任务执行完成后执行,jobException如果它不为空则说明任务在执行过程中出现了异常
    39      * Called by the Scheduler after a JobDetail has been executed, and be for the associated Trigger's triggered(xx) method has been called.
    40      */
    41     @Override
    42     public void jobWasExecuted(JobExecutionContext context,
    43             JobExecutionException jobException) {
    44         System.out.println("MyJobListener.jobWasExecuted()");
    45     }
    46 
    47 }

    二:将这个监听器注册到Scheduler

    假设有一个任务的key是 job1与 group1

    // define the job and tie it to our HelloJob class
            JobDetail job = newJob(HelloJob.class)
                .withIdentity("job1", "group1")
                .build();
    全局注册,所有Job都会起作用
    Registering A JobListener With The Scheduler To Listen To All Jobs
    sched.getListenerManager().addJobListener(new MyJobListener());
    指定具体的任务
    Registering A JobListener With The Scheduler To Listen To A Specific Job
    Matcher<JobKey> matcher = KeyMatcher.keyEquals(new JobKey("job1", "group1"));
    sched.getListenerManager().addJobListener(new MyJobListener(), matcher);
    指定一组任务
    Registering A JobListener With The Scheduler To Listen To All Jobs In a Group
    GroupMatcher<JobKey> matcher = GroupMatcher.jobGroupEquals("group1");
    sched.getListenerManager().addJobListener(new MyJobListener(), matcher);
    可以根据组的名字匹配开头和结尾或包含
    GroupMatcher<JobKey> matcher = GroupMatcher.groupStartsWith("g");
    GroupMatcher<JobKey> matcher = GroupMatcher.groupContains("g");
    sched.getListenerManager().addJobListener(new MyJobListener(), matcher);
  • 相关阅读:
    ASP.NET 2.0+GridView开发中遇到ViewState MAC验证错误
    Microsoft Robotics Studio与LEGO机器人[转]
    .net反射技术的应用—如何调用Java的COM接口[转]
    ORACLE函数大全
    BCD代码
    SQL Server 2005中Datetime类型转Varchar类型
    新任项目经理的五项必修课[转载]
    利用SqlBulkCopy实现网上答题系统[转]
    电信技术名词解释:移动IP技术综述[转]
    ASP页面中访问基于.net的COM组件[转]
  • 原文地址:https://www.cnblogs.com/daxin/p/3105830.html
Copyright © 2011-2022 走看看