Quartz timer use project

  In daily life, we will inevitably be used in some places to some regular reminders tasks, regular business logic operations. Let me tell you about a simple idea 

Quartz is
  An open source job scheduling framework , it is written entirely in Java, and is designed for J2SE and J2EE applications. It offers great flexibility without sacrificing simplicity. You can use it to create simple or complex schedules for the implementation of a job.

 

Steps for usage:

  1, it is necessary to perform regular tasks of:

public  class QuartzSrvBean {
     Private Logger Logger = LoggerFactory.getLogger (QuartzSrvBean. class ); 

    Private PreOrderMonitorService preOrderMonitorService; 

    public  void setPreOrderMonitorService ( 
            PreOrderMonitorService preOrderMonitorService) { 
        the this .preOrderMonitorService = preOrderMonitorService; 
    } 
    / ** 
     * ..... scheduled task 
     * / 
    public  void Exec () {
         // recording timing task logs 
        logger.info ( "xxxx scheduled tasks started -------------" );
         Boolean= preOrderMonitorService.execUpdateTask isComplete ( null ); 
        logger.info ( "XXXX scheduled tasks isComplete:" + isComplete); 
    } 

}

  2, import dependencies 

   <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.2.3</version>
    </dependency>

  3, the configuration 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" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">


<bean id = "quartzSrvBean"a, bean work
increase job classes//
class="com.xxx.yyy.common.QuartzSrvBean">
    <property name="preOrderMonitorService" ref="preOrderMonitorService"></property>
</bean>//执行作业的method(job)
<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="quartzSrvBean"></property>  //需要定时执行的方法
    <property name="targetMethod" value="exec"></property>
</bean>//工作的trigger
//简易
<bean id="simpleTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean "> 
        <Property name =" the startDelay "value =" 0 "/> <-! instantiated factory after scheduling, scheduling is performed through at 0s ->


<property name="jobDetail" ref="job2" /> <property name = "repeatInterval" value = "60000" /> <-! scheduling time every 60 seconds -> </ the bean> // quartz expression Corn <the bean ID = "CronTrigger" class = "org.springframework. scheduling.quartz.CronTriggerFactoryBean "> <Property name =" jobDetail "ref =" JOB2 "/> <Property name =" cronExpression "value =" 0 0/1 14 * * "/> <-?! from 14:00 start until 14:59 run once every one minute -> </ the bean> // configuration control center <the bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean"> <Property name = "Triggers"> <List> </list> </property> </bean> </beans>//解释: <ref bean="cronTrigger" />

(1), Job: represents a task (work), the specific contents to be executed.
(2), JobDetail: represents a specific executable scheduler, Job is the content scheduler executables to be executed, in addition JobDetail also includes the scheduling of programs and strategies.
(3), Trigger: on behalf of a scheduling configuration parameter, when to tune.
(4), Scheduler: scheduling on behalf of a container, the container may be registered in a plurality of scheduling JobDetail and Trigger. When Trigger JobDetail combination with, the container may be scheduled Scheduler

  4, this time the task will be based on the timing of the trigger configuration rules we have to perform regularly.

Quartz expressions tips: http: //cron.qqe2.com/

 

  5, how to monitor the Quartz job execution status: running, paused, waiting? 
By adding to the table (a new operation log table) is inserted in the form of logs:
    1) operation: Changing table information is achieved when run by JobListener listener.
    2) pause: When calling scheduler.pauseTrigger () method, changes in the job information table.
    3) Waiting: newly added job waiting to their default state, and a change in the job information table but the trouble is that the form obtained above is frequently inserted into the table data.

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/guanyuehao0107/p/11962712.html