Status of Quartz task scheduling Job

Stateful job and stateless job

@Use of PersistJobDataAfterExecution annotation

A stateful job can be understood as holding some state information during multiple job calls, which is stored in the jobdatamap. The default stateless job creates a new jobdatamap every time it is called

Add setter and getter methods for count

public Integer getCount() {
    return count;
}
 public void setCount(Integer count) {
        this.count = count;
    }
     public Integer getCount() {
        return count;
    }

Add in the execute method

public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    ++count;
         //Output the value of count
        System.out.println("count Value:"+count);
        //Store the value of count in the jobDataMap
       jobExecutionContext.getJobDetail().getJobDataMap().put("count",count );
}

The HelloJob class does not add the @ PersistJobDataAfterExecution annotation. A new jobDataMap will be created each time it is called and will not be accumulated;

The HelloJob class adds the @ PersistJobDataAfterExecution annotation, which can hold some information when calling multiple jobs, and can realize the accumulation of count
Two maven dependencies of quartz need to be imported for environment preparation and task scheduling

 <!--quzrtz Task scheduling core package-->
<dependency>
  <groupId>org.quartz-scheduler</groupId>
  <artifactId>quartz</artifactId>
  <version>2.3.2</version>
</dependency>
<!--Kit is optional-->
<dependency>
  <groupId>org.quartz-scheduler</groupId>
  <artifactId>quartz-jobs</artifactId>
  <version>2.3.2</version>
</dependency>

Log framework used

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.2</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>

First, create a class to implement the Job interface, which is the completed task

package com.quartz;

import javafx.scene.input.DataFormat;
import org.quartz.*;

import javax.naming.Context;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * package_name:com.quartz
 * Author:Xu Ya Yuan
 * Date:2020/1/14 14:20
 * Project Name: Quartz one
 * Desription:
 **/
@PersistJobDataAfterExecution //When a job is called multiple times, some data will be persisted in the job
 public class HelloJob implements Job {
    /*  public  HelloJob(){
          System.out.println("hello world");
      }*/

    public String getMessage() {
        return message;
    }

    public Integer getCount() {
        return count;
    }

    private String message;
    private Integer count;

    public void setCount(Integer count) {
        this.count = count;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //Get the contents of JobDetail
        JobKey jobKey = jobExecutionContext.getJobDetail().getKey();
        System.out.println("Name of task:" + jobKey.getName() + ";Name of the task force:" + jobKey.getGroup());
        System.out.println("Name of task class:(Tape path)" + jobExecutionContext.getJobDetail().getJobClass().getName());
        System.out.println("Name of task class:" + jobExecutionContext.getJobDetail().getJobClass().getSimpleName());
        //Get jobDataMap data from JobDetail object
        /*JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        System.out.println("messageJobDetail:" + jobDataMap.getString("message"));*/
        //Get Trigger content
        TriggerKey triggerKey = jobExecutionContext.getTrigger().getKey();
        System.out.println("Trigger name:" + triggerKey.getName() + ";Name of departure group:" + triggerKey.getGroup());
        System.out.println("Name of trigger class(Tape path):" + jobExecutionContext.getTrigger().getClass().getName());
        System.out.println("Name of trigger class:" + jobExecutionContext.getTrigger().getClass().getSimpleName());
        //Get jobDataMap data from Trigger object
       /* JobDataMap triggerDataMap = jobExecutionContext.getTrigger().getJobDataMap();
        System.out.println("messageTrigger:"+triggerDataMap.getString("message"));*/
        System.out.println("message information:" + message); //Note here: if you encounter a key with the same name, the. usingData("message", "print log Trigger") in the Trigger
        // Overwriting. usingData("message", "print log jobDetail") in jobDetail
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = simpleDateFormat.format(date);
        System.out.println("Database backup in progress:Database backup time:" + dateString);
        //Get more
        System.out.println("Execution time of current task:" + simpleDateFormat.format(jobExecutionContext.getFireTime()));
        System.out.println("Execution time of next task:" + simpleDateFormat.format(jobExecutionContext.getNextFireTime()));
         ++count;
         //Output the value of count
        System.out.println("count Value:"+count);
        //Store the value of count in the jobDataMap
       jobExecutionContext.getJobDetail().getJobDataMap().put("count",count );

    }
}

In the implementation of a main method, there are Scheduler, task instance JobDetail and Trigger trigger

package com.scheduler;

import com.quartz.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.net.StandardSocketOptions;

/**
 * package_name:com.scheduler
 * Author:Xu Ya Yuan
 * Date:2020/1/14 14:29
 * Project Name: Quartz one
 * Desription: quartz task scheduling
 **/
public class HelloSchedulerDemo {
    public static void main(String args[]) throws Exception {
        //Scheduler, get the scheduling instance from the factory,
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //Task instance (JobDetail)
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) //Loading the task class to complete the binding with HelloJob requires that HelloJob must implement the Job interface
                .withIdentity("job","group1") //Name of parameter 1 task name of parameter 2 task group
                .usingJobData("message","Print log jobDetail") //Transfer parameters
                .usingJobData("count",0 ) //Judging whether a job has a state or not
                .build();
        /*System.out.println("Task name: "+ jobDetail.getKey().getName()); / / output task name
        System.out.println("The name of the group: '+ jobDetail.getKey().getGroup()); / / the name of the output task group does not specify the DEFAULT default DEFAULT
        System.out.println("Name of task class: "+ jobDetail.getClass().getName());*/
        // Name of task group
        //Trigger
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1") //Name of parameter 1 trigger name of parameter 2 trigger group
                .startNow()  //Start trigger now
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForever(5))
                .usingJobData("message","Print log Trigger")//Transfer parameters
                .build();
       /* System.out.println("Trigger name: "+ trigger.getKey().getName());
        System.out.println("Trigger group name: '+ trigger.getKey().getGroup()); / / the output trigger group name does not specify DEFAULT default
        System.out.println("Trigger class name: "+ trigger.getClass().getName());*/
        //Let the scheduler associate tasks and triggers to ensure that tasks are executed according to the conditions of triggers
        scheduler.scheduleJob(jobDetail, trigger);
        //start-up
        scheduler.start();
    }
}

The result is: the stateful job adds @ PersistJobDataAfterExecution / / to the HelloJob class. When the job is called multiple times, some data will be persisted and saved
Value accumulation of count

The result is: the stateful job does not add @ PersistJobDataAfterExecution on the HelloJob class, that is, the HelloJob class code is changed to
Helloshchedulerdemo class is the same as above

package com.quartz;

import javafx.scene.input.DataFormat;
import org.quartz.*;

import javax.naming.Context;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * package_name:com.quartz
 * Author:Xu Ya Yuan
 * Date:2020/1/14 14:20
 * Project Name: Quartz one
 * Desription:
 **/

 public class HelloJob implements Job {
    /*  public  HelloJob(){
          System.out.println("hello world");
      }*/

    public String getMessage() {
        return message;
    }

    public Integer getCount() {
        return count;
    }

    private String message;
    private Integer count;

    public void setCount(Integer count) {
        this.count = count;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //Get the contents of JobDetail
        JobKey jobKey = jobExecutionContext.getJobDetail().getKey();
        System.out.println("Name of task:" + jobKey.getName() + ";Name of the task force:" + jobKey.getGroup());
        System.out.println("Name of task class:(Tape path)" + jobExecutionContext.getJobDetail().getJobClass().getName());
        System.out.println("Name of task class:" + jobExecutionContext.getJobDetail().getJobClass().getSimpleName());
        //Get jobDataMap data from JobDetail object
        /*JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        System.out.println("messageJobDetail:" + jobDataMap.getString("message"));*/
        //Get Trigger content
        TriggerKey triggerKey = jobExecutionContext.getTrigger().getKey();
        System.out.println("Trigger name:" + triggerKey.getName() + ";Name of departure group:" + triggerKey.getGroup());
        System.out.println("Name of trigger class(Tape path):" + jobExecutionContext.getTrigger().getClass().getName());
        System.out.println("Name of trigger class:" + jobExecutionContext.getTrigger().getClass().getSimpleName());
        //Get jobDataMap data from Trigger object
       /* JobDataMap triggerDataMap = jobExecutionContext.getTrigger().getJobDataMap();
        System.out.println("messageTrigger:"+triggerDataMap.getString("message"));*/
        System.out.println("message information:" + message); //Note here: if you encounter a key with the same name, the. usingData("message", "print log Trigger") in the Trigger
        // Overwriting. usingData("message", "print log jobDetail") in jobDetail
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = simpleDateFormat.format(date);
        System.out.println("Database backup in progress:Database backup time:" + dateString);
        //Get more
        System.out.println("Execution time of current task:" + simpleDateFormat.format(jobExecutionContext.getFireTime()));
        System.out.println("Execution time of next task:" + simpleDateFormat.format(jobExecutionContext.getNextFireTime()));
         ++count;
         //Output the value of count
        System.out.println("count Value:"+count);
        //Store the value of count in the jobDataMap
       jobExecutionContext.getJobDetail().getJobDataMap().put("count",count );

    }
}

The result of stateless job class is: the value of count remains unchanged

Published 16 original articles, praised 0, visited 70
Private letter follow

Tags: Java Database log4j Maven

Posted on Wed, 15 Jan 2020 22:51:36 -0800 by accu