quartz-Job scheduling Framework _quartz

Source: Internet
Author: User
Tags static class websphere application server
Brief introduction

Quartz is an open source job scheduling framework that provides a simple but powerful mechanism for job scheduling in Java applications. Quartz allows developers to schedule jobs based on time intervals (or days). It implements a many-to-many relationship between a job and a trigger, and it can associate multiple jobs with different triggers. An application that consolidates Quartz can reuse jobs from different events, and can combine multiple jobs for one event. Although you can configure Quartz by using the properties file, which can specify the JDBC transaction's data source, global job and/or trigger listeners, plug-ins, thread pools, and more, it is not integrated with the context or reference of the application server at all. The result is that the job does not have access to the internal functions of the WEB server; For example, when using the WebSphere application server, jobs scheduled by Quartz do not affect the dynamic caching and data sources of the server. Use Prelude

1. Create a new MAVEN project (either Web or normal Java project) to add dependencies on.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    < quartz.version>2.2.0</quartz.version>
</properties>

<dependencies> ...
.
<!--quartz dependency-->
      <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz-jobs</artifactId>
          <version>${quartz.version}</version>
      </dependency>
      <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz</artifactId>
          <version>${quartz.version}</version>
      </dependency>
</dependencies>

2. Under Resources, create a new configuration file Quartz.properties, add the following:

Org.quartz.scheduler.instanceName = Myscheduler #指定调度器的名称
org.quartz.threadPool.threadCount = 3 #线程池中最多同时有3个线程运行
Org.quartz.jobStore.class = Org.quartz.simpl.RAMJobStore #指定Quartz的数据 (Job and trigger information) storage location, Ramjobstore refers to memory
Start a scheduler
Package Com.netease.test.quartz;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;

Import Org.quartz.impl.StdSchedulerFactory; /** * user:hzwangxx * date:14-2-26 * time:0:16/public class Quartztest {public static void main (string[] Ar GS) {try {//1. Get a Task scheduler from the Stdschedulerfactory factory Scheduler Scheduler = stdschedulerfactory

            . Getdefaultscheduler (); 2.
            Start scheduler Scheduler.start ();

            System.out.println ("Scheduler is start ...");
        Close scheduler Scheduler.shutdown ();  catch (Schedulerexception e) {e.printstacktrace (); To change body the catch statement use File | Settings |
        File Templates. 
}}/* console output:2014-02-26 00:22:31,744 0 [main] info-using default implementation for Threadexecutor 2014-02-26 00:22:31,766 [main] info-initialized Scheduler signaller of Type:class Org.quartz.core.SchedulerSigna Lerimpl 2014-02-26 00:22:31,767 [main] Info-quartz Scheduler v.2.2.0 created.
2014-02-26 00:22:31,768 [main] info-ramjobstore initialized. 2014-02-26 00:22:31,769 [main] Info-scheduler Meta-data:quartz Scheduler (v2.2.0) ' Myscheduler ' with Instanceid '
  Non_clustered ' Scheduler class: ' Org.quartz.core.QuartzScheduler '-running locally.
  Not started.
  Currently in standby mode.
  Number of jobs executed:0 Using thread pool ' org.quartz.simpl.SimpleThreadPool '-with 3 threads. Using job-store ' Org.quartz.simpl.RAMJobStore '-which does not support persistence.

and is not clustered. 2014-02-26 00:22:31,769 [main] Info-quartz scheduler ' Myscheduler ' initialized from default resource file in quart Z Package: ' quartz.properties ' 2014-02-26 00:22:31,769 [main] Info-quartz scheduler version:2.2.0 2014-02-26 00:2
2:31,769 [main] Info-scheduler myscheduler_$_non_clustered started.
Scheduler is start ... 2014-02-26 00:22:31,769 [main] Info-scheduler MYscheduler_$_non_clustered shutting down.
2014-02-26 00:22:31,770 [main] info-scheduler myscheduler_$_non_clustered paused.
2014-02-26 00:22:31,770 [main] info-scheduler myscheduler_$_non_clustered shutdown complete. */
  Simple HelloWorld
Package Com.netease.test.quartz;
Import org.quartz.*;

Import Org.quartz.impl.StdSchedulerFactory;

Import Java.util.Date;
Import static Org.quartz.JobBuilder.newJob;
Import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

Import static Org.quartz.TriggerBuilder.newTrigger; /** * user:hzwangxx * date:14-2-26 * time:0:16/public class Quartztest {public static void main (string[] Ar GS) {try {//1. Get a Task scheduler from the Stdschedulerfactory factory Scheduler Scheduler = stdschedulerfactory

            . Getdefaultscheduler (); 2.
            Start scheduler Scheduler.start ();
            System.out.println ("Scheduler is start ..."); 3. Add timed Task//3.1 Define job Jobdetail job = Newjob (Hellojob.class). Withidentity ("Job

            1 "," group1 "). Build (); 3.2 Define Trigger, make the job run now, run every 3s, repeat 5 times, withrepeatcount (number) Set the job to run for number+1 Trigger Trigger = NE
       Wtrigger ()             . Withidentity ("Trigger1", "group1"). Startnow (). Withschedule (Simples
                    Chedule (). Withintervalinseconds (3). Withrepeatcount (4))

            . build ();
            3.3 To Scheduler to dispatch scheduler.schedulejob (job, trigger); 4.
        Close scheduler//scheduler.shutdown ();
        catch (Schedulerexception e) {e.printstacktrace (); }/** * Custom job, implement job interface and implement Execute method/public static class Hellojob implements job{Publi c void Execute (Jobexecutioncontext context) throws Jobexecutionexception {System.out.println ("the Execute job at
        "+ new Date () +" by trigger "+ Context.gettrigger (). Getjobkey ()); 
}}/* console output:2014-02-26 01:05:25,766 0 [main] info-using default implementation for Threadexecutor 2014-02-26 01:05:25,794 [main] info-initialized Scheduler SignallEr of type:class org.quartz.core.SchedulerSignalerImpl 2014-02-26 01:05:25,795 [main] Info-quartz Scheduler
.0 created.
2014-02-26 01:05:25,797 [main] info-ramjobstore initialized. 2014-02-26 01:05:25,798 [main] Info-scheduler Meta-data:quartz Scheduler (v2.2.0) ' Myscheduler ' with Instanceid '
  Non_clustered ' Scheduler class: ' Org.quartz.core.QuartzScheduler '-running locally.
  Not started.
  Currently in standby mode.
  Number of jobs executed:0 Using thread pool ' org.quartz.simpl.SimpleThreadPool '-with 3 threads. Using job-store ' Org.quartz.simpl.RAMJobStore '-which does not support persistence.

and is not clustered. 2014-02-26 01:05:25,798 [main] Info-quartz scheduler ' Myscheduler ' initialized from default resource file in quart Z Package: ' quartz.properties ' 2014-02-26 01:05:25,799 [main] Info-quartz scheduler version:2.2.0 2014-02-26 01:0
5:25,799 [main] Info-scheduler myscheduler_$_non_clustered started. Scheduler IS start ... execute job at Wed Feb 01:05:25 CST 2014 to trigger GROUP1.JOB1 execute job at Wed Feb CST 2014 By trigger Group1.job1 execute job at the Wed Feb 01:05:31 CST 2014 by Trigger Group1.job1 execute job at Wed Feb 26 01:05: CST 2014 by Trigger Group1.job1 execute job at Wed Feb-CST 01:05:37 by 2014 Trigger *
API description

Several classes of the core of the quartz framework are as follows: Scheduler-the primary API that interacts with Scheduler, and the entire lifecycle is created by Schedulerfactory to invoke the shutdown () method Job- The custom task interface that you want the scheduler to schedule, mainly implements the Execute method of the interface. Jobdetail-Define job-related information Trigger-triggers that define scheduler scheduling tasks, mainly describe the execution time of tasks, cycle information Jobbuilder-to define and create Jobdetail instance Triggerbuilder-to define and create Trigger instance

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.