Quartz job scheduling (4) JobListener points detailed version Hyperanalytic

JobListener

Our jobListener its implementation class must implement the following methods:

method Explanation
getName() getName () method returns a string name for explaining the JobListener. For global registered listener, getName () is mainly used for logging, for JobListener referenced by the Job-specific, registered on JobDetail listener name must match the value returned getName () method from the listener.
jobToBeExecuted() Scheduler calls this method when JobDetail to be executed.
jobExecutionVetoed() Scheduler is about to be executed in JobDetail, but this method is called when it is rejected TriggerListener.
jobWasExecuted() Scheduler call this method after JobDetail be executed.

Then we " the Quartz scheduling (1) the rapid evolution of concepts " in an article timed Pa journalistic mission and get the hottest news tasks, for example, analyze our listener method.

1. Custom Listener interface class

public  class MyJobListener the implements JobListener { 

    @Override // corresponds to our listener named 
    public String getName () {
         return "myJobListener" ; 
    } 

    @Override 
    public  void jobToBeExecuted (the JobExecutionContext context) { 
        System.out.println (getName () + "trigger" + context.getJobDetail () getJobClass () + " some resources before the work begins execution of listening, where you can complete the task of preparation or logging." ); 
    } 

    @Override // "veto JobDetail" in Triiger is provided when listening their respective capabilities listener 
    public  void jobExecutionVetoed (the JobExecutionContext context) { 
        System.out.println ("Rejected executed, logging can be done." ); 
    } 

    @Override 
    public  void jobWasExecuted (the JobExecutionContext context, 
            JobExecutionException jobException) { 
        System.out.println (getName () + "trigger" + context.getJobDetail (). getJobClass () + "end monitor the work performed, the destruction of resources where you can do some statistical work or the results were grilled news" ); 

    } 

}

2. Register the listener in scheduler

There are two ways, one is registered as a global listener, the JobDetail effective for all, and the other is registered for the local listener for a specific JobDetail. For different versions, there are different configurations

1. Preparations

In the test, we used to work for the implementation class

public class PickNewsJob implements Job {

    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        System.out.println("在" + sdf.format(new Date()) + "扒取新闻");
    }
}

public class GetHottestJob implements Job {

    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {
        SDF SimpleDateFormat= New new SimpleDateFormat ( "HH: mm: SS" ); 
        System.out.println ( + sdf.format ( "in the" new new a Date ()) + "to generate the amount based on the amount of reading articles and reviews of our hottest articles list " ); 
    } 

}

2. 1.x version configuration

1. In + version, we can monitor the job by the following code

/ ** ******** partially disposed listener *************************************** * / 
JobListener myJobListener = new new MyJobListener (); 
pickNewsJob.addJobListener ( "myJobListener"); // name and herein myJobListener as the name getName () method 
scheduler.addJobListener (myJobListener); // Sign up for our listeners to Scheduler 
/ ** ******* *********** global listener configuration * / 
JobListener myJobListener = new new MyJobListener (); 
scheduler.addGlobalJobListener (myJobListener); // added directly as a global listeners

Here is our complete test code:

public static void main(String args[]) throws SchedulerException {
    JobDetail pickNewsJob =new JobDetail("job1", "jgroup1", PickNewsJob.class); 
    JobDetail getHottestJob =new JobDetail("job2", "jgroup2", GetHottestJob.class);
    SimpleTrigger pickNewsTrigger = new SimpleTrigger("trigger1", "group1",1,2000);
    SimpleTrigger getHottestTrigger = new SimpleTrigger("trigger2", "group2",1,3000);

    SchedulerFactory schedulerFactory = newThe StdSchedulerFactory (); 
    Scheduler Scheduler = schedulerFactory.getScheduler (); 

    / ** ******** partially disposed listener *************************************** * / 
    JobListener myJobListener = new new MyJobListener (); 
    pickNewsJob.addJobListener ( "myJobListener"); // name names here and myJobListener the getName () method as 
    scheduler.addJobListener (myJobListener); // Sign up for our listeners to Scheduler 
    / ** ******* global listeners Configuring *********** * / 
//       JobListener myJobListener new new myJobListener = ();
 //       scheduler.addGlobalJobListener (myJobListener); // added directly as a global listeners

    scheduler.scheduleJob(pickNewsJob,pickNewsTrigger);
    scheduler.scheduleJob(getHottestJob,getHottestTrigger);

    scheduler.start();

}

Now is the use of local listener configuration, run the program, the console print:

myJobListener trigger monitor started work on class tool.job.PickNewsJob, where you can do some preparatory work or resources before logging task
Pa journalistic at 11:18:31
in 11:18:31 and articles based on the amount of reading comments amount to generate a list of our hottest articles -------- from here we can see the two work is asynchronous
myJobListener trigger the end of the class tool.job.PickNewsJob monitor the work performed, where resources can be destruction or doing some news chops to take the results of the statistical work
myJobListener trigger monitor started work on the class tool.job.PickNewsJob, where you can do some preparatory work or resources to the task of recording the log before
picking journalistic at 11:18:33
myJobListener trigger the end of the class tool.job.PickNewsJob monitor the work performed, the destruction of resources where you can do some statistical work or the results were grilled news
in 11:18:34 to generate our best based on the amount of reading articles and comments amount hot list of articles
we look carefully will find that we are running two working three times, but we are in with When the trigger, repeatCount are set to 2. This shows that our task scheduler is characterized by: a main execution 1 was repeated 2 times, then were performed 3 (1 + repeatCount) times.

If we listen commented local codes, enable global monitoring, you will see the console print:

myJobListener trigger monitor started work on the class tool.job.PickNewsJob, where resources can do some preparatory work before the task or log records
before myJobListener trigger monitor started work on the class tool.job.GetHottestJob, where you can complete the task Some preparatory work or resources logging
Pa journalistic at 11:25:41
in 11:25:41 to generate our hottest list of articles based on the amount of reading articles and comments amount
myJobListener trigger the end of the implementation of the class tool.job.GetHottestJob monitor work, where you can make resource destruction or doing some news the results were grilled statistics
myJobListener trigger monitor the work of the class tool.job.PickNewsJob end of the implementation, where resources can be made destruction or do some statistical information the results were grilled work
myJobListener trigger monitor started work on the class tool.job.PickNewsJob, where resources can do some preparatory work before the task or log record
in 11:25:43 Pa journalistic
myJobListener trigger the end of the implementation of the class tool.job.PickNewsJob monitor work here can be funded Source News destruction or doing some chops to take the results of the statistical work
myJobListener trigger monitor started work on the class tool.job.GetHottestJob, where resources can do some preparatory work before the task or log recorded
in accordance with article 11:25:44 the amount of reading and comments amount to generate a list of our hottest articles
myJobListener trigger the end of the class tool.job.GetHottestJob monitor the work performed, the destruction of resources where you can do some statistical work or the results were grilled news
that we are the two tasks are listening for

3. 2.x version configuration

In the 2. + version, introduced ** org.quartz.ListenerManager and org.quartz.Matcher
** for more fine-grained configuration management for our listeners

1. ListenerManager

We add our listeners to the scheduler by ListenerManager. It JobDetail for common methods are:
1. public void addJobListener (JobListener JobListener)
add global listener, the listener will be JobDetail i.e. all this listener
2. public void addJobListener (JobListener jobListener, Matcher matcher)
was added with matching listener conditions declare our matching conditions in the matcher
3. public void addJobListener (JobListener jobListener, Matcher ... matchers)
to add conditions that came with uncertain parameters spend time with the listener
4. public boolean removeJobListener (String name)
removed JobListener by name
5. public List getJobListeners ()
Gets all the monitor
6. public JobListener getJobListener (String name)
Gets listeners by name

2. matches

We matcher so that different listeners listening for different tasks. It has achieved a lot of class, the first one by one as follows:

1. KeyMatcher<JobKey>

The JobKey JobKey matching JobDetail each has a corresponding, stored inside the JobName and to locate unique JobGroup JobDetail. It's commonly used methods are:

    / ** ********** configured Matcher method *********** * / 
    KeyMatcher <JobKey> keyMatcher = KeyMatcher.keyEquals (pickNewsJob.getKey ()); // configuration matching JobKey of keyMatcher pickNewsJob in. 

    / ** ******* use *********** * / 
    scheduler.getListenerManager () addJobListener (myJobListener, keyMatcher);. // by our listeners to complete the sentence of pickNewsJob The only listens
2. GroupMatcher

The group name information matches, it is commonly used methods are:

    GroupMatcher <JobKey> groupMatcher = GroupMatcher.jobGroupContains ( "named group1"); // contains specific string 
            GroupMatcher.groupEndsWith ( "oup1"); // in a particular end of the string 
            GroupMatcher.groupEquals ( "jgroup1"); // in a particular exact string 
            GroupMatcher.groupStartsWith ( "jgou"); // start with a specific string
3. AndMatcher

Taking an intersection of two matcher, examples are as follows:

KeyMatcher <JobKey> keyMatcher = KeyMatcher.keyEquals (pickNewsJob.getKey ()); 
GroupMatcher <JobKey> groupMatcher = GroupMatcher.jobGroupContains ( "named group1" ); 
AndMatcher <JobKey> andMatcher = AndMatcher.and (keyMatcher, groupMatcher); // simultaneously matching the two parameters satisfy
4. OrMatcher

And the matcher takes two sets, examples are as follows:

OrMatcher <JobKey> orMatcher = OrMatcher.or (keyMatcher, groupMatcher); // satisfies any one can
5. EverythingMatcher

Local global matching, which has two constructors:

EverythingMatcher.allJobs (); // matching all JobListener 
EverythingMatcher.allTriggers (); // for all matching TriggerListener

Here is our complete test sequence:

public static void main(String args[]) throws SchedulerException {
    final JobDetail pickNewsJob = JobBuilder.newJob(PickNewsJob.class)
            .withIdentity("job1", "jgroup1").build();
    JobDetail getHottestJob = JobBuilder.newJob(GetHottestJob.class)
            .withIdentity("job2", "jgroup2").build();
    SimpleTrigger pickNewsTrigger = TriggerBuilder
            .newTrigger()
            .withIdentity("trigger1","tgroup1")
            .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(2, 1)).startNow()
            .build();
    SimpleTrigger getHottestTrigger = TriggerBuilder
            .newTrigger()
            .withIdentity("trigger2","tgroup2")
            .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(2, 2)).startNow()
            .build();
    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    JobListener myJobListener = new MyJobListener();
    KeyMatcher<JobKey> keyMatcher = KeyMatcher.keyEquals(pickNewsJob.getKey());
    scheduler.getListenerManager().addJobListener(myJobListener, keyMatcher);
    scheduler.scheduleJob(pickNewsJob, pickNewsTrigger);
    scheduler.scheduleJob(getHottestJob,getHottestTrigger);
    scheduler.start();
}

Run the program, we get the following printed information:

myJobListener trigger monitor started work on the class tool.job.PickNewsJob, where resources can do some preparatory work before the task or log records
to generate our hottest articles based on the amount of reading and comment on the amount of list of articles
in 12:48: 58 Pa journalistic
myJobListener trigger monitor the work of the class tool.job.PickNewsJob end of the implementation, where resources can be made destruction or do some news chops to take the results of the statistical work
myJobListener trigger monitor started work on the class tool.job.PickNewsJob Some preparatory work resources or log in front, where you can complete the task record
in 12:48:59 Pa journalistic
myJobListener trigger monitor the work of the class tool.job.PickNewsJob end of the implementation, where resources can be made destruction or do some chops to take news the results of the statistical work
to generate our hottest list of articles based on the amount of reading articles and comments amount
obviously, myJobListener only our PickNewsJob matches.
Other configurations on the venue and test code can refer to the previous article in this series, which has detailed configuration examples to explain

Guess you like

Origin www.cnblogs.com/deityjian/p/11680784.html