quartz2.3.0 (a) your first program Quartz

Task class

Package org.quartz.examples.example1; 

Import java.util.Date;
 Import org.slf4j.Logger;
 Import org.slf4j.LoggerFactory;
 Import the org.quartz.Job;
 Import org.quartz.JobExecutionContext;
 Import org.quartz.JobExecutionException ; 

/ ** 
  * task execution timing of the job class, need to inherit the parent class job 
 * / 
public  class HelloJob the implements job { 

    Private  static Logger LoggerFactory.getLogger the LOG = (HelloJob. class ); 

    // must be modified public constructor with no arguments 
    public HelloJob () { 
    } 

    //A simple task, when the execution of a print "the Hello World -!" 
    Public  void the Execute (JobExecutionContext context)
         throws JobExecutionException { 

        // Say to the Hello and Run the display at The World at The DATE / Time 
        log.info ( "the Hello World -!" + New new DATE ()); 
    } 

}

 

Scheduling Management

Package org.quartz.examples.example1; 

Import  static org.quartz.DateBuilder.evenMinuteDate;
 Import  static org.quartz.JobBuilder.newJob;
 Import  static org.quartz.TriggerBuilder.newTrigger;
 Import org.quartz.JobDetail;
 Import org.quartz .Scheduler;
 Import org.quartz.SchedulerFactory;
 Import org.quartz.Trigger;
 Import org.quartz.impl.StdSchedulerFactory;
 Import org.slf4j.Logger;
 Import org.slf4j.LoggerFactory; 

Import java.util.Date; 

/ ** 
 * this example demonstrates how to start and close the Quartz scheduler, as well as how to schedule jobs to run at the Quartz in.
 * / 
Public  class SimpleExample { 
    Logger the LOG = LoggerFactory.getLogger (SimpleExample. Class ); 

    public  void RUN () throws Exception { 

        // initialize a scheduling facility, and a scheduling class instantiates 
        the SchedulerFactory SchedulerFactory = new new the StdSchedulerFactory (); 
        Scheduler Scheduler = schedulerFactory.getScheduler ();
         // define a start run time: next minute 
        a Date Runtime = evenMinuteDate ( new new a Date ()); 

        // define a Job class, named the jobs that job1, and bound to a group named in group1 
        Job = newJob the JobDetail (HelloJob. class) .withIdentity ( "the jobs that job1", "group1" ) .build ();
         // instantiate a trigger named trigger1, and bound to a group named in group1, Job class operation start time Runtime (lower one minute) 
        the trigger = newTrigger the trigger () withIdentity ( "trigger1", "group1." ) .startAt (RUNTIME) .build (); 

        // tell quartz use our triggers to schedule tasks 
        scheduler.scheduleJob (job, trigger) ; 
        log.info (job.getKey () + "by will rUN aT:" + RUNTIME); 

        // start the scheduler (before the scheduled start, in fact, nothing could run) 
        scheduler.start (); 

        // wait long enough to make the scheduler has a chance to run the job finished job! 
        the try { 
            Thread.sleep ( 65L * 1000L );
        } catch (Exception e) {
        }
        // 关闭调度程序
        scheduler.shutdown(true);
    }

    public static void main(String[] args) throws Exception {
        SimpleExample example = new SimpleExample();
        example.run();
    }

}

 

Guess you like

Origin www.cnblogs.com/zhuwenjoyce/p/11073184.html