Quartz Task Scheduling

Quartz is an open source project of the OpenSymphony open source organization in the field of Job scheduling . It can be combined with J2EE and J2SE applications or used alone. Quartz can be used to create simple or complex daily schedules for running ten, hundreds, or even tens of thousands of Jobs .

 

Quartz is a task scheduling system, a system responsible for executing (or notifying) other software components when a predetermined (scheduled) time arrives.

 

Quartz his core object: Scheduler--core scheduler

            Job task

            JobDetail job description

            Trigger--trigger

pom.xml file

 

<dependency>

 

<groupId>org.quartz-scheduler</groupId>

 

<artifactId>quartz</artifactId>

 

<version>2.2.2</version>

 

</dependency>

 

<dependency>

 

    <groupId>org.springframework</groupId>

 

    <artifactId>spring-context-support</artifactId>

 

    <version>4.1.3.RELEASE</version>

 

</dependency>

 

application.xml file:

<!--Configuration task object-->
<bean id="quartzDemo" class="com.hzq.QuartzDemo"></bean>

<!--配置任务详情-->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartzDemo"></property>
<property name="targetMethod" value="run"/>
</bean>

<!--Create trigger-->
<bean id="targer" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!--Configure job object-->
<property name="jobDetail" ref= "jobDetail"/>
<!--Specify the condition of the trigger by Cron expression-->
<property name="cronExpression" value="0/3 * * * * ?"/>
</bean>

<!--创建调度者-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="targer"/>
</list>


</property>
</bean>

 

task method

public class QuartzDemo {

public void run(){
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) );
}
}

test:

public class TestQuartz {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324775049&siteId=291194637
Recommended