Quartz call Daquan

Source: Internet
Author: User

Quartz call Daquan

 

1. quartz is widely used and can be executed independently or embedded in spring.

Similar scheduled tasks can be executed using crontab in Linux.

 

 

2. Implementation Code:

Quartztest: Main execution class

Package COM. taobao. terminator. allen. quartztest; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import Org. quartz. crontrigger; import Org. quartz. jobdetail; import Org. quartz. joblistener; import Org. quartz. scheduler; import Org. quartz. schedulerfactory; import Org. quartz. impl. stdschedulerfactory; public class quartztest {private final static log logger = logfactory. getlog (Quartztest. class); public static void main (string [] ARGs) {system. out. println ("this is test for quartz"); // initialize scheduler factory class schedulerfactory factory = new stdschedulerfactory (); try {// directschedulerfactory to generate an instance, create a shced1_sched1_shced1_= factory. getscheduler (); // Z listener, divided into global and local. Partial return must be performed in the specified jobdetail. joblistener = new quartzlistener ("quartz-test"); shcedener is not required globally. addjoblistener (joblistener); // The shceduler context is similar to the servlet context and has similar functions. context will be passed to the listener shced.pdf. getcontext (). put ("corename", "corename"); // You can also save the key-value and jobdetail of context. getjobdatamap () jobdetail = new jobdetail ("full-detail", "full-detail-group", quartzjob. class); jobdetail. addjob Listener ("quartz-test"); jobdetail. getjobdatamap (). put ("jobdetail-Data", "jobdetail"); // generate the trigger crontrigger trigger = new crontrigger ("full-Trigger", "full-trigger-group"); trigger. setcronexpression ("0/30 ****? "); // Add job shcedtasks. schedulejob (jobdetail, trigger); // start to execute shceduler. start (); While (true) {thread. sleep (1000*10); system. out. println ("trigger scheduled task"); shcedtasks. triggerjob ("full-detail", "full-detail-group"); thread. sleep (1000*40); // terminate the running job shcedtasks. interrupt ("full-detail", "full-detail-group"); system. out. println ("the schedule is over"); // disable the timer shceduler. shutdown (); break;} catch (exception e) {logger. error ("failed to generate the debugger", e );}}}

Quartzlistener: Listener, which can be set at three levels: jobdetail, trigger, and schedener

Mainly performs operations before and after the job is executed. context can communicate with each other and perform exceptions, similar to the Context Function of servlet.

package com.taobao.terminator.allen.QuartzTest;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.JobListener;public class QuartzListener implements JobListener{    private String listenerName = "quartz-listener";        public QuartzListener(String listenerName) {        this.listenerName = listenerName;    }        public String getName() {        return this.listenerName;    }    public void jobToBeExecuted(JobExecutionContext context) {        System.out.println("the job listener is start");            }    public void jobExecutionVetoed(JobExecutionContext context) {    }    public void jobWasExecuted(JobExecutionContext context,            JobExecutionException jobException) {        System.out.println("the job listener is end");    }}

Quartzjob: Pay attention to the implementation of the interrupt method. It can be used to stop the current job or be deleted directly using shcedule. deletejob (name, groupname ).

import org.quartz.InterruptableJob;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.UnableToInterruptJobException;public class QuartzJob implements InterruptableJob{    private boolean stop = false ;    public void execute(JobExecutionContext context) throws JobExecutionException {        if(!this.stop) {            System.out.println("this is ok");        }    }    public void interrupt() throws UnableToInterruptJobException {        this.stop = true ;        System.out.println("it is over");    }        public boolean isStop() {        return stop;    }}

From: http://blog.csdn.net/flyingpig4/article/details/6263560

Quartz call Daquan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.