Quartz getting started simple case, quartz getting started

Source: Internet
Author: User

Quartz getting started simple case, quartz getting started

Quartz is a job scheduling framework. It can trigger a task within the specified time.

 

Case:

The cases here are developed based on Quartz 2.2.3, Maven, and spring.

Introduce Maven-related dependencies in the pom. xml file

 

1 <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 2 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion> 4.0.0 </modelVersion> 4 <groupId> cn. wy </groupId> 5 <artifactId> FristQuartz </artifactId> 6 <version> 0.0.1-SNAPSHOT </version> 7 <! -- Define the version of the specified jar using properties --> 8 <properties> 9 <spring. version> 4.2.4.RELEASE </spring. version> 10 <slf4j. version> 1.6.6 </slf4j. version> 11 <log4j. version> 1.2.12 </log4j. version> 12 </properties> 13 14 <dependencies> 15 <dependency> 16 <groupId> org. springframework </groupId> 17 <artifactId> spring-context </artifactId> 18 <version >$ {spring. version} </version> 19 </dependency> 20 <dependency> 21 <groupId> org. springframework </ GroupId> 22 <artifactId> spring-context-support </artifactId> 23 <version >$ {spring. version }</version> 24 </dependency> 25 <dependency> 26 <groupId> org. springframework </groupId> 27 <artifactId> spring-orm </artifactId> 28 <version >$ {spring. version} </version> 29 </dependency> 30 <dependency> 31 <groupId> org. springframework </groupId> 32 <artifactId> spring-beans </artifactId> 33 <version >$ {spring. version} </version> 34 </Dependency> 35 <dependency> 36 <groupId> org. springframework </groupId> 37 <artifactId> spring-core </artifactId> 38 <version >$ {spring. version} </version> 39 </dependency> 40 <dependency> 41 <groupId> org. aspectj </groupId> 42 <artifactId> aspectjweaver </artifactId> 43 <version> 1.7.4 </version> 44 </dependency> 45 <! -- Log start --> 46 <dependency> 47 <groupId> log4j </groupId> 48 <artifactId> log4j </artifactId> 49 <version >$ {log4j. version} </version> 50 </dependency> 51 <dependency> 52 <groupId> org. slf4j </groupId> 53 <artifactId> slf4j-api </artifactId> 54 <version >$ {slf4j. version} </version> 55 </dependency> 56 57 <dependency> 58 <groupId> org. slf4j </groupId> 59 <artifactId> slf4j-log4j12 </artifactId> 60 <version >$ {slf4j. version} </Version> 61 </dependency> 62 <! -- Introduced junit support --> 63 <dependency> 64 <groupId> junit </groupId> 65 <artifactId> junit </artifactId> 66 <version> 4.10 </version> 67 <scope> test </scope> 68 </dependency> 69 70 <! -- Introduce the Z dependency --> 71 <dependency> 72 <groupId> org. quartz-scheduler </groupId> 73 <artifactId> quartz </artifactId> 74 <version> 2.2.3 </version> 75 </dependency> 76 <dependency> 77 <groupId> org. quartz-scheduler </groupId> 78 <artifactId> quartz-jobs </artifactId> 79 <version> 2.2.3 </version> 80 </dependency> 81 </dependencies> 82 </ project>

 

 

 

Step 1: Create a job class

1 package cn. wy. test; 2 3 import java. text. simpleDateFormat; 4 import java. util. date; 5/** 6 * Custom job class 7 */8 public class TestJob {9 public void run () {// method name can be any 10 System. out. println ("the custom job type is executed:" + new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "). format (new Date (); 11} 12}

Step 2: configure the applicationContext. xml file

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- Register a custom job class --> <bean id = "testJob" class = "cn. wy. test. TestJob"> </bean> <! -- Configure JobDetail --> <bean id = "jobDeteil" class = "org. springframework. scheduling. quartz. MethodInvokingJobDetailFactoryBean"> <! -- Inject the target object --> <property name = "targetObject" ref = "testJob"/> <! -- Injection target method --> <property name = "targetMethod" value = "run"/> </bean> <! -- Configure Trigger --> <bean id = "myTrigger" class = "org. springframework. scheduling. quartz. CronTriggerFactoryBean"> <! -- Inject task details object --> <property name = "jobDetail" ref = "jobDeteil"/> <! -- Inject a cronExpression expression to specify the start time --> <property name = "cronExpression"> <! -- Trigger After 3 seconds, which is equivalent to executing the run method in the TestJob class every 3 seconds --> <value> 0/3 ****? </Value> </property> </bean> <! -- Configure the scheduling factory --> <bean id = "schedulerFactoryBean" class = "org. springframework. scheduling. quartz. SchedulerFactoryBean"> <! -- Trigger injection --> <property name = "triggers"> <list> <ref bean = "myTrigger"/> <! -- You can configure multiple triggers at the same time --> </list> </property> </bean> </beans>

Step 3: Test

1 package cn.wy.test;2 3 import org.springframework.context.support.ClassPathXmlApplicationContext;4 5 public class Test {6     public static void main(String[] args) {7         new ClassPathXmlApplicationContext("applicationContext.xml");8     }9 }

Execution result:

 

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.